0

So i am trying to load URL in Dialog using webview. i have set Internet permission in Manifest file too. but the URL is not getting loaded. i don't know what is wrong. i have tried this solution too.

Edit: Internet connection is not the problem since i am able to browse google from emulator browser. activity_main.xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="25sp"
        android:text="Reddit OAuth2" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/auth"
        android:text="Sign in with Reddit" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Access"
        android:autoLink="all"
        android:textIsSelectable="true"
        android:layout_gravity="center"
        android:textSize="10sp"/>

</LinearLayout>

auth_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/webv"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

WebView web;
Button auth;
SharedPreferences pref;
TextView Access;
Dialog auth_dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Access =(TextView)findViewById(R.id.Access);
    auth = (Button)findViewById(R.id.auth);
    auth.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View arg0) {
           // TODO Auto-generated method stub
           auth_dialog = new Dialog(MainActivity.this);
           auth_dialog.setContentView(R.layout.auth_dialog);
           web = (WebView) auth_dialog.findViewById(R.id.webv);
           web.getSettings().setJavaScriptEnabled(true);

            web.loadUrl("http://www.google.com");

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

           });
           auth_dialog.show();
           auth_dialog.setTitle("Authorize");
           auth_dialog.setCancelable(true);
             }
             });

  }
   }

Output i am getting:

this is the output i am getting.

Community
  • 1
  • 1
Try
  • 31
  • 6

1 Answers1

0

you should set the shouldOverrideUrlLoading to return false. Now your code is using the inbuilt browser instead of your webviewto load the url

        web.setWebViewClient(new WebViewClient() {
           @Override
           public boolean shouldOverrideUrlLoading(WebView view, String url) {

               return false;
           }

       });
William Ku
  • 798
  • 5
  • 17