Android ViewPager - How to add buttons to the pages?
android, android-viewpager
Solution
Here is an example of inflating/programmatically adding views with buttons:
@Override
//Instantiate items based on corresponding layout IDs that were passed in
public Object instantiateItem(View container, int position)
{
//Inflate the correct layout
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//layouts[] is an int[] that points to resources such as R.layout.start_page
View inflatedView = layoutInflater.inflate(layouts[position], null);
((ViewPager)container).addView(inflatedView,0);
//If you can't/don't want to have the above inflated layout include everything up front, you can add them now...
LinearLayout ll = (LinearLayout)inflatedView.findViewById(R.id.linearLayout1);
Button newButton = new Button(context);
newButton.setText(R.string.button_text);
ll.addView(newButton);
return inflatedView;
}
Problem
I am following this link to do a ViewPager http://blog.stylingandroid.com/archives/537 I managed to get the various pages out. But how do I populate or fill these pages with buttons? instantiateItem() This creates the view for a given position. For a real application we would use a Fragment here, or inflate a layout, but to keep the example simple we create a TextView, set the text to the correct value from our titles array, and add it to the ViewPager I want to have different button functions on different pages. How do I go about doing this? By using different layout? How do I put the layout within the page (is this what you call inflat)? EDIT: I have the main.xml which consists of the ViewPager ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:weightSum="1"> <com.viewpagerindicator.TitlePageIndicator android:id="@+id/indicator" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> ``` I have several other xml which consists of buttons which I want to inflate it into the different pages of the ViewPage. Example of an 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" > <Button android:layout_width="match_parent" android:text="Button 1" android:onClick="onClick" android:id="@+id/button1" android:layout_height="100dp"></Button> <Button android:text="Button 2" android:onClick="onClick" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="100dp"></Button> <Button android:text="Button 3" android:onClick="onClick" android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="100dp"></Button> <Button android:text="Button 4" android:onClick="onClick" android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="100dp"></Button> <Button android:text="Button 5" android:onClick="onClick" android:id="@+id/button5" android:layout_height="match_parent" android:layout_width="match_parent"></Button> </LinearLayout> ``` In the ViewPagerAdapter.java, I managed to inflate the various XML into the pages. ``` @Override public Object instantiateItem( View pager, int position ) { //Inflate the correct layout LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //layouts[] is an int[] that points to resources such as R.layout.start_page View inflatedView = layoutInflater.inflate(layouts[position], null); ((ViewPager)pager).addView(inflatedView,0); return inflatedView; } ``` And this needs to be changed to prevent error. ``` @Override public void destroyItem( View pager, int position, Object view ) { ((ViewPager)pager).removeView( (View)view ); } ``` Now, I have multiple buttons on different pages. How do I programme the ONCLICK function of the different buttons on the different pages? Can this work? ``` public void onClick(View v) { switch (v.getId()) { case R.id.button1: // do something break; case R.id.button2: // do something break; case R.id.button3: // do something break; case R.id.button4: // do something break; case R.id.button5: // do something break; } } ```