I'm trying to use a webview inside a tabhost that has 4 tabs - all linked to the same webview.
This is great except for one problem:
At start up the webview is black.
Clicking tab 2,3 or 4 makes it "come alive".
My quick fix was to use setCurrentTab(1) and then back to 0, but this looks ugly, so I figured I might as well ask for a solution as I cannot find anything online.
How can this be fixed? Below is my XML:
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<android.webkit.WebView android:layout_width="fill_parent" android:id="#+id/webview" android:layout_height="fill_parent" android:scrollbars="none"/>
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</TabHost>
Update:
Putting the webview outside of the framelayout causes the app to crash at startup with following error:
java.lang.RuntimeException: Could not create tab content because could not find view with id 2131099648
This happens when I in the onCreate method initialize my tabhost like this:
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("Tab1", getResources().getDrawable(R.drawable.ligenu)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("Tab2", getResources().getDrawable(R.drawable.mad)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("Tab3", getResources().getDrawable(R.drawable.godpris)).setContent(R.id.webview));
mTabHost.addTab(mTabHost.newTabSpec("tab_test4").setIndicator("Tab4", getResources().getDrawable(R.drawable.om)).setContent(R.id.webview));
Break through!
I found the answer to my own question in another SO post that I didn't stumble upon in the past:
Why is my TabHost's FrameLayout's only child loaded with visibility = View.GONE?
Simply setting:
tabHost.getCurrentView().setVisibility(View.VISIBLE);
That fix the issue!
I was having similar problem. As suggested, I put tabHost.getCurrentView().setVisibility(View.VISIBLE); to the code, the webview still come out blank. After a few more searches, this answer saved me. It turns out that it's important to set android:layout_height="wrap_content" to the webview.
Related
I have a layout XML file straight from the tutorial:
layout/partnertrial_summary.xml
<?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>
In my Activity class, I try to use the following code, but if fails:
mTabHost = findViewById(R.layout.partnertrial_summary.tabhost);
which fails. However, it validates the R.layout.partnertrial_summary part in Green, but when I try to auto-popup the members of partnertrial_summary it yields nothing.
I assume the XML is valid because it came straight from Android, so it's something I am doing wrong on my end. Note that my Activity is not a TabActivity, but I thought it would be okay to use a regular one and add a TabHost, implementing the appropriate listeners. Correct me if my thinking is wrong.
mTabHost = findViewById(R.layout.partnertrial_summary.tabhost);
replace the above as below
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.partnertrial_summary,null);
mTabHost = (TabHost)view.findViewById(R.id.tabhost);
layout can be inflated but cannot be find using findViewById()
I'm currently trying to create an application according the customer's specs, and these include a double tab set.
Meaning that the user needs to click in a tab at the bottom, and for example in the first tab, he will also see a set of tabs at the top where he can click (but when clicking in these, only the ones at the top will change, while the tabs at the bottom will remain the same).
How could I perform this with Android?
So far I could only implement the normal tabs creating a root item 'TabHost' just like this:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" >
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
Thanks a lot in advance!
I found this, thanks to the ActionBar (and SherlockActionBar for pre-Honeycomb devices):
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/ActionBarTabs.html
and also this:
http://www.youtube.com/watch?v=gMu8XhxUBl8
I am trying to create template/BaseActivity class for other developers to use, as a part of framework.
I extended my class with TabActivity, and my xml looks like this/
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<View android:layout_width="fill_parent" android:layout_height="0dip"
android:background="#000" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginLeft="0dip" android:layout_marginRight="0dip" />
<View android:layout_width="fill_parent" android:layout_height="2dip"
android:background="#696969" />
<View android:layout_width="fill_parent" android:layout_height="1dip"
android:background="#fff" />
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
</TabHost>
when I use this activity by extending in other project, I get an error stating, it is not able to get android.R.id.tabhost and this is necessary.
This happens, even if i call getTabHost() or findViewById() both scenarios.
Please note : I tried this without extending the TabActivity, for the use of views in tab. It works fine. But I want to use activities as my tab content.
I think this is the issue when we make it a library project and include it.
Please let me know if you need more explanation or if you know some workarounds, please suggest.
I got around with the issue.
Provided a library class which requires user to pass the current activity instance to my templateLibrary.
It calls sets the Conetnt as one of the layout file containing android tab.
Then get the tab host.
Let me know if anyone wants more details on this.
If you are developing a framework providing layout to user.
I am new to Android and I have been trying to reuse the tutorial on the Android developer website about developing a TabActivity App but, unfortunately, it never worked, even when I constructed it the exact same way as it is described…
Using the debugger it seemed the problem came from the main layout.
-> setContentView(R.layout.main); //After this line the app stops.
Here is my main.xml:
<?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>
If anyone had the same problem, or if someone have some advices they are welcome :)
Tanks a lot!
Randy
The xml you posted is the same as the TabHost example and there is nothing wrong with it.
There must be something in your code.
Please post the code otherwise we can not help you.
In iPhone we can create a view that has a tab bar and make it the root view of the application then use the tab bar to navigate through sub views.
what is the most close approach to this in Android?
Is it to use a Tabbed Control? but this includes using just one activity.
what is the approach to use in Android to create an activity with a navigation control to other activities in a way similar to that of the iPhone?
There's a tutorial for creating a "Tab Layout" on the android dev site:
You can implement your tab content in
one of two ways: use the tabs to swap
Views within the same Activity, or use
the tabs to change between entirely
separate activities
(source: android.com)
Sorry, I really don't know the iPhone, but may a QuickAction Dialog help you??
http://www.londatiga.net/it/how-to-create-quickaction-dialog-in-android/
I imagine a list of some activities in that dialog.
I hope this is close to what you want.
There are a couple of examples around
http://www.anddev.org/code-snippets-for-android-f33/iphone-tabs-for-android-t14678.html
This one is scrollable
http://code.google.com/p/mobyfactory-uiwidgets-android/
<?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:layout_marginBottom="0dp"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</TabHost>