0

I am new to android apps development. By searching many links and reading the discussion on StackOverflow, I am able to create WebView through button link. But I can't success to use multi URL in WebView though I have tried a lot. I mean how can I show different URL in one webview according to specific button link. For example: if button 1 clicked it would show google.com, button 2 would show facebook.com etc in webview. I have coded as follow.

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loading from a Webpage"
        android:layout_margin="10dp"
        android:onClick="clickweb"/>

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Loading from a Webpage"
        android:layout_margin="10dp"
        android:onClick="clickweb"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {

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

}
public void clickweb(View v) {
    Toast.makeText(MainActivity.this, "Web view selected", Toast.LENGTH_LONG).show();
    Intent i1 = new Intent(MainActivity.this,LoadWeb.class);
    startActivity(i1);
}
}

LoadWeb.java

@SuppressLint("SetJavaScriptEnabled")
public class LoadWeb extends Activity {

private WebView mWebView;

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

    // Get reference of WebView from layout/webviewex.xml
    mWebView = (WebView) findViewById(R.id.webView1);

    setUpWebViewDefaults(mWebView);
    // Load website
    mWebView.loadUrl("https://www.google.co.in");
}

//Convenience method to set some generic defaults for a

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setUpWebViewDefaults(WebView webView) {
    WebSettings settings = webView.getSettings();

    // Enable Javascript
    settings.setJavaScriptEnabled(true);

    // Use WideViewport and Zoom out if there is no viewport defined
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);

    // Enable pinch to zoom without the zoom buttons
    settings.setBuiltInZoomControls(true);

    if(Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
        // Hide the zoom controls for HONEYCOMB+
        settings.setDisplayZoomControls(false);
    }

    // Enable remote debugging via chrome://inspect
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
}
}

webviewex.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>
Kartik Shandilya
  • 3,796
  • 5
  • 24
  • 42
Dipak
  • 931
  • 14
  • 33
  • Are you sure you tried everything? You need to call `mWebView.loadUrl` should work fine. call it again on button click with different URL. – Rachit Mishra Oct 15 '17 at 11:22
  • By my way, every link open same url 'google.co.in'. Maybe, it is in fault (my) while assigning next url. Suppose how to assign facebook.com to button 2, I get confused ? Thanks for your response. – Dipak Oct 15 '17 at 11:30
  • Make mWebView a member variable, and when facebook button is clicked call mWebView.loadUrl() with new url – Rachit Mishra Oct 15 '17 at 11:32

1 Answers1

2

You'll need different cases for your buttons. After that you can add extras to the intent before starting your LoadWeb activity. In this case you'll add a String that holds the URL you want to load. The last step would be to get this String in the LoadWeb activity from the starting intent and set it as the URL in loadUrl();

MainActivity.java

public void clickweb(View v) {

    Toast.makeText(MainActivity.this, "Web view selected", Toast.LENGTH_LONG).show();
    Intent i1 = new Intent(MainActivity.this,LoadWeb.class);

    switch(v.getId()) {
        case R.id.button1:
            i1.putExtra("URL", "https://www.google.co.in");
            break;
        case R.id.button2:
            il.putExtra("URL", "https://www.facebook.com");
            break;
    }

    startActivity(i1);
}

LoadWeb.java

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

    // Get reference of WebView from layout/webviewex.xml
    mWebView = (WebView) findViewById(R.id.webView1);

    setUpWebViewDefaults(mWebView);

    // Get URL from Intent
    String URL = getIntent().getExtras().getString("URL");

    // Load website
    mWebView.loadUrl(URL);
}

Take a look at the documentation of Intent to get an overview what else you can send and retrieve by an Intent https://developer.android.com/reference/android/content/Intent.html

CodeRed
  • 481
  • 4
  • 17
  • Thanks very much, @CodeRed, It works, but second link open in web browser instead of webview. How to fix it ? – Dipak Oct 15 '17 at 15:28
  • 1
    Well I don't have much time at the moment but you can try what I would've tried: [Try this](https://stackoverflow.com/questions/2378800/clicking-urls-opens-default-browser) – CodeRed Oct 15 '17 at 15:41
  • I fixed it. Thanks very much @CodeRed – Dipak Oct 16 '17 at 03:23