3

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 Answers2

5

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.

Community
  • 1
  • 1
milosmns
  • 3,595
  • 4
  • 36
  • 48
  • I am trying this code but it seems not working with picasso : 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(company_image); company_image.bringToFront(); –  Oct 27 '16 at 19:59
  • 1
    Can you update your question with the layout then? If you're using older versions, the rule for XML is: the later (lower) the View is in your ViewGroup, the higher it goes Z-wise. – milosmns Oct 27 '16 at 20:05
5

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)

ysfcyln
  • 2,857
  • 6
  • 34
  • 61