1

I have created some buttons in XML and I have method that will onClick open a URL. How do I link this method to my button so that on tap/onClick it will call the method.

Here's the method code:

    public void openResource() {
     Uri uri = Uri.parse("http://librarywales.org");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
}

And I have created an instance of my XML Button in the onCreate method:

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

    Button libButton = (Button) findViewById(R.id.button1);
}

How do I add that method to the libButton instance?

I have now solved the above issue with the help of 'vins' but when I run this application on the AVD and click on a button it pops up with an android message box saying. 'Unfortunately, ApplicationName has stopped working.'

Anyone know why this is?

Thanks, Dan

5 Answers 5

1

Add this after your button declaration

 libButton.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        {
            //open resource method call here...
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Put

android:onClick="openResource" 

In your xml file's button's property.

something like,

<Button
      android:id="@+id/button1"
      .
      .
      android:onClick="openResource"
      />

Note that this feature only is available on Android 2.1 (API Level 7) and higher

2 Comments

I thought of doing this just wasn't sure if it was more common practice to code into in the main application code. Thank you
Android already provide a nice way to handle onClick() method of a View, itself of Activity.
0
public class YourActivity extends Activity  implements OnClickListener {
     public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   Button libButton =(Button) findViewById(R.id.button1);
    libButton.setOnClickListener(this);

    Button OtherButton =(Button) findViewById(R.id.button2);
    OtherButton.setOnClickListener(this);


   }

 @Override
  public void onClick(View v) {

    switch (v.getId()) {
    case R.id.button1: openResource();
                           break;
    case R.id.button2: //do something for second button...

     default : break;
         }

5 Comments

what would the default statement be for this? Thanks by the way
default is just break; and dont forget to add import android.view.View.OnClickListener;
the emulator only responds to me selecting the first button. Is this because of the line of code Button libButton =(Button) findViewById(R.id.button1); because that libButton is assigned to the id.button1
For second or whatever other buttons also you have to set the listener like you did for libButton.setOnClickListener(this); after getting its ID from findviewbyid(). Then you'll be able to catch the onClick event of that button. If you dont setOnClickListener , then if you click that particular button android wont sense that. I have updated it
Ahh right yes the logic has just hit me. Thank you for your patience vins four days into android apps!
0

If you have many buttons with in the page you can use button.setOnClickListener(this);

And override the OnClick() method within the method use Switch statement to match Which View has been selected.It's optimized and efficient way....

Comments

0
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button libButton = (Button) findViewById(R.id.button1);


libButton.setOnClickListener(new OnClickListener() 
{           
    @Override
    public void onClick(View v) 
    {
        if(v.getId()==R.id.button1){
           Uri uri = Uri.parse("http://librarywales.org");
           Intent intent = new Intent(Intent.ACTION_VIEW, uri);
           startActivity(intent);
        }
    }
});

}

2 Comments

thanks for this but there's an error with onClick(View v) it doesn't like the fact there's an @Override present.
also when I run this in the emulator it says Unfortunately, The Application has stopped. :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.