Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing Linear layout background color while clicking

Tags:

android

I am having a linear layout in my xml and am adding another linear layout(containing two textviews) to that linear layout through java. Touch events works perfect, but i want to highlight the selected linear layout by setting background color. Please advice.

like image 521
vignesh Avatar asked Apr 12 '13 05:04

vignesh


1 Answers

Here is my solution, it's very simple:

<LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_pressed_tab"
            android:clickable="true"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="@string/app_name"
                android:src="@mipmap/ic_launcher" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/service_request"
                android:textColor="@color/white"
                android:textSize="@dimen/text_size_small" />

            <requestFocus />
        </LinearLayout>

bg_tab_pressed.xml

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

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="true" />

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="false" android:state_pressed="true" />

    <item android:drawable="@color/colorPrimaryDark" android:state_focused="true" android:state_pressed="false" />

    <item android:drawable="@color/colorPrimary" android:state_focused="false" android:state_pressed="false" />

</selector>
like image 147
Na Pro Avatar answered Oct 29 '22 22:10

Na Pro