Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define button background at one place and used around the whole app?

I would like all the buttons of my android application use the same background image, that's all buttons have attribute:

android:background="@drawable/my_btn_bg"

Is there any way to define this in one place and take effect in the whole application's buttons? (instead of define the background on each button component)

I think define inside theme.xml could be a solution, but I do not know how to do it ? Could some one give me some hints?

like image 708
Leem Avatar asked Jul 07 '11 09:07

Leem


People also ask

How do I make a background button?

To change the background color of the button, use the CSS background-color property and give it a value of a color of your taste. In the . button selector, you use background-color:#0a0a23; to change the background color of the button.

How can change button background drawable in android programmatically?

How to set Background of button in Android programmatically? setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.

How to change Button Design in android?

To change the default Button style of the application we can use the android:buttonStyle attribute in the AppTheme style inside the styles. xml.


2 Answers

For Android styles, you reference the preset attributes of R.attr. Here, you want to to reference android:buttonStyle. You can try this :

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

Also look in this Themes and Styles

like image 66
Shaireen Avatar answered Sep 24 '22 23:09

Shaireen


Define styles.xml with the below:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="buttonStyle">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:background">@drawable/bg_button</item>
    </style>
</resources>

And use the same style for any button within your application as:

<Button 
    android:text="Test Button" 
    android:id="@+id/btnTest" 
    style="@style/buttonStyle">
</Button>
like image 23
Paresh Mayani Avatar answered Sep 22 '22 23:09

Paresh Mayani