Thursday, February 21, 2019

Swipe screen with viewpager

We want user can change to next screen by use their finger to swipe on screen
Create a class with activity_main.xml like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >   

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
In folder layout, create three xml file name ac.xml, ac2.xml, ac3.xml like below.
ac.xml
<?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:background="#ffffff"
    android:orientation="vertical" >
   <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Text example"
        android:textColor="#800000"
        android:textSize="20dp" />
    <Button
        android:id="@+id/bu1"
        android:layout_width="85dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="Button 1"
        android:textColor="#0000cd"
        android:textSize="14sp" />


</LinearLayout>
ac2.xml
<?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" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Text example 2"
        android:textColor="#800000"
        android:textSize="20sp" />

    <Button
        android:id="@+id/bu1"
        android:layout_width="65dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="Button 2"
        android:textColor="#0000cd"
        android:textSize="14sp" />

</LinearLayout>
ac3.xml
<?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" >
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="5dp"
        android:text="Text example 3"
        android:textColor="#800000"
        android:textSize="20dp" />
   
    <Button
        android:id="@+id/bu"
        android:layout_width="105dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:text="Button 3"
        android:textColor="#0000cd"
        android:textSize="15sp" />
</LinearLayout>
Create a class name myadapter extends PagerAdapter{
     public int getCount() {
        return 3;
    } 
    public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.ac;           
            break;
        case 1:
            resId = R.layout.ac2;
            break;
        case 2:
            resId = R.layout.ac3;
            break;
        }       
  View view = inflater.inflate(resId, null);   
       if(position==0){
        final TextView tv = (TextView) view.findViewById(R.id.tv2);
        tv.setText("Example text 1");
        Button b1 = (Button) view.findViewById(R.id.bu1);
        b1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                      tv.setText("Button clicked");
               }
          });
        }
        else if(position==1){
          final TextView tv = (TextView) view.findViewById(R.id.tv);
            tv.setText("Example text 2");
            Button b1 = (Button) view.findViewById(R.id.bu1);
            b1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                      tv.setText("Button clicked");
               }
          });
        }
        else {
          final TextView tv = (TextView) view.findViewById(R.id.tv);
            tv.setText("Example text 3");
            Button b1 = (Button) view.findViewById(R.id.bu);
            b1.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                      tv.setText("Button clicked");
               }
          });
  }     
((ViewPager) collection).addView(view, 0);
   return view;
    }
    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);
    }
    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);
    }
    @Override
    public Parcelable saveState() {
        return null;
    }
Import liraries






In class MainActivity, copy to below setContentView.
myadapter adapter = new myadapter(this);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);   
viewPager.setAdapter(adapter);     
viewPager.setCurrentItem(0);
Run and swipe to new screen.



No comments:

Post a Comment