Android button style


Android selector for button style sample

In this tutorial, we will learn how we can use android selector to style color and background of a View on its different state like focus, clicked etc.

In this example, we have used 3 selector, 2 selector to change background on different state of view and 1 selector to change color on different state of view.

Here is result of these 3 selector. In first button, I have used color selector, for second and third button we've used 2 different background selector. Fourth view is an EditText, which uses color selector and fourth view has no selector.

Android selector

Example of Android Selector

Step 1: Create a new project in Android Studio and name it Android Selector.

Step 2: Open res -> layout -> activity_main.xml and add following code

activity_main.xml

Step 3: Create res -> drawable -> color_selector.xml and add following code

color_selector.xml

Step 4: Create res -> drawable -> background_selector.xml and add following code

background_selector.xml

Step 5: Create res -> drawable -> button_style.xml and add following code

button_style.xml

Step 6: Add below given code to your MainActivity.java

MainActivity.java

Step 7: Clean and run the project to see the result.


Share & Like

Tutorial topics

About ApnaTutorials.com

ApnaTutorials.com is for Java, J2ee, Html5 and android developers. All tutorials are well explained and are easy to understand. We have tested every tutorials on our developement environment before posting it in public domain.