Saturday, November 9, 2019

PagerTitleStrip in ViewPager Android

PagerTitleStrip android is a non-interactive signal of the current, next, and former pages of a ViewPager. It really is designed to be used as a child view of a ViewPager android widget
 The steps to create a PagerTitleStrip
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

Step 1. Create layout activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidviewpagerapp.MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:autoLink="web"
        android:text="http://android-er.blogspot.com/"
        android:textStyle="bold" />

    <android.support.v4.view.ViewPager
        android:id="@+id/myviewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
   
        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/titlestrip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
   
    </android.support.v4.view.ViewPager>
   
</LinearLayout>

Step 2. Create Activity contains ViewPager this (MainActvity.java)

public class MainActivity extends Activity {
 ViewPager viewPager;
 MyPagerAdapter myPagerAdapter;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  viewPager = (ViewPager)findViewById(R.id.myviewpager);
  myPagerAdapter = new MyPagerAdapter();
  viewPager.setAdapter(myPagerAdapter);


  PagerTitleStrip pagerTitleStrip = (PagerTitleStrip)findViewById(R.id.titlestrip);
 }
}
Step 3. Create a Adapter use custom PagerAdapter on viewpager.
  private class MyPagerAdapter extends PagerAdapter{


  int NumberOfPages = 5;


  int[] res = {
   android.R.drawable.ic_dialog_alert,
   android.R.drawable.ic_menu_camera,
   android.R.drawable.ic_menu_compass,
   android.R.drawable.ic_menu_directions,
   android.R.drawable.ic_menu_gallery};
  int[] backgroundcolor = {
   0xFF101010,
   0xFF202020,
   0xFF303030,
   0xFF404040,
   0xFF505050};


  String[] title = {
   "Page 1",
   "pAge 2",
   "paGe 3",
   "pagE 4",
   "page 5"};
  @Override
  public int getCount() {
   return NumberOfPages;
  }
  @Override
  public boolean isViewFromObject(View view, Object object) {
   return view == object;
  }
  @Override
  public Object instantiateItem(ViewGroup container, int position) {

 
      TextView textView = new TextView(MainActivity.this);
      textView.setTextColor(Color.WHITE);
      textView.setTextSize(30);
      textView.setTypeface(Typeface.DEFAULT_BOLD);
      textView.setText(String.valueOf(position));
 
      ImageView imageView = new ImageView(MainActivity.this);
      imageView.setImageResource(res[position]);
      LayoutParams imageParams = new LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
      imageView.setLayoutParams(imageParams);
 
      LinearLayout layout = new LinearLayout(MainActivity.this);
      layout.setOrientation(LinearLayout.VERTICAL);
      LayoutParams layoutParams = new LayoutParams(
        LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
      layout.setBackgroundColor(backgroundcolor[position]);
      layout.setLayoutParams(layoutParams);
      layout.addView(textView);
      layout.addView(imageView);
 
      final int page = position;
      layout.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View v) {
     Toast.makeText(MainActivity.this,
      "Page " + page + " clicked",
      Toast.LENGTH_LONG).show();
    }});
 
      container.addView(layout);
      return layout;
  }
  @Override
  public void destroyItem(ViewGroup container, int position, Object object) {
   container.removeView((LinearLayout)object);
  }
  @Override
  public CharSequence getPageTitle(int position) {
   return title[position];
  }} 
Resule image Pagertitlestrip in viewpager android example:
pagertitlestrip on viewpager

No comments:

Post a Comment