Please do not judge me, I am very new to Android development ... I want to make an application with viewPager ... I have three pages with three different layouts ... But I just could not figure out how to work with the button on the second page in viewPager. I found this question. How to write onClick method button in viewpager? "and set my codes as follows:
ViewPagerAdapter:
@Override public Object instantiateItem( View pager, int position ) { View v = new View(pager.getContext()); LayoutInflater inflater = (LayoutInflater) pager.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); int resId = 0; switch (position) { case 0: resId = R.layout.deneme; break; case 1: resId = R.layout.deneme2; break; case 2: resId = R.layout.deneme3; break; } v = inflater.inflate(resId, null); ((ViewPager) pager).addView(v, 0); return v; }
Here I get errors: "The findViewById (int) method is undefined for the type new View.OnClickListener () {}" in the line
LinearLayout l = (LinearLayout) findViewById(R.id.layout2);
and "The getResources () method is undefined for the type new View.OnClickListener () {}" in the line
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
The main:
public class ViewPagerProjectActivity extends Activity { Button btn; View deneme2lout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ViewPagerAdapter adapter = new ViewPagerAdapter( this ); ViewPager pager = (ViewPager)findViewById( R.id.viewpager ); pager.setAdapter( adapter ); pager.setCurrentItem(0); LayoutInflater getView = getLayoutInflater(); deneme2lout = (View) getView.inflate(R.layout.deneme2,null); btn = (Button) deneme2lout.findViewById(R.id.button1); } public void buttonClick(View v) { if(v.equals(btn)) { deneme2lout.setBackgroundDrawable(getResources().getDrawable(R.drawable.background)); } } }
Since I get, I can’t use these two codes ... So how should I do this? I need to change the background of the second page when I click the button in the second layout.
And here are my deneme2.xml codes:
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout2" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="175dp" android:layout_height="95dp" android:layout_x="69dp" android:layout_y="174dp" android:textSize="25dp" android:text="Change" /> </AbsoluteLayout>
yahya source share