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);

[Android] Set style programatically example

You can't. So there's a work around.

layout\textview_mystyle.xml
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
   style="@style/myStyleText"  
   android:layout_width="match_parent"  
   android:layout_height="wrap_content"  
   />  

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