Why can't my TabHost child activity find the parent TabHost ID? - android

I have a TabHost activity that are switching activities, so my main looks like this:
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="-4dp"
android:layout_weight="0" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp" />
</LinearLayout>
</TabHost>
And one of my child activities XML is a ListView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And in this child activity, (which extends ListActivity) I'm trying to find the parent TabHost:
TabHost tabHost = (TabHost) getParent().findViewById(R.id.tabhost);
tabHost.setCurrentTab(0);
But, the R.id.tabhost is not found (tabhost id cannot be resolved).
What am I doing wrong?

Make it android.R.id.tabhost
It will work.

Related

How to add seperate layout and class to TabHost in android?

My base layout like follow
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="lk.abanservice.shankan.abansserviceapp.CheckStatus">
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+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">
<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">
<LinearLayout
android:id="#+id/checkSerach"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/checkDisplayAll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
so i need to add following separate file and class to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title_request"
android:id="#+id/textViewTitleRequestt"
android:textStyle="bold" />
i tried several ways to do this it can be do using TabActivity but that was removed from android 11. anyone has solution for that?
I want to develop following interfaces
This is Tab Host Example.. Change variables and class name according to your requirements...
MainActivity.java
public class MainActivity extends TabActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tab = getTabHost();
TabSpec news = tab.newTabSpec("New");
news.setIndicator("Check New Job");
Intent intent = new Intent(MainActivity.this,First.class);
news.setContent(intent);
TabSpec old = tab.newTabSpec("Old");
old.setIndicator("Check Old Job");
Intent intent_old = new Intent(MainActivity.this,Second.class);
old.setContent(intent_old);
tab.addTab(news);
tab.addTab(old);
}
}
activity_main.java
<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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
</FrameLayout>
</LinearLayout>
</TabHost>

ActionBar not Displaying

I had created a project with three tabs in android using TabHost and TabSpec.I extend my
main activity with TabActivity The problem is ActionBar didn't display on the app.I used the Holo.Light.DarkActionBar theme.
MainActivity.java
public class MainActivity extends TabActivity {
TabHost tabhost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabhost=getTabHost();
TabHost.TabSpec homespec=tabhost.newTabSpec("Home");
homespec.setIndicator("Home",null);
Intent homeintent=new Intent(this,HomeActivity.class);
homespec.setContent(homeintent);
TabHost.TabSpec eventspec=tabhost.newTabSpec("Event");
eventspec.setIndicator("Event");
Intent eventintent=new Intent(this,EventActivity.class);
eventspec.setContent(eventintent);
TabHost.TabSpec profilespec=tabhost.newTabSpec("Profile");
profilespec.setIndicator("Profile");
Intent profileintent=new Intent(this,ProfileActivity.class);
profilespec.setContent(profileintent);
tabhost.addTab(homespec);
tabhost.addTab(eventspec);
tabhost.addTab(profilespec);
}
}
activity_main.xml
`
<TabHost
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#android:id/tabhost"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<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">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
/>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
</FrameLayout>
</LinearLayout>
</TabHost>
Is there any other way to achieve this Suggest me also?
Set theme for this activity in AndroidManifest.xml:
<activity android:name=".MainActivity"
android:theme="#android:style/Theme.Holo.Light"/>
Go to activity_main and klick to Design -> Theme (on top bar, next to Device name and land-scape) -> Manifest Themes -> and choose your theme.
edit:
add this of course (If you are not intentionally forgotten )
<?xml version="1.0" encoding="utf-8"?>
change your layout 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="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<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" >
</FrameLayout>
</LinearLayout>
</TabHost>

Using TabHost include other layout

I have a TabHost layout (the below code)
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="65dp" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65dp" >
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</TabHost>
I am loading some activities inside the TabHost and every things about the activities are OK. but my problem is about loading another layout at the top of TabHost via Include tag,
in other layouts like LinearLayout i put a include tag inside it and it was OK, but in TabHost i could not do like that because after adding include the TabHost UI gets corrupted
.
please tell me how to solve this problem
Wrap your TabHost tag with another LinearLayout tag like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- inclde goes here -->
<TabHost
android:id="#+id/myTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Other tags as you have already. -->
</TabHost>
</LinearLayout>
Then Place the LinearLayout as a parent to TabHost and include that LinearLayout using include tag in other layouts as below:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="#+id/myTabHost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="65dp" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingTop="65dp" >
<LinearLayout
android:id="#+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</TabHost>
</LinearLayout>

How do I add TabWidget and FrameLayout to TabHost, manually?

I've created a class which I want to use to create tabs(I dont want to use an Activity). Now, my tabs.xml file contains:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab_host"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="#+id/tabs" //Not #android:id="#android:id/tabs
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#+id/tabcontent" //Not#android:id="#android:id/tabcontent
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/tab_horizontal_scroll" layout="#layout/gallerymodule"/>
</FrameLayout>
</LinearLayout>
</TabHost>
I understand that the TabHost object do not retrieve references to TabWidget and FrameLayout.
I would like to know how to add these references manually? Thanks.

How to add tabs at bottom

I have a tabhost which contains three tabs, connected with three activities. All three tabs are in the upper side. How can I make these three tabs at the bottom?
I found this, but there are still some problems.
This is my tabhost XML 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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
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>
In my TabActivity class, I have following code:
setContentView(R.layout.tabs);
TabHost mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("TAB 1").setContent(new Intent(AndroisTabViewActivity.this,TabActivity1.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(new Intent(AndroisTabViewActivity.this,TabActivity1.class)));
mTabHost.addTab(mTabHost.newTabSpec("tab_test3").setIndicator("TAB 3").setContent(new Intent(AndroisTabViewActivity.this,TabActivity1.class)));
mTabHost.setCurrentTab(0);
There is one activity named TabActivity1 which I have also included in Manifest file. But it is showing the error:
Can't get the activity TabActivity1.
I assume you want to show "Tab" at bottom of the window, just try this:
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/linearLayout1"
android:layout_height="match_parent">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"
android:layout_alignParentBottom="true">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent">
</FrameLayout>
</RelativeLayout>
If you want to use the LinearLayout:
<?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="0dp"
android:layout_weight="1" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>
With this solution, the tab content will take no space firstly, the TabWidget will take its own space, then the remaining space will be given to the tab content.

Categories

Resources