Skip to content Skip to sidebar Skip to footer

Android TableRow Background

I have a problem with a TableRow, which I add dynamically. private void addRow(String body) { LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFL

Solution 1:

You are creating object for tablerow named row. and you have also clickedbackground.xml file. just use below code in addRow method.

row.setBackgroundResource(R.drawable.clickedbackground);

I think it solves your problem.


Solution 2:

In your addRow() method you're inflating the row but you're not adding it to any parent layout, and as row 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 opening TableRow tag lacks the closing >, but it might be copy/paste problem.

Using android:background="@drawable/bg" with bg 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:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">        
    <item android:state_pressed="true" android:drawable="@color/custom" />
    <item android:state_selected="true" android:drawable="@android:color/transparent" />
    <item android:state_focused="true" android:drawable="@color/custom" />
    <item android:drawable="@android:color/transparent" />
</selector>

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?


Solution 3:

Adding

<resources>
 <style name="row" parent="@android:style/Widget.Button">
     <item name="android:background">@drawable/rows</item>
</style>
</resources>

in styles.xml and setting

style="@style/row"

in the TableRow did the job.

where the rows.xml in the drawable is

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
    android:state_enabled="false"
        android:drawable="@color/blue" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@color/custom" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@color/white" />
    <item
        android:state_enabled="true"
        android:drawable="@android:color/transparent" />
</selector>

Solution 4:

do not forget to add to the style

<item name="android:focusable">true</item>
        <item name="android:clickable">true</item>

Otherwise, you will not be able to use the states of a rowLayout.


Post a Comment for "Android TableRow Background"