Thursday, May 1, 2014

[Android] How to code 'android:background="?android:attr/selectableItemBackground"' programmatically example

You can't (umm.. there is but hard). So there's a work around.

layout\textview_selectable.xml
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   android:background="?android:attr/selectableItemBackground"
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   />  

then in your code:
 TextView tv = (TextView)getLayoutInflater().inflate(R.layout.textview_selectable, null);

1 comment:

  1. TypedValue outValue = new TypedValue();
    getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
    outValue, true);
    tv.setBackgroundResource(outValue.resourceId);

    ReplyDelete