11

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.

3
  • Just to check,are you sure that each button has a different ID?
    – Lumis
    Dec 27, 2011 at 10:15
  • Hi Lumis, thanks for response, i have checked, each button have its unique id.
    – Himanshu
    Dec 27, 2011 at 10:18
  • 1
    Find it very interesting that one can alter the background for all buttons in this way, thanks for posting this problem.
    – Lumis
    Dec 27, 2011 at 10:34

4 Answers 4

20

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);
1
  • This doesn't work when the user has disabled animations on the device.
    – Denny
    Apr 12, 2017 at 18:53
8
nextBtn.getBackground().setAlpha(100);

or

nextBtn.setAlpha(0.5f); 
1
1
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);  
}
1
  • d.setAlpha(60); requires API level 11+
    – Uniruddh
    Apr 24, 2014 at 4:50
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.