Conor Taylor
Conor Taylor

Reputation: 3108

Using WebView in Android

I have the following code, but when I run it, it tells me that "http://www.google.com" is unavailable, which is total rubbish. If I run the code without the whole HelloWebViewClient class, it opens google properly, except in the pre-installed Android browser. I want to open it in-app. Thanks.

My Code:

public class MainActivity extends Activity {    
    Button btnGo;
    Editable guiNumber;
    WebView webView;

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

        btnGo = (Button) findViewById(R.id.btnGo);
        webView = (WebView) findViewById(R.id.viewWeb);

        webView.setVisibility(View.GONE);
        webView.setWebViewClient(new HelloWebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);

        btnGo.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                guiNumber = enterGUI.getText();

                if (guiNumber.length() == 8) {
                    guiNumber.replace(4, 8, "");
                    gui = Integer.parseInt(enterGUI.getText().toString());
                    System.out.println(gui);

                    InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);

                    webView.setVisibility(View.VISIBLE);
                    webView.requestFocus();
                    webView.loadUrl("http://google.com");
                }
                else {
                    enterGUI.setError("Your GUI/ILGU number must be 8 digits long");
                }
            }
        });
    }

    private class HelloWebViewClient extends WebViewClient {

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

And the manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.frostplant.clublink" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15"/>
<uses-permisssion android:name="android.permission.INTERNET"/>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="@string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

Upvotes: 0

Views: 259

Answers (2)

kabuko
kabuko

Reputation: 36302

You have mistakenly typed 3 "s"s in your permission line:

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

This should be:

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

Upvotes: 1

Daniel Lockard
Daniel Lockard

Reputation: 615

Give your application the permission for accessing internet: android.permission.INTERNET This will allow your application to use network.

Upvotes: 3

Related Questions