Code example for Field

Methods: get, getType, setAccessible

0
		Class<ColorDrawable> cdClass = ColorDrawable.class;
 
		try { 
			cdBackgroundStateField = cdClass
					.getDeclaredField(COLOR_DRAWABLE_STATE_VAR);
			cdBackgroundStateField.setAccessible(true);
		} catch (Exception e) {
			Log.e(TAG,
					"Reflection failed while trying to determine background color of view.", 
					e);
			cdBackgroundStateField = null;
			return; 
		} 
 
		try { 
			cdBackgroundStateColorField = cdBackgroundStateField.getType()
					.getDeclaredField(COLOR_DRAWABLE_USE_COLOR_VAR);
			cdBackgroundStateColorField.setAccessible(true);
		} catch (Exception e) {
			Log.e(TAG,
					"Reflection failed while trying to determine background color of view.", 
					e);
			cdBackgroundStateColorField = null;
			return; 
		} 
 
	} 
 
	private int getColorFromColorDrawable(ColorDrawable colorDrawable)
	{ 
		if (IS_HONEYCOMB_OR_GREATER) {
			return getColorFromColorDrawableHC(colorDrawable);
		} 
 
		if (!cdBackgroundReflectionReady) {
			initColorDrawableReflection(colorDrawable);
		} 
 
		if (cdBackgroundStateField == null
				|| cdBackgroundStateColorField == null) {
			Log.w(TAG, ERR_BACKGROUND_COLOR);
			return Color.TRANSPARENT;
		} 
 
		Object colorStatusInstance = null;
 
		try { 
			colorStatusInstance = cdBackgroundStateField.get(colorDrawable);
		} catch (Exception e) {
			Log.w(TAG, ERR_BACKGROUND_COLOR, e);
			return Color.TRANSPARENT;
		}