¿Cómo puedo agregar selectableItemBackground a un ImageButton mediante programación?
Android.R.attr.selectableItemBackground existe, pero ¿cómo puedo agregarlo mediante programación a un ImageButton?
Además, ¿cómo voy a encontrar la respuesta en la documentación? Se menciona aquí , pero no veo ninguna explicación de cómo se usa realmente. En realidad, rara vez parecen encontrar la documentación útil, pero espero que sea mi culpa y no la de la documentación.
- Android: la clase R dejó de ser generada
- Android R.java no genera
- Proyecto de limpieza no corrigiendo errores de R.java (Android)
- R.java no está generando debido a appcompat v7?
- Error al solicitar el recurso porque es complejo
- R.java falta en el proyecto de Android en NetBeans
- Renombrar el paquete que R.java se genera en
- Eclipse dando error, falta el archivo de R.java después de la actualización reciente
- Constantes del archivo Android R no se actualizan automáticamente
- ¿Hay alguna forma de generar el archivo R.java en Android Studio?
- Cómo generar R.java
- Android Studio R.java
- ¿Cuál es el concepto detrás de R.java?
Aquí hay un ejemplo con la respuesta aquí: ¿Cómo obtener la referencia attr en el código?
// Create an array of the attributes we want to resolve // using values from a theme // android.R.attr.selectableItemBackground requires API LEVEL 11 int[] attrs = new int[] { android.R.attr.selectableItemBackground /* index 0 */}; // Obtain the styled attributes. 'themedContext' is a context with a // theme, typically the current Activity (ie 'this') TypedArray ta = obtainStyledAttributes(attrs); // Now get the value of the 'listItemBackground' attribute that was // set in the theme used in 'themedContext'. The parameter is the index // of the attribute in the 'attrs' array. The returned Drawable // is what you are after Drawable drawableFromTheme = ta.getDrawable(0 /* index */); // Finally free resources used by TypedArray ta.recycle(); // setBackground(Drawable) requires API LEVEL 16, // otherwise you have to use deprecated setBackgroundDrawable(Drawable) method. imageButton.setBackground(drawableFromTheme); // imageButton.setBackgroundDrawable(drawableFromTheme);
Si está utilizando AppCompat podría utilizar el código siguiente:
int[] attrs = new int[]{R.attr.selectableItemBackground}; TypedArray typedArray = context.obtainStyledAttributes(attrs); int backgroundResource = typedArray.getResourceId(0, 0); view.setBackgroundResource(backgroundResource); typedArray.recycle();
Esto funciona para mí con mi TextView
:
// Get selectable background TypedValue typedValue = new TypedValue(); getTheme().resolveAttribute(R.attr.selectableItemBackground, typedValue, true); clickableTextView.setClickable(true); clickableTextView.setBackgroundResource(typedValue.resourceId);
Como uso la biblioteca AppCompat, uso R.attr.selectableItemBackground
no android.R.attr.selectableItemBackground
.
Creo que typedValue.resourceId
tiene todos los TypeArray#getResourceId(index, defValue)
que usando TypeArray#getResourceId(index, defValue)
o TypeArray#getDrawable(index)
que se recuperan sólo un drawable en el index
dado.
- Android Sdk herramientas Revisión 22 problema?
- ¿Cómo incluir archivos de datos con el APK de la aplicación?