user3695970
user3695970

Reputation: 51

android studio click button1 from activity1 to load activity2 webview

My main goal is to click a button from Activity1 and then WebView of Activity2 loads up a page from a html file locally.

Just for the test purpose I'll use only 1 button but personally I will have more then 1 button and each button will navigate to different page.

Here is the code I have in Activity1 to open Activity2

Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);

and this is what I want to load once Activity2 loads

 setContentView(R.layout.activity_main);
                WebView view = new WebView(this);
                view.getSettings().setJavaScriptEnabled(true);
                view.loadUrl("file:///android_asset/index.html");
                view.setBackgroundColor(Color.TRANSPARENT);
                setContentView(view);

I can do this with 1 button fine ? but problem comes when i want to use a different button in Activity1 with different page say index.html2 or index.html3. Can anyone help me please?

Thanks.

Upvotes: 1

Views: 222

Answers (2)

theMfromA
theMfromA

Reputation: 289

You should think about what is your problem.

You've got a button in Activity1. Clicking the button starts Activity2 and doing some specific things (like opening index1.html) there.

Now you want to add some more buttons to Activity1.

Each button shall open Activity2, too. But each button shall do some specific things (like opening one out of "index1.html", "index2.html" or "index3.html").

So what's your problem? You don't know how to tell Activity2 which Button was clicked, so Activity2 doesn't know which "index.html" to open.

Now, let me tell you how to do it:

You're using this code already to start Activity2:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

But an Intent can do much more than starting an Activity! You can add data to the intent-object, and get them back later.

Add data to your intent by doing this:

Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("htmlToOpen", "index1.html"); //Here you put your info
startActivity(intent);

Now, in your Activty2, you've got to get back the data:

String fileName = getIntent().getStringExtra("htmlToOpen");

Now you can use the String fileName to open your desired html-file:

setContentView(R.layout.activity_main);
WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/" + fileName);
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);

To learn more about Intents read here: Intents and Intent Filters


By the way: I think you're trying to do the following:

  1. You start your app in your MainActivity.
  2. There you've got a button. The user can click the button to open Activity1.
  3. In Activity1 the user can choose one website (html-file).
  4. The website should be shown in MainActivity, now.

If this is what you want to do, let me suggest the Android-way to do it:

In your MainActivity, you start Activity1 by doing this:

Intent i = new Intent(this, Activity1.class);
startActivityForResult(i, 1);

That code means you're starting your Activity1 just to get a result back (in this case, the result is the desired website).

Now, in your Activity1, you give the result (the desired website) back by doing this:

Intent returnIntent = new Intent();
returnIntent.putExtra("htmlToOpen","index1.html"); //if another button was clicked, put another filename, here.
setResult(RESULT_OK,returnIntent);
finish();

Now, back again in your MainActivity, you get the result by adding the following function:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == RESULT_OK){
        String fileName = data.getStringExtra("htmlToOpen");
        //Now open your desired website by using the String fileName,
        //which contains the String "index1.html"
    }
}
}

If you want to learn more about starting Activities for results, read:

Upvotes: 1

Dipesh Dhakal
Dipesh Dhakal

Reputation: 194

Do this:

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_1:
           Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index1.html");
            startActivity(i);

            break;
        case R.id.btn_2:
            Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index2.html");
            startActivity(i);

            break;
        case R.id.btn_3:
            Intent i = new Intent(this,MainActivity.this);
            i.putExtra("page","file:///android_asset/index3.html");
            startActivity(i);

            break;
    }

}

and in your other activity onCreate:

setContentView(R.layout.activity_main);
String pageString = getIntent().getExtras().getString("page");
WebView view = new WebView(this);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(pageString);
view.setBackgroundColor(Color.TRANSPARENT);
setContentView(view);`

Upvotes: 0

Related Questions