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.
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
Step 3: Create res -> drawable -> color_selector.xml and add following code
Step 4: Create res -> drawable -> background_selector.xml and add following code
Step 5: Create res -> drawable -> button_style.xml and add following code
Step 6: Add below given code to your MainActivity.java
Step 7: Clean and run the project to see the result.