Mohamed Hafez
Mohamed Hafez

Reputation: 8691

using standard Android button but with modifications

I'm trying to use the the standard android button in my layout, but make the background transparent when not pressed, or making text left or right justified, or whatever custom modification I want. Is there some simple way I'm missing to inherit from the standard button but change a few properties? I've looked at the two posts below, and can't get them to work and am too new to leave comments on those pages, plus both of those solutions have problems anyway:

I've tried copying @android:drawable/btn_default source per How to disable the default Button color changing property on onClick, but all of the resources linked to from there are private. I tried to find the source for those private files, but some of them i can't find even if i go into the android sdk folder to get the raw files. Where can i find these files, if this is the way to edit the standard button? copying those private files is definately not ideal though, if the standard selected/pressed/whatever button changes in another api i'll still be using the old ones in this case and have inconsistent buttons...

Also, I've seen Standard Android Button with a different color which is good for making custom buttons in general, but how do I set it to be exactly like the standard button? i.e. what are the colors, are the gradients right, etc. Again, this has the problem that if standard button changes i'll still be using old values.

Upvotes: 0

Views: 3046

Answers (2)

Mohamed Hafez
Mohamed Hafez

Reputation: 8691

The first part of CSmith's answer about using styles with parent="@android:style/Widget.Button" is the best way to change button properties like left or right justified text. However the rest of the answer describes how to create your own selectors, so I thought I'd add my solution to inherit selectors from the default button, and then just override specific ones you want to change:

just put android:drawable="@android:drawable/btn_default" for those selectors you want default behavior for, and then you can just specify custom fields for only those selectors you want to change. in my case of trying to have a button that inherits all the selectors but has a transparent background normally instead of the gray one, do the following (duplicating the first part of CSmith's answer here for clarity, thanks CSmith!):

Define a button using a custom style:

<Button id= ... style="@style/myButton" />

res/values/styles.xml:

<style name="myButton" parent="@android:style/Widget.Button">
    <item name="android:background">@drawable/myBackground</item>
    ...other changes to default button... 
</style>

then, to inherit all selectors and just overwrite the ones that show gray: in res/drawable/myBackground.xml:

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" 
          android:drawable="@drawable/transparent_btn" />    
    <item android:state_pressed="true"
          android:drawable="@android:drawable/btn_default" />
    <item android:state_focused="true"
          android:drawable="@android:drawable/btn_default" />
    <item android:drawable="@drawable/transparent_btn" />
</selector>

and, in res/drawable/transparent_btn.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="@android:color/transparent" />
</shape>

NOTE: this example doesnt worry about state_enabled, if you want specific behavior for whether a button is enabled or not just overwrite those selectors as well.

Upvotes: 0

CSmith
CSmith

Reputation: 13458

Use styles on your buttons:

<Button id= ... style="@style/myButton" />

values/styles.xml:

<style name="myButton" parent="@android:style/Widget.Button">
  <item name="android:background">@drawable/myBackground</item> 
</style>

To deal with various button states (e.g. pressed, etc) you'll need a selector drawable resource, example drawable/myBackground.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <!--  Non focused states 
  --> 
  <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
  <item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_unfocused" /> 
 <!--  Focused states 
  --> 
  <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
  <item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/button_focus" /> 
 <!--  Pressed 
  --> 
  <item android:state_pressed="true" android:drawable="@drawable/button_press" /> 
</selector>

The drawable/button_press.xml could specify a gradient, shape, borders, etc, as you need.

An example button_press.xml that does a background gradient, rounded corners, and border:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  <stroke android:width="1dp" android:color="#FF404040" /> 
  <corners android:radius="6dp" /> 
  <gradient android:startColor="#FF9B00" android:centerColor="#FFB300" android:endColor="#FFCA00" android:angle="90" /> 
</shape>

Upvotes: 2

Related Questions