11

I want to use the setForeground method to show a "play" icon in the center of my ImageView to indicate the user that a video will play if they press it.

Currently I'm having this error which I cannot solve:

enter image description here

Although the documentation says the method should be available since API 1:

enter image description here

I'm targeting and compiling against API 23 with build tools version 23.0.1. I'm targeting min API 16.

Saragis
  • 1,782
  • 6
  • 21
  • 30
  • This question already has answers here: [Why doesn't `android:foreground` attribute work?](https://stackoverflow.com/a/52411921/7594961) – Bertram Gilfoyle Dec 27 '19 at 12:15

3 Answers3

35

That is a documentation bug. setForeground() existed on FrameLayout from API Level 1; it is only on View as of API Level 23.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Alright thanks, do you have any suggestion on how to get the play icon then in my case? I'm currently thinking manually merging the regular image and the foreground image might be my only choice, but maybe I'm missing something. – Saragis Oct 18 '15 at 18:40
  • 3
    @Saragis: Off the cuff, have the "regular image" be the background image of the `ImageView`, and have the play icon be the actual image for the `ImageView` – CommonsWare Oct 18 '15 at 18:43
  • 1
    this answer little not correct. java.lang.NoSuchMethodError: android.widget.ImageView.setForeground at kitkat(19). – 최봉재 Feb 20 '18 at 06:41
  • 1
    @bongjaechoe: As my answer states, `setForeground()` was added to `View` on API Level 23. You are attempting to use it on API Level 19. API Level 19 came out years before API Level 23, and so `setForeground()` was not available on `View` at the time. – CommonsWare Feb 20 '18 at 11:51
  • If you wrap the view inside a `FrameLayout` and use `setForground` to the layout, the warning will disappear! – blueware Feb 27 '19 at 12:07
6

Since the setForeground method was added for the FrameLayout in API Level 1, as a workaround you can wrap your view inside a FrameLayout then use the setForeground method to the layout, it will work, eg:

in your xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fl_item_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/niImageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/imageView_description"
        android:scaleType="fitCenter"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</FrameLayout>

Then in code, use:

holder.flItemContainer.setForeground(ContextCompat.getDrawable(a, R.drawable.play));
blueware
  • 5,205
  • 1
  • 40
  • 60
-2

You can either change

minSdkVersion 16

with

minSdkVersion 23

or import android.support.annotation.RequiresApi;
to your class and this statement

@RequiresApi(api = Build.VERSION_CODES.M)

to the activity you used setForground method in it.

Note that M in @RequiresApi(api = Build.VERSION_CODES.M) stands for API 23

and you can use each one of the items below that each one stand for a specific API instead of M

BASE 1
BASE_1_1 2
CUPCAKE 3
DONUT 4
ECLAIR 5
ECLAIR_0_1 6
ECLAIR_MR1 7
FROYO 8
GINGERBREAD 9
GINGERBREAD_MR1 10
HONEYCOMB 11
HONEYCOMB_MR1 12
HONEYCOMB_MR2 13 ICE_CREAM_SANDWICH 14
ICE_CREAM_SANDWICH_MR1 15
JELLY_BEAN 16
JELLY_BEAN_MR1 17
JELLY_BEAN_MR2 18
KITKAT 19
KITKAT_WATCH 20
LOLLIPOP 21 LOLLIPOP_MR1 22
M 23
N 24
N_MR1 25
O 26
CUR_DEVELOPMENT 10000

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
Reza
  • 845
  • 13
  • 18