Please don't judge me, i'm extremely new on android developing... I wanna make an app with viewPager... I've got three pages with three different layouts... But i just couldn't figure out how to deal with a button on second page on viewPager. I found this question " How to write button onClick method in viewpager? " and set my codes like this:
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'm getting errors: "The method findViewById(int) is undefined for the type new View.OnClickListener(){}" on line
LinearLayout l = (LinearLayout) findViewById(R.id.layout2);
and "The method getResources() is undefined for the type new View.OnClickListener(){}" on line
l.setBackgroundDrawable(getResources().getDrawable(R.drawable.background));
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));
}
}
}
So as i get, i cannot use those two codes... So how should i do it? I need to change background of the second page when clicked the button on second layout.
And here's 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>
As for the second part of your question, it's simple! Although you now use View
v
all along, you still "overwrite" it with a new inflation at the end:So, changing the following bits should make it work perfectly:
View v = new View(pager.getContext());
toView v = null;
if (v == null) v = inflater.inflate(resId, null);
In response to your most recent question, you can set an onClick listener outside of the adapter and within the activity like this:
In your xml containing the button, make sure you have android:onClick="buttonClick" as 1 of the properties.
In your xml containing the button, make sure you have android:onClick="buttonClick" as 1 of the properties.
Edit -- I don't think you applied the changes I suggested from your most recent code... here is what I was thinking:
If that's not it, then please let us know the error so we can lead you in the right direction.
That's because, as it explains, OnClickListener doesn't have these two methods which you're using inside an OnClickListener. Replace:
with
if it then says v should be final, that should be okay. And replace:
with