void setZ(View view, final float zindex) { if (Build.VERSION.SDK_INT < 21) return; try { Method setZ = view.getClass().getMethod("setZ", float.class); setZ.invoke(view, zindex); } catch (Exception ex) { Log.i(LOG_TAG, "setZ: " + ex.toString()); } }Or you can use ViewCompat:
ViewCompat.setZ(view, zindex);