Android: set the alpha button when pressed. Only xml preferred

I want to introduce a down style on some buttons. I created a style and a list of states.

<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/inactive_button_background" android:state_enabled="false"/> <item android:drawable="@drawable/active_button_background" android:state_enabled="true"/> <item android:drawable="@drawable/pressed_button_background" android:state_pressed="true"></item> </selector> 

The problem is that I want to be able to change only the alpha of the background when I click the button (I will have variable backgrounds, so setting up a sold color with an alpha channel is not a solution).

And I want to do this only from declarative xml (I do not want to change my code using the layout).

The problem is that I donโ€™t know how to apply this alpha blending to a button from xml drawable. I am sure there is a way.

+7
source share
6 answers

I'm a little late to answer this question, but I just did it by changing the alpha value using View.onTouchListner

You can do this by implementing onTouch this way

  @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: v.setAlpha(0.5f); break; case MotionEvent.ACTION_UP: v.setAlpha(1f); default : v.setAlpha(1f) } return false; } 

I assume that you cannot change the alpha range value from drawable, so you only have to do it this way.

+5
source

It works great


 yourView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: { v.setAlpha(0.5f); break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { v.setAlpha(1f); break; } } return false; } }); 
+3
source

You will need to insert something into your code.

XML

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text" android:onClick="dialogClicked" /> 

Java

 public void dialogClicked(final View view) { ... set the alpha programatically ... } 
0
source

When adding something to Michael, answer if you're looking for a โ€œsofterโ€ click animation. I added:

 btn.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent motionEvent) { switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: v.animate().alpha(0.3f).setDuration(200).start(); break; case MotionEvent.ACTION_UP: v.animate().alpha(1f).setDuration(200).start(); default : v.setAlpha(1f); } return false; } }); } 

with a lie on the same idea, but with animation

0
source

I know this is a pretty old question, however I don't see the correct answer according to the question regarding alpha change using xml.
To do this, you need to create a selector, which is already shown in the question, and this:

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/inactive_button_background" android:state_enabled="false"/> <item android:drawable="@drawable/active_button_background" android:state_enabled="true"/> <item android:drawable="@drawable/pressed_button_background" android:state_pressed="true"></item> </selector> 

So, if you only have a normal state, choosing (suppose it is btn_background.png) and you need 50% alpha in the down state, all you have to do is create the corresponding xml-drawable namedpressed_button_background.xml (in a folder with the option transfer, no -dpi!). So it should look like this:

 <?xml version="1.0" encoding="utf-8"?> <bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/btn_background" android:alpha=".5" /> 

The same applies to the disconnected state, the only difference, I think, is the alpha level, which for this state can be 30% or whatever the designer says / wants). Thus, the shorter version will look like this (for a button with drawable ic_edit.png):

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="true" android:state_pressed="true"> <bitmap android:src="@drawable/ic_edit" android:alpha=".5" /> </item> <item android:state_enabled="false"> <bitmap android:src="@drawable/ic_edit" android:alpha=".3" /> </item> <item android:drawable="@drawable/ic_edit" /> </selector> 
0
source

This will pretty much do it. Well, that worked for me.

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <solid android:color="#5000ddff" /> </shape> </item> <item android:drawable="@color/yourdefaultcolor"/> </selector> 
-one
source

All Articles