5

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?

Anjali Patel
  • 807
  • 2
  • 11
  • 23

3 Answers3

4

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>
Saurabh Padwekar
  • 3,888
  • 1
  • 31
  • 37
  • 2
    above code is correct make sure that your button is not disabled (check in xml and in your class file). It is working for me. – Saurabh Padwekar Dec 27 '16 at 10:42
  • yes that's working but i want util SET button is pressed all button should be disable and not perform any function – Anjali Patel Dec 27 '16 at 10:49
  • Change the background color and text color to make it look disabled and return when clicked.Check the updated answer . – Saurabh Padwekar Dec 27 '16 at 11:14
  • If i will change the background color the user can click on that button and move to another screen.I want if user clicks the action should not be performed util SET button is clicked. @Saurabh Padwekar – Anjali Patel Dec 27 '16 at 11:51
  • You are not changing the background color you are setting one flag also isActivated(). If it is not activated we will simply return. – Saurabh Padwekar Dec 27 '16 at 12:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131630/discussion-between-saurabh-padwekar-and-anjali-patel). – Saurabh Padwekar Dec 27 '16 at 21:42
2

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

        }
    });
Anjali Patel
  • 807
  • 2
  • 11
  • 23
0

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();
        }
    });
Android Geek
  • 8,956
  • 2
  • 21
  • 35