maddy
maddy

Reputation: 4111

How to disable button click?

In my android application, there are number of images in drawable folder. In my layout there are two buttons: Back and Next buttons. On clicking next and back buttons 2 different images get loaded on the same layout(common for all images).

Problem:I am able to load images on Next/Back button's click but after reaching the last image, I want to make my Next button disable and same for the back button.As the user is on the first image the back button should be disabled. Code is as:

public class SequencerActivity extends Activity implements OnClickListener
    {
        private int imageCounter = 0;
        private ImageView imageLoader;

        private int[] imageList = {R.drawable.image_wo_lbl_0, R.drawable.image_wo_lbl_1, R.drawable.image_wo_lbl_2, R.drawable.image_wo_lbl_3, R.drawable.image_wo_lbl_4, R.drawable.image_wo_lbl_5,
                R.drawable.image_wo_lbl_6, R.drawable.image_wo_lbl_8, R.drawable.image_wo_lbl_9,R.drawable.image_wo_lbl_10, R.drawable.image_wo_lbl_11};
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            setContentView(R.layout.parent_frame);//this one is the common parent layout for all image views
            super.onCreate(savedInstanceState);

            /*requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);*/

            //int image1 = R.drawable.image_w_lbl_0;

            imageLoader = (ImageView) findViewById(R.id.imageLoader);
            //imageLoader.setImageResource(image1);

            ImageButton next = (ImageButton) findViewById(R.id.next);
            ImageButton back = (ImageButton) findViewById(R.id.back);
            next.setOnClickListener(this);
            back.setOnClickListener(this);
            //show the default image
            this.loadImage(imageList[imageCounter]);

        }
        @Override
        public void onClick(View v) 
        {
            int imagePath = 0;
            // TODO Auto-generated method stub
            switch (v.getId())
            {
            case R.id.next:
                Log.i("Tag","tag");
                if(imageCounter < imageList.length)
                {
                    imageCounter++;
                    imagePath = imageList[imageCounter];
                    if (imageCounter==(imageList.length)-1)
                    {
                        //how to make my next button disable

                    }
                }
                break;
            case R.id.back:
                if(imageCounter > 0)
                {
                    imageCounter--;
                    imagePath = imageList[imageCounter];
                    if (imageCounter==0)
                    {
                        //how to make my back button disable
                    }
                }
                break;
            }
            this.loadImage(imagePath);

        }

        private void loadImage(int imagePath)
        {
            imageLoader.setImageResource(imagePath);

        }
    }

Upvotes: 20

Views: 90960

Answers (6)

Bercove
Bercove

Reputation: 1175

For Kotlin you can use this

override fun onClick(v: View) {
    when (v.id) {
        R.id.cancel_button -> {
            button.isClickable = false
            button.isEnabled = false
        }
    }
}

Upvotes: 1

U.P
U.P

Reputation: 7442

KOTLIN:

next.isClickable = false

JAVA:

next.setClickable(false);

Upvotes: 21

All Іѕ Vаиітy
All Іѕ Vаиітy

Reputation: 26372

With kotlin you can disable a button with

button.isEnabled = false

or to disable clicking on it

button.isClickable = false

Upvotes: 3

Narasimha
Narasimha

Reputation: 809

more preferred solution is,

onclick(){
  btn.setEnabled(false);
  btn.setClickable(false);
  //yourwork
  myWork();
}

myWork(){
 //your tasks.
 btn.setEnabled(true);
 btn.setClickable(true);
}

As a link can be ignored easily, I had to repeat this again and again

Upvotes: 8

Avinash Kumar Pankaj
Avinash Kumar Pankaj

Reputation: 1720

just insert two extra images in your drawable folder one for disabled right arrow and one for disabled left arrow.

now try this

         case R.id.next:

         Log.i("Tag","tag");

            if(imageCounter < imageList.length)


            {
                imageCounter++;

                imagePath = imageList[imageCounter];

                if (imageCounter==(imageList.length)-1)

                {


                      //disabling right button by changing image from following code                                                                                                                

                      next.setImageDrawable(getResources().getDrawable(R.drawable.right_disabled));

                }
            }
            break;
        case R.id.back:

            if(imageCounter > 0)

            {
                imageCounter--;

                imagePath = imageList[imageCounter];

                if (imageCounter==0)

                {
                      back.setImageDrawable(getResources().getDrawable(R.drawable.left_disabled));
                }
            }
            break;

Upvotes: 0

viv
viv

Reputation: 6177

case R.id.next:
        Log.i("Tag","tag");
        if(imageCounter < imageList.length)
        {
            imageCounter++;
            imagePath = imageList[imageCounter];
            if (imageCounter==(imageList.length)-1)
            {
                ImageButton next=(ImageButton)findViewBYId(R.id.next);
                next.setEnabled(false);
            }
        }
        break;
    case R.id.back:
        if(imageCounter > 0)
        {
            imageCounter--;
            imagePath = imageList[imageCounter];
            if (imageCounter==0)
            {
                ImageButton back=(ImageButton)findViewBYId(r.id.back);
                back.setEnabled(false);
            }
        }
        break;

Upvotes: 19

Related Questions