4

So right now i am having trouble making the next button unclickable when it is on the last page of the activity. As of right now it goes back to the first screen. How do i make it so that it know when to grey out the button or make it unclickable when the user gets to the last screen.

Here is my code:

public class ReadingActivity extends Activity implements OnClickListener {

    private ViewFlipper viewFlipper;
    Button btnNext, btnPrev;
    private float lastX;

    /** Called when the activity is first created */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reading);
        viewFlipper=(ViewFlipper)findViewById(R.id.view_flipper);
        btnNext=(Button)findViewById(R.id.btnNext);
        btnPrev=(Button)findViewById(R.id.btnPre);


        btnNext.setOnClickListener(this);
        btnPrev.setOnClickListener(this);

        btnNext.setEnabled(true);
    }



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

        switch(arg0.getId()){
        case R.id.btnNext:
            viewFlipper.setInAnimation(this, R.anim.in_from_right);
            viewFlipper.setOutAnimation(this, R.anim.out_to_left);
            viewFlipper.showNext();
            break;
        case R.id.btnPre:
            viewFlipper.setInAnimation(this, R.anim.in_from_left);
            viewFlipper.setOutAnimation(this, R.anim.out_to_right);
            viewFlipper.showPrevious();
            break;
        }

    }


}
Christian Ternus
  • 8,406
  • 24
  • 39
Dan
  • 63
  • 1
  • 2
  • 8

3 Answers3

4

you can set the OnClickListener to null like so

btnNext.setOnClickListener(null);
JRowan
  • 6,824
  • 8
  • 40
  • 59
  • what does this do exactly? Kind of new to android development.. Thanks! – Dan Oct 28 '13 at 01:46
  • it will stop the OnClick method from firing for that object, because it is set from (this) to (null) it removes the listener from the object – JRowan Oct 28 '13 at 01:48
  • and if you want to put it back onto the button, set it back to (this) from null – JRowan Oct 28 '13 at 01:49
  • oh ok thanks i tried this and it works but it not what i was looking for. i only need it to work for the last page. I only have two buttons that does not change only the images change so yeah. – Dan Oct 28 '13 at 01:51
  • you could use a boolean to trigger when its on the last page, set it to (null) then if they hit the previous button and the boolean is true or whatever set the listener bacl to (this) – JRowan Oct 28 '13 at 01:55
  • While that is a solution, I highly recommend @Dan having a look at [ViewPager](http://developer.android.com/reference/android/support/v4/view/ViewPager.html), instead of `ViewFlipper`, since it has callbacks to handle paging events – Ole Oct 28 '13 at 01:59
  • with viewflipper you would have to track the index with viewFlipper.getDisplayedChild () – JRowan Oct 28 '13 at 02:03
  • you could even use that in an if(){} with viewFlipper.showNext() and make it not happen if it is on the last index – JRowan Oct 28 '13 at 02:04
  • alright sounds good. thanks a bunch will give it try – Dan Oct 28 '13 at 02:05
2

I think you just want this method

button.setClickable(false);
Ole
  • 7,899
  • 2
  • 29
  • 34
Ryan Hu
  • 21
  • 1
  • thanks i tried button.setClickable already but it did not do what i wanted.. :( I have two consistent buttons so only the images change. If i do button.setClickable it will disable it completely.. – Dan Oct 28 '13 at 01:42
1

To make a button grey and UnClickable

put android:enabled="false" in button tag

And using code button.setEnabled(false);

Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300