Dev.Sinto
Dev.Sinto

Reputation: 6852

transparency of button is not changed using 'setAlpha'

I am developing a simple application ,in that there is set of 6 buttons. when i clicked on one of these button then the other buttons must be partially transparent. I tried to done that By setting alpha of 5 buttons

acre_button.getBackground().mutate().setAlpha(155);

Ui of the application not changed as i expected. i got only 3 out of 5 is get transparent.when clicking on that two button it is slowly changing it's transparency

Thanks in advance

regards, kariyachan

Upvotes: 3

Views: 3538

Answers (4)

Kakey
Kakey

Reputation: 4024

Try the following:

button.setBackgroundColor(android.R.color.transparent);

Upvotes: 0

ian_donguston
ian_donguston

Reputation: 1

For anyone still hunting for a solution to this:

The method setBackgroundDrawable(Drawable d) is deprecated as of API 16

Assuming your button's id is buttonId and your drawable is named button_img, handle this by using the following within the onCreate method:

((Button)(findViewById(R.id.buttonId))).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Drawable d = v.getResources().getDrawable(R.drawable.button_img);
            d.setAlpha(40);  
            if (Build.VERSION.SDK_INT >= 16)
                v.setBackground(d);
            else
                v.setBackgroundDrawable(d);
            //Then call your next Intent or desired action.

        }
    });

Tested and works for me!

Upvotes: 0

Litus
Litus

Reputation: 632

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

This works for me :)

Upvotes: 1

Graeme
Graeme

Reputation: 25864

Find a button background in your android-sdk directory here: android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png

You can modify it to make it semi transparent (Please note that this is a 9-patch and you shouldn't change the opacity of the black lines).

Once you have this changed button in your drawable directory you can add this to your code:

button.setBackgroundDrawable(getResources().getDrawable(R.drawable.transparentImage));

to make it semi transparent and

button.setBackgroundDrawable(getResources().getDrawable(Android.R.drawable.btn_default_normal));

to change it back.

Upvotes: 0

Related Questions