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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With