How to open a webpage in my application?

This is my first question. I know that this question was asked before, but I did not find an answer / solution that really explains the answer for completely newbies like me.

I am creating a linear layout application that has many buttons, each button should lead the user to a different web page. Buttons work well, and each button goes to a specific web page, but in the default browser and not in the application.

This is my webview.xml file:

<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/webView1" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 

This is the WebViewActivity.java file:

 public class WebViewActivity extends Activity { private WebView webView; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); webView = (WebView) findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl( "http://egy-tech-droid.blogspot.com.eg/search/label/%D8%AA%D8%B7%D8%A8%D9%8A%D9%82%D8%A7%D8%AA%20%D8%AD%D8%B5%D8%B1%D9%8A%D8%A9"); } 

In the manifest file, I added permission to access the Internet:

  <uses-permission android:name="android.permission.INTERNET" /> 

This opens the webpage, but in the device’s default browser, and I want it to open in my application. Any help? (please give me a detailed answer / explanation)

+5
source share
6 answers

Add this to your code

 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url){ view.loadUrl(url); return true; } }); 
+2
source

You need to configure WebViewClient to override this behavior (open links using a web browser).

Use it;

 webview.setWebViewClient(new WebViewClient()); 

The Android documentation says:

public void setWebViewClient (WebViewClient client)

Sets a WebViewClient that will receive various notifications and requests. This will replace the current handler.

+1
source

Enjoy the full code:

Oncreate ():

  webView = (WebView) findViewById(R.id.webView1); if(Constants.isNetworkAvailable(mContext)){ webView.setWebViewClient(new MyWebViewClient()); webView.setWebChromeClient(new WebChromeClient() ); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setPluginState(PluginState.ON); webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setSupportZoom(true); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setScrollbarFadingEnabled(false); webView.setInitialScale(30); webView.loadUrl(url); }else{ Toast.makeText(mContext, Constants.msgNoInternet, Toast.LENGTH_LONG).show(); } 

MyWebViewClient:

 private class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); if (!pDialog.isShowing()) { pDialog.show(); } return true; } @Override public void onPageFinished(WebView view, String url) { //view.loadUrl(url); System.out.println("on finish"); if (pDialog.isShowing()) { pDialog.dismiss(); } } } 
0
source

Hope this helps you.

In web layout mockup:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainll" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/relay" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#c7bbac"> <ImageView android:id="@+id/txtmain" android:layout_width="fill_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="fitXY" android:src="@drawable/topbar50" /> <ImageView android:id="@+id/backbutn" android:layout_width="wrap_content" android:layout_height="30dp" android:adjustViewBounds="true" android:paddingTop="2dp" android:src="@drawable/backbtn" /> </RelativeLayout> <WebView android:id="@+id/webView1" android:layout_below="@+id/relay" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout> 

Onclick Web Browsing Button:

 webbutton = (ImageView) findViewById(R.id.web); webbutton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(getApplicationContext(), WebViewActivity.class); startActivity(intent); } }); 

Web Viewer Activity:

 public class WebViewActivity extends Activity { private WebView webViewurl; ImageView back; AndroidInterface AMW = AndroidInterface.GetInstance(); public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); back = (ImageView) findViewById(R.id.backbutn); webViewurl = (WebView) findViewById(R.id.webView1); webViewurl.getSettings().setJavaScriptEnabled(true); webViewurl.getSettings().setBuiltInZoomControls(true); final Activity activity = this; webViewurl.setWebViewClient(new WebViewClient() { public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); } }); webViewurl.loadUrl("http://example.com"); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub finish(); } }); } 
0
source

Kotlin version of the solar response

 webView.webViewClient = object : WebViewClient() { override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean { view?.loadUrl(request?.url.toString()) return true } } 
0
source

WebView myWebView = (WebView) findViewById (R.id.webview); myWebView.loadUrl (" http://www.google.co.in ");

this code works fine ...

Above the code, a link opens in your application.

-1
source

Source: https://habr.com/ru/post/1233110/


All Articles