Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

Saturday, January 18, 2014

Android Button Click Event

There are three ways to do it,

First Method,

In the onCreate function,


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);
      }
    });

Second Method,

Import the following package,
import android.view.View.OnClickListener;

In onCreate method add the following Listener,

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();

}

Implement the Listener,

 
public void addListenerOnButton() {
 
button = (Button) findViewById(R.id.button1);
 
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent browserIntent = 
new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.vijaynetwork.com"));
startActivity(browserIntent);
}
});

Third Method,

<Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"

     android:onClick="displayMap" />

when a user clicks the button, the Android system calls the activity's displayMap(View) function.

And the function declaration would be,

public void displayMap(View myview)
{
//made your own code for button click
}