Take a look at this small android app:
MainActivity.java:
package io.github.gsaga.toucheventtest;
import android.support.v7.app.AppCompatActivity;
i
It looks like at one time (API <23) that android:foreground would work with only FrameLayout as VicJordan suggests. However, for API 23+ it appears that android:foreground will work for any view type. See this selection from the View.java source:
case R.styleable.View_foreground:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
setForeground(a.getDrawable(attr));
}
Here is an example of android:foreground working on API 28 with the following layout:
Yet, on API 22, we see this:
No foreground image. So, android:foreground works only when the API level is 23+. I agree that this is not really documented, but that is just the way it is.
Update: API 23 seems to have an issue with android:foreground, so let's say android:foreground works on API 24+ for general views.
Second update: Came across a couple of other posts addressing this same issue regarding setForeground() here and here. In the accepted answer to the second question, CommonsWare identifies this as a "documentation bug."