Search code examples
androidbuttonandroid-toast

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


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?


Solution

  • 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>