Reputation: 939
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>
Upvotes: 0
Views: 1740
Reputation: 529
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
Upvotes: 2