How to make a background button selector in XML?
- Home
- chevron_right
- Snippets
- chevron_right
- How to make a background button selector in XML?
Step 1: Create an XML in the drawable folder and name it selected_item.xml (or any another name you want but to be suggestive). The code in this xml will be:
<shape android="http://schemas.android.com/apk/res/android" shape="rectangle"> <solid color="#80000000"></solid> </shape>
The color=”#80000000″ is transparent black.
Step 2: Create an XML again, in the drawable folder an name it not_selected.xml. The code in this xml will be:
<shape android="http://schemas.android.com/apk/res/android" shape="rectangle"> <solid color="#80ffffff"></solid> </shape>
The color=”#80ffffff” is transparent white.
Step 3 Create an XML again 🙂 in the drawable folder and name it button_selector.xml. The code in this xml will be:
<selector android="http://schemas.android.com/apk/res/android"> <item drawable="@drawable/selected" state_focused="false" state_pressed="true"></item> <item drawable="@drawable/not_selected"> </item> </selector>
Step 4 In the xml file where you make the button put this:
android:background="@drawable/button_selector"
Related
Search In Blog
Want to donate?
Categories
Recent Posts: My Android Solutions
Android Parse JSON file from Assets
If you ever wondered how to read and parse a simple JSON file stored in the assets directory of your Android project, here is a way to do it.
Android Simple Section RecylerView
In this tutorial I will create a simple Sectioned RecyclerView that will show 2 sections and a few items in each section and how to move an item from one section to another. Note: For RecyclerView I used AndroidX imports and not android.support.v7.widget.RecyclerView! Setup We need to import the recycler view from AndroidX. The build.gradle…
Android Squared CardView
Recently, I had to add a perfect square CardView and I wanted to achieve this without hardcoding its width and height. I happily discovered the power of ConstraintLayout and this is how I managed to create a perfect square which should work on every dimension. In my project I created a RecyclerView with 2 columns,…