Android examples for User Interface:View Position
set Background While Retaining Padding
//package com.java2s; import android.os.Build; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; import android.view.View; public class Main { /**/*from w w w . j a va 2s. c o m*/ * In older version of android (< 5.0), setting certain background drawable (mainly layer-lists) will cause view's * padding to reset. So we need to explicitly remember and restore those paddings * * https://code.google.com/p/android/issues/detail?id=27235 * * @param view The view whose background will be set * @param backgroundRes The drawable resource id for the background */ public static void setBackgroundWhileRetainingPadding( @NonNull View view, @DrawableRes int backgroundRes) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { int paddingTop = view.getPaddingTop(); int paddingLeft = view.getPaddingLeft(); int paddingRight = view.getPaddingRight(); int paddingBottom = view.getPaddingBottom(); view.setBackgroundResource(backgroundRes); view.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom); } else { view.setBackgroundResource(backgroundRes); } } }