Comment rendre une image cliquable? J'ai essayé certains moyens, mais sans succès. est ici le dernier code j'ai essayé (il est cliquable, mais obtient une erreur):Image cliquable - android
ImageView btnNew = (ImageView) findViewById(R.id.newbutton);
btnNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do stuff
}
});
et voici la partie de xml:
<ImageView
android:src="@drawable/tbnewbutton"
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/newbutton"
android:clickable="true"
android:onClick="clickImage"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
Lors de l'exécution de ce code, et en cliquant sur l'image que je reçois cette erreur:
01-24 19:14:09.534: ERROR/AndroidRuntime(1461): java.lang.IllegalStateException: Could not find a method clickImage(View) in the activity
VOICI LA SOLUTION:
Le XML:
<ImageButton
android:src="@drawable/tbnewbutton"
android:text="@string/hello"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/newbutton"
android:clickable="true"
android:onClick="clickNew"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@null" />
Le code:
public void clickNew(View v)
{
Toast.makeText(this, "Show some text on the screen.", Toast.LENGTH_LONG).show();
}
Ok, celui-ci fonctionne, mais maintenant mon image est à l'intérieur d'un bouton .. Donc, il freine ma mise en page. Pour résoudre ce problème, j'ai ajouté la ligne android: background = "@ null" dans le fichier XML et tout fonctionne parfaitement! – user484146
Souhaitez-vous alors marquer cette réponse comme utile? –
L'arrière-plan correct à utiliser est 'android: background ="? Android: selectableItemBackground "'. – thirtythreeforty