Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Open Url within WebView

Hi guys,

inspired by the blogreader app tutorial i wanted to build a simple webview reader for android. The MainActivity.java shows a button, which onclick opens a webpage.. The thing is that my url opens up in the browser and not within the webview. I tried the setwebviewclient http://developer.android.com/guide/webapps/webview.html without success..

Here is the code for the WebViewActivity.java

package com.example.sintest3;

import android.app.Activity; import android.os.Bundle; import android.webkit.WebView;

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://www.google.com");

}

}

I appreciate every help Best, Dave

8 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Can you paste your onClick() code in here? It sounds like the problem is there and not with your WebViewActivity. Without seeing that, my guess is that it's probably something with how you are declaring the Intent.

Hi Ben,

thanks for your response. Here is the code for the MainActivity

package com.example.sintest3;

import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button;

public class MainActivity extends Activity {

private Button button;

public void onCreate(Bundle savedInstanceState) {
    final Context context = this;

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

    button = (Button) findViewById(R.id.buttonUrl);

    button.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View arg0) {
        Intent intent = new Intent(context, WebViewActivity.class);
        startActivity(intent);
      }

    });

}

}

I still haven't solved the issue but think its something with the webviewactivity. Any Ideas?

Best, Dave

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Sorry I missed this over the long weekend! Hmmmm...everything seems okay. So when you tap on the button, it sounds like WebViewActivity isn't even loading? I really can't tell why it would just open the browser instead. You need to declare the Intent differently to get it to open in the browser.

What does your WebViewActivity layout look like? Can you paste the XML for that in here?

The webview xml looks like this

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

maybe there is something with the manifest ?

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sintest3" android:versionCode="1" android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".WebViewActivity"
        android:theme="@android:style/Theme.NoTitleBar" />

    <activity
        android:name="com.example.sintest3.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Looks like the forum stripped some formatting from your code. :-/ If you surround your code with three backticks and a language, like this, then it should keep everything:

```xml

<paste xml stuff in here>

```

Alternatively, you can zip up your project and email it to help@teamtreehouse.com and I can take a look later today.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hey dawid hintz, I'm replying in here in case the information is helpful for anyone else. :)

There is one thing wrong and one thing missing from your code. You are very close, and it's easy to fix!

  1. You want your WebViewActivity to use activity_web_view.xml, not webview.xml. Activity layouts should always have some kind of ViewGroup at the root level like a RelativeLayout or LinearLayout. So delete the TextView in there and add a WebView. You can copy the WebView element from webview.xml if you want (just don't take the 1st line).

  2. You need to explicitly tell your webView to keep links to other pages within it by following this answer on StackOverflow.

I avoided putting the code in here intentionally so you can hopefully work it out as that will be more beneficial. But certainly let me know if you need further help! Happy coding!