-1

I have created 2 buttons and i want to link both of them to 2 different html links,but i could link only one by using this below code....

package com.kk24.adding two buttons;

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

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse("http://........."));
                    startActivity(myWebLink);
             }
        });

}

Now i want to link button 2 to another link how do we link ????

Give me step by step details if there is something to import or creating a class or so.....

Thanks in advance.

  • You clearly just copied code you don't understand and are asking someone to write the code for you. That is not what *StackOverflow* is for. – Phil Jan 09 '14 at 18:29

3 Answers3

1

Try this code:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse("http://link1."));
                    startActivity(myWebLink);
             }
        });

Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Intent myWebLink2 = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink2.setData(Uri.parse("http://link2."));
                    startActivity(myWebLink2);
             }
        });
Vibhor Bhardwaj
  • 3,071
  • 5
  • 28
  • 49
0

create new String stringUris then make the String stringUris equals the first link in the first button and in the second button equals the second link then start the activity

    String stringUris;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stringUris = "http://www.example1.com";
                Intent Intent1 = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink.setData(Uri.parse(stringUris));
                    startActivity(myWebLink);
             }
        });

Button btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                stringUris = "http://www.example2.com";
                Intent Intent2 = new Intent(android.content.Intent.ACTION_VIEW);
                myWebLink2.setData(Uri.parse(stringUris));
                    startActivity(myWebLink2);

             }

you can use a class for showing webview within the app if you want the class contact me.

0

On xml file in each button add two attributes android:tag with the url and android:onClick with the method name that handle the event

<Button android:id="@id/btSite1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="http://site_1.com" 
    android:onClick="openBrowser"/>

<Button android:id="@id/btSite2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:tag="http://site_2.com"
    android:onClick="openBrowser"/>  

On activity declare the method openBrowser to handle the click event:

public class Main extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void openBrowser(View view){

        //Get url from tag
        String url = (String)view.getTag();
        if(url != null){

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_BROWSABLE);

            //pass the url to intent data
            intent.setData(Uri.parse(url));

            startActivity(intent);
        }
    }
}  

Now when an button is clicked the openBrowser method is called and the browser is open.

ramaral
  • 6,149
  • 4
  • 34
  • 57