Nandhiya
Nandhiya

Reputation: 203

On Click listener is not working in View Pager

I am having two pages in ViewPager.In the two pages I am having buttons for which i have to make action on them.On click listener for button is working perfectly fine for second page and it is not working for first page. Here is the activity and adapter class which i have tried

    public class MainActivity extends Activity {
    int noofsize = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
                noofsize);
        ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
        myPager.setAdapter(adapter);
        myPager.setCurrentItem(0);
    }


}

And the adapter class is as follows

    public class ViewPagerAdapter extends PagerAdapter implements OnClickListener {
    int size;
    Activity act;
    View layout;
    TextView pagenumber;
    Button click;

    public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
        // TODO Auto-generated constructor stub
        size = noofsize;
        act = mainActivity;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return size;
    }

    @Override
    public Object instantiateItem(View container, int position) {
        // TODO Auto-generated method stub
        LayoutInflater inflater = (LayoutInflater) act
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layout = inflater.inflate(R.layout.pages, null);
        pagenumber = (TextView) layout.findViewById(R.id.pagenumber);
        pagenumber.setText("Now your in Page"+position);
        click = (Button) layout.findViewById(R.id.click);
        click.setOnClickListener(this);
        ((ViewPager) container).addView(layout, 0);
        return layout;
    }

    @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;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v == click) {
            Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
                    .show();
        }

    }

}

Upvotes: 1

Views: 10589

Answers (3)

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9765

if you want to catch button click and do something, it is better to do it in simplest possible way...
less code is better!
here we go;
inside your layout.xml of the button component (or whatever is the component) should have this

android:onClick="onClick_function"  

then you can automatically catch this event inside your code, all you need just have a function with the same name

public void onClick_function(View v) {
// here we go!
}

Upvotes: 2

Yog Guru
Yog Guru

Reputation: 2104

I Face this same problem,In my case it solved by set click listener in instantiateItem. You can do something like this may it can helpful

click.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });

or

You Can try this also

 click.setOnClickListener(act);

Upvotes: 1

g00dy
g00dy

Reputation: 6788

i think this part is wrong:

if (v == click) { // What is click ?! you can't compare that
    Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
            .show();
}

Replace it with:

if (v.getId() == R.id.theIdOfTheView) {
    Toast.makeText(act, "click event called", Toast.LENGTH_SHORT)
            .show();
}

Use this inside the instantiateItem:

container.onClickListener();

Upvotes: 0

Related Questions