How to programmitically set android:background="?android:attr/selectableItemBackground"?

John Sardinha :

How can I do android:background="?android:attr/selectableItemBackground"" programmatically?

I tried mView.setBackgroundResource(android.R.attr.selectableItemBackground); and it didn't work.

kris larson :

You need to resolve the attribute first.

    TypedValue typedValue = new TypedValue();

    // I used getActivity() as if you were calling from a fragment.
    // You just want to call getTheme() on the current activity, however you can get it
    getActivity().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);

    // it's probably a good idea to check if the color wasn't specified as a resource
    if (typedValue.resourceId != 0) {
        mView.setBackgroundResource(typedValue.resourceId);
    } else {
        // this should work whether there was a resource id or not
        mView.setBackgroundColor(typedValue.data);
    }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=454139&siteId=1