2

I am trying to set the onClick() highlight for each row in my TableLayout.

I have a drawable resource file:

<item
    android:drawable="?android:attr/selectableItemBackground"
    android:state_focused="true"
    android:state_pressed="true" />

<!-- Bottom border -->
<item android:top="65dp" android:left="15dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <size android:height="0.25dp"/>
        <solid android:color="@color/dark_blue"/>
    </shape>
</item>

I have tested this on a Samsung Galaxy S5 device which works. However, when I test on a ZTE Compel Device, which has Android 4.4.2 installed on it -- it does not work.

My application is targeting API 15 and up.

Any ideas why this: ?android:attr/selectableItemBackground doesn't work on Android 4.4.2 device?

Error in logs:

Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #7: tag requires a 'drawable' attribute or child tag defining a drawable

I have ?attr/selectableItemBackground and it doesn't resolve the issue. I have done a ton of research.

Any other suggestions from your side?

Jiri Tousek
  • 12,211
  • 5
  • 29
  • 43
Sandy D.
  • 3,166
  • 1
  • 20
  • 31

1 Answers1

0

The issue was in my drawable Resource File I was using a layer-list. Drawable attribute in a layer-list (or state-list) does not accept an attr value.

However, I couldn't set this attr value in my layout file because I also needed a bottom border for each table row.

So to fix this, in the Java Class file:

I retrieve a reference to each row in my onCreate() method.

Example: TableRow row1 = (TableRow)findViewById(R.id.table_row_1);

Then I created a method that would handle adding the attribute to the bottom_border drawable file:

`void setRowHighlight(TableRow row) {
    // Attribute array
    int[] attrs = new int[] { android.R.attr.selectableItemBackground };

    TypedArray a = getTheme().obtainStyledAttributes(attrs);

    // Drawable held by attribute 'selectableItemBackground' is at index '0'
    Drawable d = a.getDrawable(0);

    a.recycle();

    if (Build.VERSION.SDK_INT < 16) {
        LayerDrawable ld = new LayerDrawable(new Drawable[] {

                // Table Rows Border Drawable
                getResources().getDrawable(R.drawable.table_rows_border),

                // Drawable from attribute
                d });

        // Set the background to 'ld'
        row.setBackgroundDrawable(ld);
    } else {
        LayerDrawable ld = new LayerDrawable(new Drawable[] {

                // Table Rows Border Drawable
                ResourcesCompat.getDrawable(getResources(), R.drawable.table_rows_border, null),

                // Drawable from attribute
                d });

        // Set the background to 'ld'
        row.setBackground(ld);
    }

}`

Enable the row highlight, call my method in the onCreate(): setRowHighlight(row1);

I have tested this on my Samsung Galaxy S5 device (Android 5.0) and ZTE Compel device (Android 4.4.2) and it works.

Reference: ?android:attr/selectableItemBackground with another existing background

Community
  • 1
  • 1
Sandy D.
  • 3,166
  • 1
  • 20
  • 31