Android examples for User Interface:View Background
Set State View Background to StateListDrawable
//package com.java2s; import android.content.Context; import android.graphics.drawable.Drawable; import android.graphics.drawable.StateListDrawable; import android.view.View; public class Main { public static void SetStateViewBkg(View view, Context context, int nNormalImgId, int nDownImgId, int nFocusImgId, int nDisableImgId) { StateListDrawable selector = newSelector(context, nNormalImgId, nDownImgId, nFocusImgId, nDisableImgId); view.setBackgroundDrawable(selector); }/* w w w . j a va 2 s . c o m*/ public static void SetStateViewBkg(View view, Context context, int nNormalImgId, int nDownImgId, int nDisableImgId) { StateListDrawable selector = newSelector(context, nNormalImgId, nDownImgId, nNormalImgId, nDisableImgId); view.setBackgroundDrawable(selector); } public static void SetStateViewBkg(View view, Context context, int nNormalImgId, int nDownImgId) { StateListDrawable selector = newSelector(context, nNormalImgId, nDownImgId, nNormalImgId, nNormalImgId); view.setBackgroundDrawable(selector); } public static StateListDrawable newSelector(Context context, int nNormaImgId, int nDownImgId, int nFocusImgId, int nDisableImgId) { StateListDrawable selector = new StateListDrawable(); Drawable normalDrawable = (nNormaImgId <= 0) ? null : context .getResources().getDrawable(nNormaImgId); Drawable downDrawable = (nDownImgId <= 0) ? null : context .getResources().getDrawable(nDownImgId); Drawable focusDrawable = (nFocusImgId <= 0) ? null : context .getResources().getDrawable(nFocusImgId); Drawable disableDrawable = (nDisableImgId <= 0) ? null : context .getResources().getDrawable(nDisableImgId); // View.PRESSED_ENABLED_STATE_SET selector.addState(new int[] { android.R.attr.state_pressed, android.R.attr.state_enabled }, downDrawable); // View.ENABLED_FOCUSED_STATE_SET selector.addState(new int[] { android.R.attr.state_enabled, android.R.attr.state_focused }, focusDrawable); // View.ENABLED_STATE_SET selector.addState(new int[] { android.R.attr.state_enabled }, normalDrawable); // View.FOCUSED_STATE_SET selector.addState(new int[] { android.R.attr.state_focused }, focusDrawable); // View.WINDOW_FOCUSED_STATE_SET selector.addState( new int[] { android.R.attr.state_window_focused }, disableDrawable); // View.EMPTY_STATE_SET selector.addState(new int[] {}, normalDrawable); return selector; } }