1

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?

Community
  • 1
  • 1
Osdon
  • 126
  • 1
  • 14
  • post what you have done. where you went wrong! According to the ex. that you provide create a resource xml file for the style and in your view xml under the button tag link it as its background – Charuක Dec 13 '16 at 02:21
  • i have done the same as this [Link](http://stackoverflow.com/a/1726352/7105612) but i want also to modify the text style in the three different states of the button :idle-pressed-focused – Osdon Dec 13 '16 at 02:25
  • haha till i ask you haven't mention that in your question. arn't you? just mentioned three models. mm find for the selector tag in buttons background – Charuක Dec 13 '16 at 02:27
  • http://stackoverflow.com/questions/14023886/android-button-selector – Charuක Dec 13 '16 at 02:28
  • Yeah that is what i was looking for, Appreciate it !! – Osdon Dec 13 '16 at 02:34
  • Check out the answer below as well for using selectors – Justin Mitchell Dec 13 '16 at 02:35
  • glad that i can help :) – Charuක Dec 13 '16 at 02:36
  • i'm very embraced to ask but in that [answer](http://stackoverflow.com/a/14024279/7105612) he mention to place a xml selector for changing text color in the res/color/ directory but i dont have such directory in my project i have only a res/colors.xml but that is a file not a directory !! – Osdon Dec 13 '16 at 02:43
  • yes use it added that on my answer ! his xml name might be directory you might have clour.xml it doesnt matter its only a name. get the logic and use it . its there only to define colors.name can be different but you can lint to the one what you have thats the point – Charuක Dec 13 '16 at 02:46
  • I did it Thanks ! – Osdon Dec 13 '16 at 02:49
  • if you find an answer is useful you can mark it as correct! up-vote or down-vote based on your requirement – Charuක Dec 13 '16 at 02:53

2 Answers2

1

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.
  1. link the selector xml to button 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>
Charuක
  • 12,953
  • 5
  • 50
  • 88
0

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>
Justin Mitchell
  • 665
  • 6
  • 13