Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing button style in the whole application

I'm trying to change all the TextColor of my buttons in my app to white and also trying to make it bold. But that isn't happening, I'm overwriting the android:Widget.Button I'm developing for Jelly Bean 4.1.2 What am I doing wrong?

Theme definition in manifest

android:theme="@style/spui" >

The theme where I like to

<style name="spui" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/Buttonspui</item>
</style>

The style for the button itself

  <style name="Buttonspui" parent="android:Widget.Button">
      <item name="android:background">@drawable/btn_default_holo_light</item>
      <item name="android:minHeight">48dip</item>
      <item name="android:minWidth">64dip</item>
      <item name="android:textColor">#ffffff</item>
      <item name="android:textStyle">bold</item>
  </style>

The button

 <Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="15dp"
     android:text="@string/edit"
     android:id="@+id/btnEdit"
     style="@style/Buttonspui"/>
like image 272
mXX Avatar asked Jun 19 '13 13:06

mXX


People also ask

How do I change the default button style in Android Studio?

This means that there is a drawable called btn_default set as button background. Now we need to find a file named btn_default. * in one of the drawable folders under android-sdk\platforms\android-15\data\res . This can be either an image (very unlikely) or a xml file like btn_default.

How can I make my android button more attractive?

Using colors (background, text, border) Using custom shapes like circle, rounded corners and more. You can add images to your buttons to customize them. Using drawables to make gradients, dotted borders and more.

Can we change the shape of button in Android Studio?

We can set custom shapes on our button using the xml tag <shape> . These xml files are created in the drawable folder too. shape can be used inside selectors . The shape can be set to rectangle (default), oval , ring , line .


2 Answers

For styling your Button you can use this:

Go to the drawable folder and make an XML (e.g. button.xml) file called "style" which contains the following:

<?xml version="1.0" encoding="utf-8"?> 

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

    <item>
         <shape>
             <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
             <stroke   android:width="1px"          android:color="#000000" />  
                <corners android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="8dp"
                android:topRightRadius="8dp"/>       
              <padding  android:left="10dp"  android:top="10dp" android:right="10dp" android:bottom="10dp" />
         </shape>  
   </item> 

</selector>

This is my code, you can make the necessary changes you want

Now call it in your layout XML (mainactivity.xml) like this

android:background="@drawable/button.xml"

Now to change the font color and style you can use the following which comes as part of the styles.xml in the values folder

<style name="buttonStyle" parent="@android:style/Widget.Button.Small">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">12sp</item>
    <item name="android:textStyle">bold</item>
</style>

Now call this in the layout XML (mainactivity.xml) like this

  style="@style/buttonStyle"

The final code is:

<Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="15dp"
 android:text="@string/edit"
 android:id="@+id/btnEdit"
 android:background="@drawable/button.xml"
 style="@style/buttonStyle"
/>

Hope this helps :)

like image 193
Benil Mathew Avatar answered Sep 18 '22 05:09

Benil Mathew


This will change the default button style for the entire app, include alert dialog button. Adjust the style based on yours.

res/values/styles.xml

<resources>

    <!-- Your base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Other styles -->
        <item name="buttonStyle">@style/MyCustomButton</item>
    </style>

    <style name="MyCustomButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/selector_button</item>
        <item name="android:textColor">@color/buttonText</item>
        <item name="android:textStyle">bold</item>
        <item name="android:paddingLeft">16dp</item>
        <item name="android:paddingRight">16dp</item>
    </style>
</resources>

res/drawable/selector_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_selected" android:state_focused="true"/>
    <item android:drawable="@drawable/button_unselected"/>
</selector>

res/drawable/button_selected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="1000dp"/>

    <solid android:color="@color/buttonSelected"/>
</shape>

res/drawable/button_unselected.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="1000dp"/>

    <solid android:color="@color/buttonUnselected"/>
</shape>
like image 29
HendraWD Avatar answered Sep 18 '22 05:09

HendraWD