articles

Home / DeveloperSection / Articles / Android resource selectors

Android resource selectors

Manoj Pandey3559 24-Feb-2015

Once you provide a resource in your application, you can apply it by referencing its resource ID. All resource IDs are defined 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 provides many resources 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 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 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 

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     Android resource selectors

 

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   

 

 

 


Leave Comment

Comments

Liked By