I need to open a webpage directly if i run the app. Without using a single a component in it.
5 Answers
Here is onCreate method of an Activity that opens google.com directly:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_browser);
WebView wb = (WebView) findViewById(R.id.webView1);
wb.loadUrl("http://www.google.com.tr");
}
and here is the activity_browser.xml layout file:
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BrowserActivity"
android:id="@+id/webView1" >
</WebView>
Don't forget to add the internet permission to AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
I do not quite understand, but I think that's what you need ...
-
Put a link in the response is not the best way to answer. As you can see the link was expired. Commented Jul 8, 2021 at 14:55
As Ercan said, this is how it is done:
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Do it on onCreate method of your Activity.
Add Code in onCreate()
webView.setWebViewClient(new WebViewClient()); //the lines of code added
webView.setWebChromeClient(new WebChromeClient()); //same as above
webView.getSettings().setJavaScriptEnabled(true);
webView.canGoBack();
webView.loadUrl("your url");
final ProgressDialog progressBar = new ProgressDialog(getActivity());
progressBar.setMessage("Please wait...");
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if (!progressBar.isShowing()) {
progressBar.show();
}
}
public void onPageFinished(WebView view, String url) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
if (progressBar.isShowing()) {
progressBar.dismiss();
}
}
});
//To handle Webpage back in fragment
webView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}
});
in xml Add webview:
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<uses-permission android:name="android.permission.INTERNET" />
to your Manifest file