android image button background, source and selector - android

I've got some imagebuttons (about 15) with different backgrounds and sources. I want to change the pressed state of all of them on the same way: adding a color overlay.
Is there a way without onTouchListern (all examples I tried didn't work correctly...) and without a separat active-image and selector.xml for each button, like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_one_active" android:state_pressed="true"/>
<item android:drawable="#drawable/button_one"/>
</selector>
ImageButton (current state):
<ImageButton
android:id="#+id/button_one"
android:layout_width="53dp"
android:layout_height="53dp"
android:background="#drawable/button_one_background"
android:contentDescription="#string/button_one_description"
android:onClick="onButtonClick"
android:scaleType="fitCenter"
android:layout_alignTop="#+id/button_two"
android:layout_toRightOf="#+id/button_two" />
Source is set in Java with setImageResource or setImageBitmap

In java code, for the onClicklisteners of the buttons add,
button.setBackgroundResource(R.drawable.button_pressed_image);

Related

Button with getBackground().setAlpha on version 5 - lollipop isn't working correctly

I have this code and works for every version since API 14 but on Android 5.0 (Lollipop) isn't working correctly.
Below is the way how I want the buttons to appear.
click of button1
buttonArrivals.getBackground().setAlpha(180);
buttonDepartures.getBackground().setAlpha(255);
click of button2
buttonArrivals.getBackground().setAlpha(255);
buttonDepartures.getBackground().setAlpha(180);
On the Lollipop version, the buttons appear with the same Alpha but I never set the same alpha. I just use the code above.
UPDATE 24/11/2014
Here is the XML of the buttons (AutoResizeButton extends Button)
br.com.timo.gru.util.AutoResizeButton
android:id="#+id/buttonArrivals"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00abbd"
android:drawableLeft="#drawable/icon_aviao_desemb"
android:drawablePadding="-5dp"
android:drawableStart="#drawable/icon_aviao_desemb"
android:gravity="center"
android:paddingEnd="0dp"
android:paddingLeft="2dp"
android:paddingRight="0dp"
android:text="#string/chegadas"
android:textColor="#android:color/white"
br.com.timo.gru.util.AutoResizeButton
android:id="#+id/buttonPartidas"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#00abbd"
android:drawableLeft="#drawable/icon_aviao_partida"
android:drawablePadding="-5dp"
android:drawableStart="#drawable/icon_aviao_partida"
android:ellipsize="end"
android:gravity="center"
android:text="#string/partidas"
android:textColor="#android:color/white"
Internally ColorState (used by ColorDrawable) is shared between these 2 buttons (optimization), so whenever you change alpha on one button's background - other button will get this change as well.
You can try to to mutate background drawable before changing its alpha:
buttonArrivals.getBackground().mutate().setAlpha(180);
buttonDepartures.getBackground().mutate().setAlpha(255);
You can also read good explanation from Romain Guy on why this is happening: http://curious-creature.org/2009/05/02/drawable-mutations
However, it looks like you try to implement something which is easily achievable with Android selectors. You can specify different color for each button state (in your case selected/not selected), so in your code you just need to update state:
buttonArrivals.setSelected(true);
buttonDepartures.setSelected(false);
And selector would look like:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#ff00abbd"
android:state_selected="true" >
</item>
<item android:color="#b400abbd"
android:state_selected="false">
</item>
</selector>

Custom Radio Button with State List Drawable not Changing

I have a set of Radio Buttons in a RadioGroup. I have created a StateList Drawable to indicate the state of each button. The buttons operate properly in that selecting any one will kick off the listeners, etc. However, the StateList Drawable isn't working. Here's the relevant stuff:
Layout XML with button:
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:button="#drawable/score_button_selector" />
score_button_selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/score_bg_2c_on" />
<item android:state_checked="false"
android:drawable="#drawable/score_bg_2c_off"/>
</selector>
score_bg_2c_off is a blue button:
and score_bg_2c_on is a green button:
The blue button appears properly, but when selected (pressed/clicked), the green one should appear instead. As I said, the operation of the button is fine, I get it's value properly, etc. - just not the drawable change. I tried state_selected instead of state_checked with no better results.
Any ideas why this isn't working as I'd like?
Thanks.
Here's some more data... I took out the android:button= and put the drawable on the android:background=.
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:background="#drawable/score_button_selector" />
This way the default radio button shows up. This button shows when it is selected (blue inside the button), but the background never changes. I know it's seeing the score_button_selector drawable because it's showing the button with the blue background that is only defined in the drawable.
Try this
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:button="#android:color/transparent"
android:background="#drawable/score_button_selector" />
Nothing a good project clean can't fix! I think that perhaps a majority of the time doing a clean as a last resort b-4 posting a question is a good protocol. It cleared this problem up after chasing ghosts for 3 days.

Android - change background of component while KEEPING selector?

Is this possible? I have for example simple linearLayout with selector to create "click effect":
<LinearLayout
android:id="#+id/clickToChangeColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect" />
This is selector click_effect:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="#drawable/unpressed" android:state_enabled="false"/>
</selector>
Everything works just fine, but when I press on this layout, I would like to launch activity (like color picker - but it doesn't matter) and then change color of linearlayout for example to blue. BUT: keep selector "click effect". Trying to do this almost for 2 hours, but nothing works...
PS: I know it can be done for example with another layout inside this layout, apply some padding and apply selector to outer layout and then I can change background of inner layout etc - but it's only ugly workaround
<ImageView
android:id="#+id/imageViewSelectedColor"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#color/click_effect"
android:src="#color/picked color" />
Use an image view instead now you can change src according to picked color.

What makes a button change color on mouseclick?

Why is this button changing color to orange when clicked:
<Button android:background="#android:drawable/btn_plus" ...>
but this one is not?
<Button android:background="#drawable/ic_btn_round_plus" ...>
Edit:
Found another type of button (text and image) that changes color to orange when clicked
without having to create a selector:
<Button android:text="List" android:drawableTop="#drawable/list" ...>
because the first one is from android framework and has a selector associated to it, and the other one is a custom from your code, and you obviously didn't put a selector on it.
This is nicely explained here.
In short you need to put a selector drawable in the background of your button, instead of just one drawable :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="#drawable/ic_btn_round_plusorange" />
<item android:state_pressed="true" android:drawable="#drawable/ic_btn_round_plusorange" />
<item android:drawable="#drawable/ic_btn_round_plus" />
</selector>
and you create you copy of your drawable but with an orange color added to it for instance.
Android system will switch the drawable when the button is clicked or selected.

ImageButton in Android homescreen widget

I have a homescreen widget with an imagebutton. I have the button working with a pending intent, but I can't seem to figure out how to change the button image when it is pressed.
I tried using a selector and it works in my widget test activity, but not in the remoteview.
How could I implement this functionality in the home screen widget?
You can set a different Image within your remote view.
remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);
I was able to get it working by moving the selector inside it's own XML file and putting in res/drawable and then referencing that xml file as my resource for my imagebutton in the app-widget's layout xml.
buttonimg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/button_off" />
<item
android:state_pressed="true"
android:drawable="#drawable/button_on" />
<item
android:drawable="#drawable/button_off" />
</selector>
widget_layout.xml
<ImageButton
android:id="#+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="#drawable/button"
>
I now have a different problem, however. The selector only allows me to change the image onPress. On release, the image changes back >:-(
How do I get the image state change on press and stay in that state until next press (like the ToggleButton)?

Categories

Resources