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>