user1464493
user1464493

Reputation: 11

Android: Tabwidget (three tabs) with one webview (not all seperate webviews)

What I'm trying to do is make my Tabs in my TabWidget point to one webview, rather than all three of them having a separate webview.

What I did so far was basically take this Tab-Layout-tutorial and create webviews in the different tabs. This works fine.

The problem is that this obviously opens three websites at the same time. This is not what I'm trying to accomplish. What I want is that all three tabs link to ONE webview.

I've searched for an answer but unfortunately couldn't find one. I did find someone who did the exact same thing on SO here, but unfortunately I don't quite understand how to do it myself.

At the moment my app is quite similar to how it should be made in the tutorial, apart from of course having three separate webviews in the tabs (not textviews), like so (exactly the same, apart from the name, times 3):


public class SongsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.webview);
          WebView webView = (WebView) findViewById(R.id.myWebView);
          webView.getSettings().setJavaScriptEnabled(true);
          webView.setWebViewClient(new WebViewClient());
          webView.loadUrl("http://www.google.com");
         }
}

class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean result = false;
        /* ... */
        // Return false to proceed loading page, true to interrupt loading
        return result;
    }
}

I then have main.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>
</TabHost>

and a webview.xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<WebView
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/myWebView"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent" />

My AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hellotabwidget.android"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-feature android:name="android.hardware.touchscreen" android:required="false"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloTabWidgetActivity"
            android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
            <activity android:name=".SongsActivity"  android:label="@string/app_name"></activity>
            <activity android:name=".AlbumsActivity"  android:label="@string/app_name"></activity> 
            <activity android:name=".ArtistsActivity"  android:label="@string/app_name"></activity> 
    </application>

</manifest>

Upvotes: 0

Views: 2104

Answers (1)

Joes
Joes

Reputation: 11

I'm not really sure, but try with a singleton:

    setContentView(R.layout.webview);
    WebView webView = (WebView) findViewById(R.id.myWebView);
    webView.getSettings().setJavaScriptEnabled(true);
    // webView.setWebViewClient(new WebViewClient());
    webView.setWebViewClient(MyWebViewClient.getInstance());
    webView.loadUrl("http://www.google.com");

    public class MyWebViewClient extends WebViewClient {

    private static MyWebViewClient uniqInstance;

private MyWebViewClient() {
super();
}

public static synchronized MyWebViewClient getInstance() {
if (uniqInstance == null) {
uniqInstance = new MyWebViewClient();
}
return uniqInstance;
}

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

Upvotes: 1

Related Questions