Android – set the selector for the button and publish it programmatically
I have a row of buttons, and I am programmatically setting the selector for background and text. The reason I want to do this programmatically is because I have a set of topics that users can select, and I want to change the selector of the button according to the selected topic
For example, if the user selects the blue theme, the background of the button will be blue and the text color will be white when loading. When he presses the button, the background will become white and the text color will become blue. When the user removes his finger from the button, the change will revert to the default. Blue represents the background and white represents the text color. You can see the corresponding blue selector below
This is similar to all other themes. I have separate XML for all themes. The selector for text color change works normally. The problem is the background selector of the button
selector_ background_ blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" android:state_pressed="true"/>
<item android:drawable="@color/blue_500"/>
</selector>
color_ selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/blue_500"/>
<item android:color="@android:color/white"/>
</selector>
I have a class that returns drawable (selector) according to the selected topic. I get the selector as follows:
public Drawable getButtonBackgrounds(String theme) {
Drawable drawable = null;
if (theme.equalsIgnoreCase(Const.Theme.BLUE))
drawable = context.getResources().getDrawable(
R.drawable.selector_background_blue);
return drawable;
}
I'm setting these selectors for the background of the button, as follows:
private void setButtonBackgrounds(Drawable buttonDrawable) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
btnA.setBackgroundDrawable(buttonDrawable);
btnT.setBackgroundDrawable(buttonDrawable);
.....
.....
btnVoice.setBackgroundDrawable(buttonDrawable);
} else {
btnA.setBackground(buttonDrawable);
btnT.setBackground(buttonDrawable);
.....
.....
btnVoice.setBackground(buttonDrawable);
}
}
Button XML:
<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_t"
android:textSize="22sp" />
XML for total row:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<Button
android:id="@+id/btnA"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/arithmetic_symbol"
android:textSize="16sp" />
<Button
android:id="@+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/trigonometric_symbol"
android:textSize="16sp" />
<Button
android:id="@+id/btnN"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/voice_calculator_symbol"
android:textSize="16sp"
android:visibility="gone" />
<ImageButton
android:id="@+id/btnVC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="@string/empty"
android:src="@drawable/ic_keyboard_voice_black"
android:text="" />
<Button
android:id="@+id/btnC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_c"
android:textSize="16sp" />
<Button
android:id="@+id/btnD"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="@string/button_del"
android:textSize="16sp" />
</LinearLayout>
This is the same for all buttons in the row
The load that can be plotted is set well. Please refer to the figure below
The problem is that when I click a button (for example, a), the adjacent imagebutton (microphone) is also changing its state. Please see the following picture:
Why is that? Can someone help me with this? If you need any other information, please let me know
I think you're having a mutation related problem (see here, which is very useful)
If you do not want to share public status in various instances, you need to call mutate () before rendering, and then assign it to View:
Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.btn);
buttonDrawable.mutate()
btnA.setBackgroundDrawable(buttonDrawable);
In your code, you use the same drawable for multiple views, so you need to adopt the method I described above to avoid state sharing