How to change the background color of the focal view

I want to change the color of the presentation in focus; in my case, this is a row in the table.

This is my current code, but it does not work.

r.setOnTouchListener(new OnTouchListener(){ public boolean onTouch(View arg0, MotionEvent arg1) { { { arg0.setBackgroundColor(Color.LTGRAY); r.setFocusableInTouchMode(true); } return false; }}); 
+6
source share
3 answers

Here is an example for you ... http://life-as-a-coder.com/_shared/background_state.rar

Take a look at my main.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dip" android:background="@drawable/item_background" android:clickable="true"> <TextView android:text="Item 1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="35dip"></TextView> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dip" android:background="@drawable/item_background" android:clickable="true"> <TextView android:text="Item 2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="35dip"></TextView> </LinearLayout> </LinearLayout> 

I am using LinearLayout for this example. I set android:clickable="true" to activate the click state ... It can also be used in a ListView ... just use it in the custom line Item ...

Sorry for my bad english grammar n

+1
source

You can use XML drawable,
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

create xml like this

 <?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/button_pressed" /> <!-- pressed --> <item android:state_focused="true" android:drawable="@drawable/button_focused" /> <!-- focused --> <item android:drawable="@drawable/button_normal" /> <!-- default --> </selector> 

and you will need a background image for this

+1
source

@nagesh this link will solve your problem OnClick will change the background color of the contributor

0
source

Source: https://habr.com/ru/post/888615/


All Articles