Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom button in Android using XML Styles

I want to make this kind of button [same background & text] colors by using XML Styles

enter image description here

that's just for an example, i want to write some other texts, like: About Me

Still i am using button created by designer in Photoshop

    <ImageButton         android:id="@+id/imageButton5"         android:contentDescription="AboutUs"         android:layout_width="wrap_content"         android:layout_marginTop="8dp"         android:layout_height="wrap_content"         android:layout_below="@+id/view_pager"         android:layout_centerHorizontal="true"         android:background="@drawable/aboutus" /> 

Note: I need this kind of button in every size and shape

I don't want to use any image in my Android App i want to make it using XML only

like image 703
Sneha Avatar asked Aug 29 '13 09:08

Sneha


People also ask

How do I create a button style in Android?

To add a button, that has an Android style all you need to do is to drag and drop a button from the Palette to your layout. For most versions that would mean a grey button with all corners at 2dp roundness. Check our blog, if you need to learn more about using Android Studio Layout Editor.

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.

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

Copy-pasted from a recipe written by "Adrián Santalla" on androidcookbook.com: https://www.androidcookbook.com/Recipe.seam?recipeId=3307

1. Create an XML file that represents the button states

Create an xml into drawable called 'button.xml' to name 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

Create one xml file for each of the four button states. All of them should be under drawables folder. Let's follow the names set in the button.xml file.

button_enabled.xml:

<?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> 

button_focused.xml:

<?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> 

button_pressed.xml:

<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> 

button_disabled.xml:

<?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

Once you have created the files mentioned above, it's time to create your application button style. Now, you need to create a new XML file, called styles.xml (if you don't have it yet) where you can include more custom styles, into de values directory.

This file will contain the new button style of your application. You need to set your new button style features in it. Note that one of those features, the background of your new style, should be set with a reference to the button (button.xml) drawable that was created in the first step. To refer to the new button style we use the name attribute.

The example below show the content of the styles.xml file:

<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> </resources> 

4. Create an XML with your own custom application theme

Finally, you need to override the default Android button style. For that, you need to create a new XML file, called themes.xml (if you don't have it yet), into the values directory and override the default Android button style.

The example below show the content of the themes.xml:

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

Hope you guys can have the same luck as I had with this, when I was looking for custom buttons. Enjoy.

like image 184
Gramowski Avatar answered Sep 21 '22 13:09

Gramowski


Have you ever tried to create the background shape for any buttons?

Check this out below:

Below is the separated image from your image of a button.

enter image description here

Now, put that in your ImageButton for android:src "source" like so:

android:src="@drawable/twitter" 

Now, just create shape of the ImageButton to have a black shader background.

android:background="@drawable/button_shape" 

and the button_shape is the xml file in drawable resource:

    <?xml version="1.0" encoding="UTF-8"?> <shape      xmlns:android="http://schemas.android.com/apk/res/android">     <stroke          android:width="1dp"          android:color="#505050"/>     <corners          android:radius="7dp" />      <padding          android:left="1dp"         android:right="1dp"         android:top="1dp"         android:bottom="1dp"/>      <solid android:color="#505050"/>  </shape> 

Just try to implement it with this. You might need to change the color value as per your requirement.

Let me know if it doesn't work.

like image 37
Shreyash Mahajan Avatar answered Sep 23 '22 13:09

Shreyash Mahajan