-1

I created a fun application. I created a ImageView which is always above all like an Floating Action Button. The problem ist: everything works fine at Android 5.0 or higher and it looks like this: https://gyazo.com/0f02bbe0130defd03cc5ce6282d8e321

And on every device with Android 4 it looks like this: https://gyazo.com/9275e291f215d92d5e7f2b8740a3d749

This is the code for the ImageView:

   <ImageView
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/infoicon"
        android:adjustViewBounds="true"
        android:maxWidth="25dp"
        android:maxHeight="25dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:elevation="10dp"/>

this line should elevate the ImageView:

android:elevation="10dp"

Why is it working on Android version 5 or higher but on Android version 4 not?

TheUseracc awd
  • 444
  • 2
  • 5
  • 16
  • try to put your imageview with framelayout – Ichigo Kurosaki Apr 05 '17 at 12:46
  • See this link http://stackoverflow.com/questions/27693843/androidelevation-doesnt-work-on-devices-pre-lollipop-with-compile-api21 – debo.stackoverflow Apr 05 '17 at 12:50
  • Elevation property started with API 21, older API versions can't execute that layout command. – Marcelo Ferracin Apr 05 '17 at 12:50
  • @MarceloFerracin is there no way to do it with API 20 or older? – TheUseracc awd Apr 05 '17 at 15:49
  • @debo.stackoverflow this is an explanation on how to ad shadows to an button. But not helpful in my case – TheUseracc awd Apr 05 '17 at 15:50
  • If your root element is a RelativeLayout, the XML elements order matters. So, in theory, if you put your ImageView in last, it should be with the higher Z-axis. – Marcelo Ferracin Apr 05 '17 at 15:53
  • Oh, I think I forgott to say that I have a Fragment and a MainActivity. So the ImageView is in my MainActivity and the buttons which are in Android version 4 above the ImageView are in the Fragment. So I want that the ImageView of the MainActivity is above the Buttons in the Fragment. – TheUseracc awd Apr 05 '17 at 16:07
  • Ok in this case you have to create a `` on your activity, before your ImageView, and you have to call your fragment with: `getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout_id, yourFragment, TAG).commit();` – Marcelo Ferracin Apr 05 '17 at 16:13
  • This is how I call my Fragment:https://pastebin.com/VMeqXc5u and this is my ImageView in my FrameLayout: https://pastebin.com/PYMp90A0 still not working – TheUseracc awd Apr 05 '17 at 16:34
  • The ImageView is inside your FrameLayout. It should belongs to the same parent of it. – Marcelo Ferracin Apr 05 '17 at 17:54

2 Answers2

0

Try calling this:

imageView.bringToFront();  

in onCreate() and onResume()

EDIT

try wrapping your imageview inside Framelayout

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"
        android:elevation="10dp"
        android:maxHeight="25dp"
        android:maxWidth="25dp"
        android:src="@drawable/infoicon" />
</FrameLayout>
rafsanahmad007
  • 23,062
  • 6
  • 43
  • 58
  • It doesn't work if I write imageView.bringToFront(); in my onCreate() it still looks like this https://gyazo.com/9275e291f215d92d5e7f2b8740a3d749 – TheUseracc awd Apr 05 '17 at 15:37
0

android:elevation was added in API level 21, therefore it will work only on Android devices having API level 21 or more.

If you need to provide elevation for devices below API level 21, please refer How to implement the Material-design Elevation for Pre-lollipop

Community
  • 1
  • 1
Suraj Makhija
  • 1,368
  • 6
  • 16