Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the alternative for translationZ in lower api level less than 21

Tags:

android

I am using translationZ to put a imageview on top of a relative layout. But it seems android:translationZ="2dp" is not working below api level 21. Whats the alternative for this ?

PFB code :

   RelativeLayout layout_main = (RelativeLayout) findViewById(R.id.layout_main);
  ImageView  myimage= (ImageView) view.findViewById(R.id.myimage);
      Picasso.with(getContext()).load("image url")
                    .error(getContext().getResources().getDrawable(R.mipmap.ic_business_black_48dp))
                    .resize(200,200).centerInside()
                    .placeholder(getContext().getResources().getDrawable(R.mipmap.ic_business_black_48dp)).into(myimage);
                layout_main.bringToFront();


   <RelativeLayout
                android:id="@+id/layout_main"
                android:layout_width="90dip"
                android:layout_height="90dip"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="70dp"
                android:background="#ededeb"
                android:translationZ="3dp"
                android:elevation="20dp">

                <ImageView
                    android:id="@+id/myimage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#00000000"
                    android:src="@mipmap/ic_business_black_48dp" />
            </RelativeLayout>

2 Answers

You can ask to bring it to front (similar to How to bring view on front of everything?), use View.bringToFront(), but this will not render a shadow for you. Earlier OS versions don't have native support for shadows so you would need to work around it if you do desperately need it.

like image 120
milosmns Avatar answered Sep 08 '25 11:09

milosmns


API 21 has view.setElevation(float) build-in

Use ViewCompat.setElevation(view, float); for backward compatibility

More methods ViewCompat.setZ(v, pixels) and ViewCompat.setTranslationZ(v, pixels)

like image 42
ysfcyln Avatar answered Sep 08 '25 09:09

ysfcyln