How to display a message when clicked on disable button in android?

5.4k views Asked by At

I want to display a Toast message when clicked on disable button.

   button.setEnable(false);                                                                                

       button.setOnClickListener(new View.OnClickListener()
    {
            @Override
            public void onClick (View v)
            {


                Toast.makeText(SliderDemo.this, "Button Disabled",Toast.LENGTH_LONG).show();        }}

Can we use both Touch listener and Click listener on same button?

3

There are 3 answers

6
Saurabh Padwekar On BEST ANSWER

You cant click a disabled button.Try doing this ,

// if you want to show it as disabled simply change the button background and text color
    button.setActivated(false); 
    button.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.disabled_background_color));
    button.setTextColor(ContextCompat.getColor(getContext(),R.color.disabled_text_color));
    button.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick (View v){
            if(!button.isActivated()){
            Toast.makeText(SliderDemo.this, "Button Disabled",Toast.LENGTH_LONG).show();
            return; 
          }
         //else do your stuff
     }

Add this lines in your color.xml

<color name="disabled_background_color">#10181818</color>
<color name="disabled_text_color">#aaa</color>
0
Android Geek On

You can set aplha for the button porgromatically: button.getBackground().setAlpha(128); //For 50% transparency.Alpha ranges from 0 (fully transparent) to 255 (fully opaque).

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
             Toast.makeText(SliderDemo.this, "Button Disabled",Toast.LENGTH_LONG).show();
        }
    });
0
Anjali Patel On

If you have to do 2 actions on a button than use this as i am disabled the button and enabled only when it SET button is clicked

  button.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            Log.i("Touch eventssssss","Inside onTouch");
            if(button.isActivated())
            {
                Toast.makeText(SliderDemo.this, "Your Message On Disabled Button ", Toast.LENGTH_SHORT).show();
                return true;
            }
            else
            {
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                startActivity(intent);
                return true;
            }

        }
    });