Tuesday, 5 January 2016

Example for sliding multiple images in android

Step 1> create a xml file with a name of android_main.xml and paste fillowing code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" android:background="#ffffff">
 
 
    <ImageView android:id="@+id/imgView"
        android:layout_height="100dp"
        android:layout_width="100dp"
        android:src="@drawable/ic_launcher"
        />
    <TextView
        android:id="@+id/tv1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:textColor="#000000"
        android:textSize="30dp"
        />

</LinearLayout>



Step 2> write the below logic in MainActivity.java class

package com.example.sampleproject;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

ImageView imgView;
TextView tv1;
int[] a={R.drawable.a,R.drawable.b, R.drawable.c, R.drawable.d,R.drawable.e,R.drawable.f,R.drawable.g};
Handler myhHandler=new Handler();
    int i=0;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgView=(ImageView)findViewById(R.id.imgView);
        tv1=(TextView)findViewById(R.id.tv1);
        slide();
    }
    public void slide()
    {
             if(i<7)
             {
imgView.setImageResource(a[i]);
myhHandler.postDelayed(run, 2000);
i++;
             }
    }
    Runnable run=new Runnable()
    {
@Override
public void run()
{
              slide();
}
};
 
}


No comments:

Post a Comment