M. Omar
M. Omar

Reputation: 271

How To Open Web Page Within My App?

This is my first question here. I know that this question has been asked before, but I didn't find an answer/solution that really explains the answer for a totally newbie like me.

I am creating an app with a linear layout that has a lot of buttons, each button should drive the user to a different web page. The buttons works well and every buttons goes to its specific web page, but in the default browser, not within the app.

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");

}

I added the internet permission in the Manifest file:

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

This opens the web page but in the default browser of the device and I want it to open inside my app. Any help? (please give me a detailed answer/explanation)

Upvotes: 21

Views: 39534

Answers (8)

Leonardo Fabbro
Leonardo Fabbro

Reputation: 13

You can't talk about the Web nowadays without considering Javascript. By default, its use in a WebView is not active. To enable Javascript just insert these lines of code:

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Upvotes: 1

S HemaNandhini
S HemaNandhini

Reputation: 333

 webView = (WebView) findViewById(R.id.youtubelink);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("your url");

        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

Upvotes: 0

Rammohan Raja
Rammohan Raja

Reputation: 626

Kotlin version of Sunny's answer

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

Upvotes: 2

MurugananthamS
MurugananthamS

Reputation: 2405

I hope this will help you.

In your web view layout:

<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>

Webview Button Onclick:

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);
    }
});

Webview 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();
        }
    });
}

Upvotes: 0

KishuDroid
KishuDroid

Reputation: 5451

Enjoy 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();
            }

        }
    }

Upvotes: 2

Naveen
Naveen

Reputation: 993

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

this code works fine...

Above code opens the link in your App.

Upvotes: -1

Rajesh Jadav
Rajesh Jadav

Reputation: 12861

You need to set up a WebViewClient in order to override that behavior (opening links using the web browser).

Use this;

webview.setWebViewClient(new WebViewClient());

Android documentation says:

public void setWebViewClient (WebViewClient client)

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

Upvotes: 2

Jithin Sunny
Jithin Sunny

Reputation: 3402

Add this to your code

webView.setWebViewClient(new WebViewClient(){

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url){
      view.loadUrl(url);
      return true;
    }
});

Upvotes: 16

Related Questions