0

I want to change the style of the Button. And I add the style in style.xml then set the @style/BaseButton to every Button.
Is there any way can do it better? Can I just set the theme to change all Button or other views?
I know that I can add the theme to the style.xml but unfortunately, if I set the attributes like @android:background to the Button, other views' backgrounds are set too.

So here are what I need:

  • Set the style in only one(or some) place(s). No need to set style in every layout and every <Button />.
  • Only the Button is influenced while other views stay where they are.
  • Can be changed easily.
blackdog
  • 2,007
  • 3
  • 25
  • 43

2 Answers2

3

Follow below steps

  1. Create an XML file that represents the button states

     <?xml version="1.0" encoding="utf-8"?>
    
     <selector xmlns:android="http://schemas.android.com/apk/res/android">
     <item
     android:state_enabled="false"
     android:drawable="@drawable/button_disabled" />
    <item
    android:state_pressed="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_pressed" />
    <item
    android:state_focused="true"
    android:state_enabled="true"
    android:drawable="@drawable/button_focused" />
    <item
    android:state_enabled="true"
    android:drawable="@drawable/button_enabled" />
    </selector>
    
  2. Create an XML file that represents each button state

    First button shape is for the enabled button state.

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
    <gradient
    android:startColor="#00CCFF"
    android:centerColor="#0000CC"
    android:endColor="#00CCFF"
    android:angle="90"/>
    <padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
    <stroke
    android:width="2dip"
    android:color="#FFFFFF" />
    <corners android:radius= "8dp" />
    </shape>
    

    Second button shape is for the focused button state.

    <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
    android:startColor="#F7D358"
    android:centerColor="#DF7401"
    android:endColor="#F7D358"
    android:angle="90"/>
    <padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
    <stroke
    android:width="2dip"
    android:color="#FFFFFF" />
    <corners android:radius= "8dp" />
    </shape>
    

    Third button shape is for the pressed button state.

        <?xml version="1.0" encoding="utf-8"?>
    
      <shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">
     <gradient
      android:startColor="#0000CC"
      android:centerColor="#00CCFF"
      android:endColor="#0000CC"
      android:angle="90"/>
      <padding android:left="7dp"
      android:top="7dp"
      android:right="7dp"
      android:bottom="7dp" />
      <stroke
      android:width="2dip"
      android:color="#FFFFFF" />
      <corners android:radius= "8dp" />
      </shape>
    

    And finally, Fourth button shape is for the disabled button state.

     <?xml version="1.0" encoding="utf-8"?>
    
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient
    android:startColor="#F2F2F2"
    android:centerColor="#A4A4A4"
    android:endColor="#F2F2F2"
    android:angle="90"/>
    <padding android:left="7dp"
    android:top="7dp"
    android:right="7dp"
    android:bottom="7dp" />
    <stroke
    android:width="2dip"
    android:color="#FFFFFF" />
    <corners android:radius= "8dp" />
    </shape>
    
  3. Create an XML file that represents the button style

    <resources>
    <style name="button" parent="@android:style/Widget.Button">
    <item name="android:gravity">center_vertical|center_horizontal</item>
    <item name="android:textColor">#FFFFFFFF</item>
    <item name="android:shadowColor">#FF000000</item>
    <item name="android:shadowDx">0</item>
    <item name="android:shadowDy">-1</item>
    <item name="android:shadowRadius">0.2</item>
    <item name="android:textSize">16dip</item>
    <item name="android:textStyle">bold</item>
    <item name="android:background">@drawable/button</item>
    <item name="android:focusable">true</item>
    <item name="android:clickable">true</item>
    </style>
    

  4. Create an XML with your own custom application theme .themes.xml below

    <resources>
    <style name="YourApplicationTheme" parent="android:style/Theme.NoTitleBar">
    <item name="android:buttonStyle">@style/button</item>
    </style>
    </resources>
    

Now, you can create buttons on your application with the new style

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
2

Use this

<style name="ApplicationStyle" parent="android:Theme">
  <item name="android:buttonStyle">@style/CKButton</item>
</style>

Linked from: How do I apply a style to all buttons of an Android application

Community
  • 1
  • 1
mTrebusak
  • 66
  • 4
  • later on add some explanation also. Because your answer will be invalid if the link is broken. right? however up voted.good luck @Busi_rusi – Zahan Safallwa Oct 20 '15 at 08:27