Home » Android » How to setAlpha to android Button

How to setAlpha to android Button

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have three buttons, sharing same background image, i want to disable button one of them by using Alpha.

But when i am using the following code:

 button1.getBackground().setAlpha(45);

it is changing the background for all three buttons. but i need for only one.
can we done by using by Alpha()?? or some other things we can use so that button will looks in disabled mode.

How to&Answers:

You can set alpha using AlphaAnimation to any view

Sample Code

Button btn = (Button) findViewById(R.id.button);  
float alpha = 0.45f;
AlphaAnimation alphaUp = new AlphaAnimation(alpha, alpha);
alphaUp.setFillAfter(true);
btn.startAnimation(alphaUp);

Answer:

nextBtn.getBackground().setAlpha(100);

or

nextBtn.setAlpha(0.5f); 

Answer:

Button btn;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);   
    setContentView(R.layout.main);  
    btn = (Button) findViewById(R.id.main_btn);  
    Drawable d = getResources().getDrawable(R.drawable.imagen);  
    d.setAlpha(60);  
    btn.setBackgroundDrawable(d);  
}

Answer:

In my case I set one button to alpha 75. But all the other buttons with same background color also changed to alpha 75. Calling mutate() solved the problem.

Pavel Dudka’ answer works perfectly for my case.

buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);