I have a situation where I need to set a background on a LinearLayout programatically.
In my layout, I am setting my background using `android:background=\"?android:att
I had the same problem and I fixed it using this piece of code.
The android.R.attr.* are pointers to the in a theme and not to the actual drawable resource defined. You have to use the TypedArray to access the id.
theView = this.inflater.inflate(R.layout.list_row_job_favorited, null);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
TypedArray a = mContext.obtainStyledAttributes(new int[] { android.R.attr.activatedBackgroundIndicator });
int resource = a.getResourceId(0, 0);
//first 0 is the index in the array, second is the default value
a.recycle();
theView.setBackground(mContext.getResources().getDrawable(resource));
}
I used this in my custom list adapter when detects SDK upper and worked fine.