I want to completely change my button style in Android. I found an excellent answer for changing the button background here, but I can't figure out how to make the text style to change also in the same file.
Does anybody know how to do that?
I want to completely change my button style in Android. I found an excellent answer for changing the button background here, but I can't figure out how to make the text style to change also in the same file.
Does anybody know how to do that?
1.create your button
<Button
android:id="@+id/button"
android:background="@drawable/selector_xml_name"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Hello" />
2.create a selector.xml in your drawable
directory
<item android:drawable="@drawable/your_button_is_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/your_button_is_pressed" android:state_pressed="true"></item>
<item android:drawable="@drawable/your_button_in_default_state"></item>
your_button_is_selected,your_button_is_pressed,your_button_in_default_state are your custom xml files for each state that you want to change colors or etc.
android:background="@drawable/selector_xml_name"
if you don't have color codes create a one too res/values/colours.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="yellow">#FFFF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="red">#FF0000</color>
<color name="silver">#C0C0C0</color>
<color name="gray">#808080</color>
<color name="olive">#808000</color>
<color name="purple">#800080</color>
<color name="maroon">#800000</color>
<color name="aqua">#00FFFF</color>
<color name="lime">#00FF00</color>
<color name="teal">#008080</color>
<color name="green">#008000</color>
<color name="blue">#0000FF</color>
<color name="navy">#000080</color>
<color name="black">#000000</color>
</resources>
If I understand correctly, you want to create a background style for your button that has different interaction states, as well as text colors that respond accordingly.
You need to create 2 selectors, one sets the background color and the other sets the font color.
Below is a very basic example of doing this. There's more steps involved and more to do, but you should get an idea of what is involved.
// res/values/styles.xml
<style name="SomeStyle" parent="MyAppTheme">
<item name="android:background">@drawable/selector_background</item>
</style>
<style name="SomeTextView" parent="@android:style/TextAppearance">
<item name="android:textColor">@drawable/selector_text</item>
</style>
// Styling the text color selector
// res/drawable/selector_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/inactive_color" android:state_focused="true" android:state_pressed="false"/>
<item android:color="@color/active_color" android:state_pressed="true"/>
<item android:color="@color/active_color" android:state_drag_hovered="true" android:state_pressed="true"/>
<item android:color="@color/active_color" android:state_selected="true" android:state_pressed="true"/>
<item android:color="@color/inactive_color"/>
</selector>
// The background selector
// res/drawable/selector_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_selector_unselected" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="@drawable/background_selector_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/background_selector_unselected"/>
</selector>
// Styling the selected background state
// res/drawable/selector_background_state_active.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/shadow_color_1" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="@color/shadow_color_2" />
</shape>
</item>
<item android:bottom="1.5dp">
<shape android:shape="rectangle">
<solid android:color="@color/active_background_color" />
</shape>
</item>
</layer-list>