0

In my XML, I have a TableLayout with only 1 TableRow i.e. the heading. Other all rows I add dynamically setting BackgroundColor (LTGray) for TableRow & TextColor for TextViews in it . I also handle click event on each row.

private void createView(TableRow tr, TextView tv, String data, int rowId) {
    tv.setText(data);

    //tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    tv.setTextColor(Color.BLACK);
    tv.setPadding(20, 0, 0, 0);

    tr.setPadding(0, 1, 0, 1);
    tr.setBackgroundColor(Color.LTGRAY);
    tr.setId(rowId);
    tr.setClickable(true);
    tr.setOnClickListener(this);
    tr.addView(tv);
}

Reg selection : I want to change the BackgroundColor of TableRow lets say Yellow. So if 1st row is selected it bgColor should be Yellow. Then if 3rd row is selected the 1st row's color should turn to LTGray.

And if anywhere out of the Rows is clicked, then the selected Row (if at all) should also be de-selected. For this do I have to add the main layout clickListener OR make the row select again and it turns deselected ?

Can selector (state list drawable) work for both the ways or I got to handle it programmatically. What sort of Drawable should I use to setBackgroundDrawable in my Java Code to se the statelist drawable ?

I believe like other components for TableRow also onClick will also take care of onTouch. Please correct me if am wrong. As want to handle the same feature with touching the row also.

What is the best way to achieve the goal ? Any help is highly appreciated.

Tvd
  • 4,463
  • 18
  • 79
  • 125

3 Answers3

0

Do not change that in code! Use selector instead.

Taken from here:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--  Active state --> 
    <item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />     
<!--  Inactive state--> 
    <item android:state_selected="false" android:state_focused="false"         android:state_pressed="false" android:drawable="@android:color/transparent" />
     <!--  Pressed state-->
     <item android:state_pressed="true" android:drawable="@android:color/yellow" /> 
    <!--  Selected state (using d-pad) -->
     <item android:state_focused="true" android:state_selected="true"         android:state_pressed="false" android:drawable="@android:color/yellow" />
 </selector> 

Not so thorough, but more useful answer is here

changing the selector dynamically here

Community
  • 1
  • 1
Gangnus
  • 24,044
  • 16
  • 90
  • 149
  • @Gangus, but how do I set the selector in my java code dynamically. I mean I got to set it a method calling "R.....". I can't ge which method to use to set it for my TableRow. – Tvd Jan 19 '12 at 09:01
  • The reference added to the answer. – Gangnus Jan 19 '12 at 09:56
  • @Gangus, I used the above xml code and added setBackgroundDrawable() to my TableRow. RESULT : When I click on it, it just blinks yellow color and then comes to normal & then the onclick event is caught where the state of the row is : ROW CLICKED CAUGHT - Selected: false Pressed: true Focused: false Why the yellow color doesn't retain ? – Tvd Jan 19 '12 at 11:14
  • Sorry, comrad. I have found a solution that will surely save you tons of code. - work with selector is easier and possible. And put proofs and examples here. How to make selector work in your code? Don't you think it is already **another** question? Make another question and give me here the reference, please. – Gangnus Jan 19 '12 at 12:06
  • the link for how to make it selector work Q - http://stackoverflow.com/questions/8927358/android-selector-isnt-working-as-expected-in-tablerow-selection – Tvd Jan 19 '12 at 14:03
0

Ok, than use OnFocusChangeListener. It catches obtaining and losing the focus.

onFocusChange(View v, boolean hasFocus) 

Called when the focus state of a view has changed.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
0

Thanks Friends,

I managed it handling in the code itself. Added click listener to each row addd and handle the colors of selected and non-selected row accordingly.

Tvd
  • 4,463
  • 18
  • 79
  • 125