I have a problem with a TableRow, which I add dynamically.
private void addRow(String body) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
TableRow row = (TableRow) inflater.inflate(R.layout.customrow,null);
TextView name = (TextView) row.findViewById(R.id.customName);
name.setText(body);
row.setOnLongClickListener(this);
}
I would like this row to change color upon onClick
and onLongClick
.
Code in the customrow.xml
file is:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableRow1"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:onClick="showOnClick">
<TextView android:id="@+id/customName"
android:textSize="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_weight="5">
</TextView>
</TableRow>
I have tried to use android:background="@drawable/clickedbackground"
with the row but it is not working.
Code in the clickedbackground.xml
file is:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@color/custom" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="@color/custom" />
</selector>
Anyone knows what I am doing wrong (color/custom is defined in another xml which works)?
Thank you
You are creating object for
tablerow
named row. and you have alsoclickedbackground.xml
file. just use below code inaddRow
method.row.setBackgroundResource(R.drawable.clickedbackground);
I think it solves your problem.
do not forget to add to the style
Otherwise, you will not be able to use the states of a rowLayout.
In your
addRow()
method you're inflating the row but you're not adding it to any parent layout, and asrow
is a local variable I think you're not doing it anywhere else, is it a copy/paste problem?Again, your
customrow.xml
might be not working because the openingTableRow
tag lacks the closing>
, but it might be copy/paste problem.Using
android:background="@drawable/bg"
withbg
being a selector is a common pattern and it should work. You might want to simplify your selector: you don't need to specify all the states for each item and all the combinations. It works with a "first match", so this will do the job:Also, notice that selected and focused are two different states, focused being the one you get when moving around with dpad.
If this didn't help please specify what "is not working" means: what do you expect? what's happening instead?
Adding
in styles.xml and setting
in the TableRow did the job.
where the rows.xml in the drawable is