AndroidKrayze
AndroidKrayze

Reputation: 359

Create a " Pressed State" for a relative layout

I have build this Dashboard(See image Attached) enter image description here

What I am trying to do is create a "Press State" for the buttons.A colour change would work fine for me.

The dashboard buttons are made of 5 Relative layouts where each one holds two image views and two textviews.

The code is listed below just for one button in this case "Mobile Apps" :

<RelativeLayout
            android:id="@+id/appsHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/gradient_background"
            android:padding="5dp" >

            <!-- ListRow Left sied Thumbnail image -->

            <LinearLayout
                android:id="@+id/thumbnail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_marginRight="5dip"
                android:padding="3dip" >

                <ImageView
                    android:id="@+id/list_image"
                    android:layout_width="150dip"
                    android:layout_height="150dip"
                    android:src="@drawable/ic_apps_dashboard" />
            </LinearLayout>

            <!-- Title Of Song -->

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/thumbnail"
                android:layout_toRightOf="@+id/thumbnail"
                android:text="Mobile Apps"
                android:textColor="#000000"
                android:textSize="30dip"
                android:textStyle="bold"
                android:typeface="sans" />

            <!-- Artist Name -->

            <TextView
                android:id="@+id/subtitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/title"
                android:layout_marginTop="1dip"
                android:layout_toRightOf="@+id/thumbnail"
                android:text="Preview the Viessmann apps..."
                android:textColor="#000000"
                android:textSize="30dip" />

            <!-- Rightend Duration -->


            <!-- Rightend Arrow -->

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_arrow_dashboard" />
        </RelativeLayout>

ANY GUIDANCE WILL BE APPRECIATED!

Upvotes: 1

Views: 5360

Answers (1)

sockeqwe
sockeqwe

Reputation: 15929

You have to write your own selector:

http://developer.android.com/guide/topics/resources/color-list-resource.html

Create a xml file in your res/drawable/ folder and apply it as background to your button:

my_background_selector.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true" android:drawable="@drawable/background_gradient_pressed" />
   <item android:state_focused="true" android:drawable="@drawable/background_gradient_pressed" />
   <item android:state_selected="true" android:drawable="@drawable/background_gradient_pressed" />


   <item android:drawable="@drawable/background_gradient" />

 </selector>

Then simply apply the selector as background on your layout

<RelativeLayout
            android:id="@+id/appsHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:background="@drawable/my_background_selector"
            android:padding="5dp" 
    ...
</RelativeLayout>

If you want to work with colors, you can also do something like this:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:state_pressed="true">
       <shape>
         <solid android:color="@color/pressed_color" />
       </shape>
   </item>


   <item>
     <shape>
        <solid android:color="#B8B8B8" />
     </shape>
   </item>

 </selector>

Upvotes: 4

Related Questions