Android Button setOnClickListener Example

  1. Add a button to your “res/layout/main.xml” file.
    exmaple:
  2. Now attach an event Click Listener to the button.
    example:
  3. Here is the complete code for it

[code lang=”java”]
package com.demo.buttonexample;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnClick();
}
public void btnClick() {
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.howtosolvenow.com”));
startActivity(browserIntent);
}
});
}
} 0

About the author

Janaki(http://mjanaki.com) - A Software developer with 9+ years of experience in multiple technology projects across web, mobile, and cloud applications with expertise in PHP, C#, MySQL, Web services and open source platforms.

Similar Posts

Sample nginx vhsot config file
Uninstall MySql on a Mac OS X
VMWare shared folder as apache vhost

Comments are closed.