Button transparency does not change with 'setAlpha'

I am developing a simple application that has a set of 6 buttons. when I clicked on one of these buttons, the rest of the buttons should be partially transparent. I tried to do this by setting alpha from 5 buttons

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

The app's ui has not changed as I expected. I got only 3 out of 5, it becomes transparent. When you click on these two buttons, it slowly changes transparency.

Thanks in advance

Regards, kariyachan

+3
source share
4 answers
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 :)

+1
source

Try the following:

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

android-sdk : android-sdk\platforms\android-10\data\res\drawable-mdpi\btn_default_normal.9.png

, ( , 9-patch, ).

, , :

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

to make it translucent and

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

to change it.

0
source

For those who are still looking for a solution to this:

Method setBackgroundDrawable(Drawable d) deprecated as API 16

Assuming your button id buttonId, and your drawable is called button_img, handle this using the following in 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!

0
source

Source: https://habr.com/ru/post/1777165/


All Articles