Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setAlpha to android Button

Tags:

android

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.

like image 263
Himanshu Avatar asked Dec 27 '11 10:12

Himanshu


4 Answers

nextBtn.getBackground().setAlpha(100);

or

nextBtn.setAlpha(0.5f); 
like image 194
Faakhir Avatar answered Nov 18 '22 17:11

Faakhir


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);
like image 35
meazuri Avatar answered Nov 18 '22 16:11

meazuri


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);
like image 20
Sunil Kumar Sahoo Avatar answered Nov 18 '22 16:11

Sunil Kumar Sahoo


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);  
}
like image 1
NikhilReddy Avatar answered Nov 18 '22 16:11

NikhilReddy