¿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.

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.

  • "El prefijo" xliff "para el elemento" xliff: g "no está enlazado"
  • Android ADT versión 22, archivos R.java no generados
  • Proyecto Android a Cordova plugin - R.java no encontrado / creado
  • Cómo eliminar R.java de JavaDoc
  • Falta el archivo R.java aunque se limpió el proyecto
  • Corregir Error en styles.xml para generar R.java: Ningún recurso encontrado nombre 'Theme.AppCompat.Light'
  • Después de configurar android-support-v7-appcompat R.java falta
  • "No se puede resolver el símbolo R" en Android Studio
  • Maven Compilation Error paquete R no existe
  • Error XML al agregar Google Analytics a mi aplicación
  • Problema con aapt.exe
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.