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.

Lucifer
  • 29,392
  • 25
  • 90
  • 143
Himanshu
  • 1,066
  • 6
  • 19
  • 44

4 Answers4

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);
rekire
  • 47,260
  • 30
  • 167
  • 264
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243
8
nextBtn.getBackground().setAlpha(100);

or

nextBtn.setAlpha(0.5f); 
Fakhar
  • 3,946
  • 39
  • 35
  • 1
    Note, nextBtn.setAlpha(0.5f); setAlpha(int) has been around since API level 1, but is deprecated since level 16. setAlpha(float) continues to work. Consider this: http://stackoverflow.com/questions/21831775/difference-between-setalpha-and-setimagealpha – Brian Bird Oct 11 '16 at 19:19
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);
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
meazuri
  • 73
  • 1
  • 11
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);  
}
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58