How can i set different styles for button selector according to current app theme?
Here is my button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<color android:color="@color/color_theme1"/>
</item>
<!-- pressed -->
<item android:drawable="@color/transparent"/>
<!-- default -->
</selector>
As your app theme color is in color_primary in color.xml. you can use that in your selector like this. But you have to create two drawables file one for default state and other for selected_state.
button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--selected/pressed/focused -->
<item android:state_selected="true"
android:drawable="@drawable/button_selected"
/>
<item android:drawable="@drawable/button_default"/>
<!-- default -->
</selector>
button_default.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient android:angle="270"
android:startColor="@color/gray"
android:endColor="#@color/gray"
/>
<!-- this will make corners of button rounded -->
<corners android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"/>
</shape>
button_selected.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<!--this is to give gradient effect -->
<gradient android:angle="270"
android:startColor="@color/color_primary"
android:endColor="#@color/color_primary"
/>
<!-- this wil make corners of button rounded -->
<corners android:topLeftRadius="5dip"
android:bottomRightRadius="5dip"
android:topRightRadius="5dip"
android:bottomLeftRadius="5dip"/>
</shape>
you have to programmatically do the following as well, so that the button will stay selected.
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.isSelected())
{
v.setSelected(false);
}
else
{
v.setSelected(true);
}
}
});
Use dynamic selector in your app so that you can assign colors according to your requirements.
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);
Or
Check if this or this can help you
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With