Create style for android widget and include

As you guys are android developer you need to make your code beautiful as well as less code. Here is a basic tip for you, here we are creating a style in res->values->styles.xml for buttons and include it in our activity xml page.

So lets create our style for Button widget paste your code in your style.xml file.

1
2
3
4
5
6
7
8
9
    <style name="Widget.Button.info" parent="android:Widget">
        <item name="android:background">@drawable/btn_bg_info</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:paddingLeft">20dp</item>
        <item name="android:paddingRight">20dp</item>
        <item name="android:foreground">?selectableItemBackground</item>
        <item name="android:clickable">true</item>
        <item name="android:textAllCaps">true</item>
    </style>

Above code will create a style like in css and you can add a style and include anywhere by CLASS or by ID similarly here Widget.Button.info is your style name it can be included in any button's style.

To Include above style in your button look out the below code snippet.



1
2
3
4
5
6
7
8
9
<Button
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	android:gravity="center"
	android:text="Info Button"
	android:textAllCaps="false"
	
	style="@style/Widget.Button.info"
/>

last line in above code snippet that is style="@style/Widget.Button.info" is including the style which we have created in our styles.xml file.

that's it, by using this much enough code you can make your button beautiful and include this style in any button by just single line of code.

Thanks,
Happy Coding!

Comments