---
title: "Android resource selectors"  
description: "In this article I am going to discuss about use of Android resource selectors."  
author: "Manoj Pandey"  
published: 2015-02-24  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1604/android-resource-selectors  
category: "android"  
tags: ["android", "style", "android styles", "android controls"]  
reading_time: 3 minutes  

---

# Android resource selectors

Once you provide a [resource](https://www.mindstick.com/blog/43433/top-best-resources-for-women-about-beauty-and-health) in [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding), you can apply it by referencing its resource ID. All resource IDs are defined in [your project](https://www.mindstick.com/forum/156583/best-way-to-link-bootstrap-file-in-your-project)'s R class, which the aapt tool automatically generates.\

When you compiled your application then R class generated automatically, which contain resource Ids for all the resource in your res directory.

[Android](https://www.mindstick.com/articles/1567/navigation-drawer-implementation-in-android) provides many [resources](https://www.mindstick.com/interview/1265/what-are-the-different-types-of-resources-through-which-cold-fusion-can-communicate) directories like as layout, menu, values, drawable, anim etc.

If you are adding image or icon in res folder then you have need to better pixels choice to add in folder images. Here I am giving format pixels of image.

**[Density](https://yourviews.mindstick.com/view/81694/rising-fire-damage-fear-level-thanks-to-rising-residential-density) [selector](https://www.mindstick.com/forum/504/what-is-id-selector)** **Equal**

ldpi 160 dpi x 0.75

mdpi 160 dpi

hdip 1.5 x 160 dpi = 240 dpi

xhdpi 2 x 160 dpi = 320 dpi

xxhdpi 3 x 160 dpi = 480 dpi

xxxhdpi 4 x 160 dpi = 640 dpi

##### Icon size in Android

## Icons mdpi hdpi xhdpi xxhdpi xxxhdpi

Launcher icon 48 px 72 px 96 px 144 px 192 px

Action bar icon 32 px 48 px 64 px 96 px 128 px

[Notification](https://www.mindstick.com/blog/205/property-notification-in-c-sharp) icon 24 px 36 px 48 px 72 px

96 px

##### \

##### Here I am creating a sample of selector for button in resource file

##### \
1. Create an android project and minimum sdk must be greater than 10.

##### \
2. Add xml.file in with Resource type selector res/drawable-hdpi folder, name as

##### button_style.xml

##### \

##### ![Android resource selectors](https://www.mindstick.com/mindstickarticle/f6d45ae2-f5cb-4f81-98d4-6d799bdbe1e6/images/c77a239f-b850-4af0-82dd-d57bdfa16742.png)

##### \

##### And add following code

```
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_pressed="true"><shape android:shape="oval">         <corners android:radius="3dip" />          <stroke android:width="1dip" android:color="#2c2f34" />           <gradient android:angle="-90" android:endColor="#363c45"             android:startColor="#090b0e" />       </shape></item>   <item android:state_focused="true"><shape android:shape="rectangle">          <corners android:radius="3dip" />            <stroke android:width="1dip" android:color="#2c2f34" />            <solid android:color="#1d242c" />        </shape></item>         <item><shape android:shape="rectangle">            <corners android:radius="3dip" />             <stroke android:width="1dip" android:color="#2c2f34" />              <gradient android:angle="-90" android:endColor="#1d242c"               android:startColor="#545c67" />        </shape></item> </selector>
```

3. Now add style in your views

```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_marginTop="30sp"    tools:context=".MainActivity" >     <Button        android:id="@+id/mybtn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="3sp"        android:layout_weight=".25"        android:background="@drawable/buttonstyle"        android:text="Text1"        android:textColor="#ffffff" />     <Button        android:id="@+id/mybtn2"        style="@android:attr/borderlessButtonStyle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="3sp"        android:layout_weight=".25"        android:background="@drawable/buttonstyle"        android:text="Text2"        android:textColor="#ffffff" />     <Button        android:id="@+id/mybtn3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="3sp"        android:layout_weight=".25"        android:background="@drawable/buttonstyle"        android:text="Text3"        android:textColor="#ffffff" />     <Button        android:id="@+id/mybtn4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginLeft="3sp"        android:layout_marginRight="3sp"        android:layout_weight=".25"        android:background="@drawable/buttonstyle"        android:text="Text4"        android:textColor="#ffffff" /> </LinearLayout>
```

4. Run your application

![Android resource selectors](https://www.mindstick.com/mindstickarticle/f6d45ae2-f5cb-4f81-98d4-6d799bdbe1e6/images/537ab771-1678-41ef-b3ff-7c8da36777db.png) ![Android resource selectors](https://www.mindstick.com/mindstickarticle/f6d45ae2-f5cb-4f81-98d4-6d799bdbe1e6/images/573b2851-5400-410e-bf4a-6b6d962043a3.png)

You can change your button format. Add following code in button_style.xml

```
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_pressed="true"><shape android:shape="rectangle">            <corners android:radius="3dip" />             <stroke android:width="1dip" android:color="#2c2f34" />             <gradient android:angle="-90" android:endColor="#1A76C1"  android:startColor="#1F94F4" />        </shape></item>    <item android:state_focused="true"><shape android:shape="oval">            <corners android:radius="3dip" />             <stroke android:width="1dip" android:color="#2c2f34" />             <solid android:color="#1d242c" />        </shape></item>      <item><shape android:shape="oval">            <corners android:radius="1dip" />             <stroke android:width="1dip" android:color="#545c67" />             <gradient android:angle="-90" android:endColor="#1F94F4" android:startColor="#1A76C1" />        </shape></item> </selector>
```

Run your application

\
![Android resource selectors](https://www.mindstick.com/mindstickarticle/f6d45ae2-f5cb-4f81-98d4-6d799bdbe1e6/images/c0f443cd-5e73-44cb-8d3e-7a97cc561b65.png)

---

Original Source: https://www.mindstick.com/articles/1604/android-resource-selectors

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
