Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android imagebutton click event in xml

i define an imagebutton like this:

<ImageButton android:src="@raw/blaimage" /> 

now how can i also define which method should be called when the button is clicked.

in the android documentation it says that you can use onClick but it doesnt seem to compile for me.

 android:onClick="selfDestruct" 

and in the activity i have:

 public void selfDestruct(View view) {
     // Kabloey
 }

http://developer.android.com/reference/android/widget/Button.html

like image 304
clamp Avatar asked Sep 07 '26 17:09

clamp


2 Answers

just found out, what the problem was. i was targeting android 1.5 but this feature is only available since API level 4 which is 1.6

like image 151
clamp Avatar answered Sep 10 '26 08:09

clamp


When you get inflated your layout your button is available by id. So you can set any code to be executed when the button is clicked:

Button button = (Button) findViewByID(R.id.button_id);
button.setOnClickListener(new OnClickListener() {
 void onClick(...) {
   // your code here
}
};

Remember, that you should specify the id of your button like this(here it is button_id)

like image 45
Vladimir Ivanov Avatar answered Sep 10 '26 08:09

Vladimir Ivanov