Nas
Nas

Reputation: 2198

How to use webview as a browser in android application?

URL HERE

When I load the above url in my application webview it change as http://m.allrecipes.com But when i load the same url in a browser its url as http://allrecipes.com.

Is there any way to load normal url(http://allrecipes.com) in my application webview without loading page for mobile(http://m.allrecipes.com)

Anybody Know please help me

go.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (Searchtext.getText().toString().equalsIgnoreCase("")) {

                    alertDialog = new AlertDialog.Builder(OnlineRecipe.this)
                            .create();
                    alertDialog.setTitle("Message");
                    alertDialog.setMessage("Please Enter Some Word");

                    alertDialog.setButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // Write your code here to execute after
                                    // dialog closed
                                    alertDialog.dismiss();
                                }
                            });

                    // Showing Alert Message
                    alertDialog.show();
                } else {

                    String url = "http://allrecipes.com/search/default.aspx?qt=k&wt="
                            + Searchtext.getText().toString()
                            + "&rt=r&origin=Recipe Search Results";
                    webview.getSettings().setJavaScriptEnabled(true);
                    webview.loadUrl(url);


                }



            }
        });

Upvotes: 0

Views: 2564

Answers (2)

anthonycr
anthonycr

Reputation: 4186

If I understand correctly, all you want to do is load http://allrecipes.com in your WebView in desktop mode... If that is the case, use the following code to get it to work:

webview.getSettings().setUserAgentString("Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/20 Safari/537.31");

This only needs to be placed in your code right after the part where you create the WebView object, webview.

EDIT: just so you know for future reference, this line of code changes the user agent to trick the website into thinking you're accessing it from a computer instead of a phone.

Upvotes: 4

d.danailov
d.danailov

Reputation: 9800

Do not remeber to add activity in your : AndroidManifest.xml

Example :

    <activity android:name=".Second"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:configChanges="orientation|keyboardHidden">
    </activity>

Class :

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Second extends Activity {

    WebView webView;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webView = (WebView)findViewById(R.id.webview);

        webView.getSettings().setJavaScriptEnabled(true);

        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        webView.setWebViewClient(new WebClient());

        webView.loadUrl(getIntent().getExtras().getString("url"));

    }



    public class WebClient extends WebViewClient
    {
        ProgressDialog pd; // Create Proggress Dialog to show if User Internet connection is slow

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            Intent i = new Intent(Second.this, Second.class);
            i.putExtra("url", url);
            startActivity(i);

            return true;
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            pd = ProgressDialog.show(Second.this, "",getString(R.string.loading), true);
            pd.setCancelable(true);
        }

        public void onPageFinished(WebView view, String url)
        {
            if (pd.isShowing()) {
                pd.dismiss();
            }
        }
    }
}

Upvotes: 0

Related Questions