Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch a click event on a button?

Tags:

android

.NET Developer here just getting started with Eclipse and Android.

Can someone show me in the simplest way possible, with the absolute fewest lines of code, how to DO something when a button is clicked?

My button has id button1 and I just want to see where/how to write the onClick() handler.

So let's say I have imageview1 set to invisible. How would I make it visible when the button is clicked?

EDIT:

Thanks everyone, but since not a single one of your examples work for me, I'll try this: Can someone please post the ENTIRE code to make this work? Not just the method, because when I try to use ANY of your methods I get errors all over the place so obviously something else is missing. I need to see everything, beginning with all the imports.

like image 951
Jimmy D Avatar asked May 03 '11 13:05

Jimmy D


People also ask

How do you call a button click event?

How can I call SubGraphButton_Click(object sender, RoutedEventArgs args) from another method? private void SubGraphButton_Click(object sender, RoutedEventArgs args) { } private void ChildNode_Click(object sender, RoutedEventArgs args) { // call SubGraphButton-Click(). }

Is button click an event?

The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button). For more information about handling events, see Handling and Raising Events.


2 Answers

/src/com/example/MyClass.java

package com.example  import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView;  public class MyClass extends Activity {    @Override   public void onCreate(Bundle savedInstanceState)   {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);          Button button = (Button) findViewById(R.id.button1);      button.setOnClickListener(new OnClickListener()     {       public void onClick(View v)       {          ImageView iv = (ImageView) findViewById(R.id.imageview1);          iv.setVisibility(View.VISIBLE);       }     });    } } 

/res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="fill_parent"   android:layout_height="fill_parent">     <Button        android:text="Button"       android:id="@+id/button1"       android:layout_width="wrap_content"        android:layout_height="wrap_content"     />     <ImageView        android:src="@drawable/image"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:id="@+id/imageview1"       android:visibility="invisible"     /> </LinearLayout> 
like image 137
Ben Williams Avatar answered Sep 19 '22 16:09

Ben Williams


The absolutely best way: Just let your activity implement View.OnClickListener, and write your onClick method like this:

public void onClick(View v) {     final int id = v.getId();     switch (id) {     case R.id.button1:         // your code for button1 here         break;     case R.id.button2:         // your code for button2 here         break;     // even more buttons here     } } 

Then, in your XML layout file, you can set the click listeners directly using the attribute android:onClick:

<Button     android:id="@+id/button1"     android:onClick="onClick"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="Button 1" /> 

That is the most cleanest way of how to do it. I use it in all of mine projects today, as well.

like image 42
mreichelt Avatar answered Sep 18 '22 16:09

mreichelt