How to display tab view without extending TabActivity - android

I am experimenting with a TabHost widget. I did not realise that the TabActivity has been deprecated. I am having trouble actually displaying my TabHost on the screen when I run my activity. At present My XML file contains the following:
<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"
tools:context=".MainActivity" >
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
And my MainActivity.java contains:
public class MainActivity extends Activity {
TabHost myTabBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
How can I get the TabHost to appear on the screen? Every time I google TabHost all of the tutorials involve extending TabActivity!
Thanks.
EDIT: Am I on the right track when doing something like this?
myTabBar = (TabHost)findViewById(android.R.id.tabhost);
myTabBar.setup();

Activities in tabs was deprecated and Google agrees that it's a bad idea to do this, you have to consider using views as the contents of your Tabs, here an exemple from CommonsWare book you can find helpful resources.

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>

Android autohide feature interferes with my TabHost

I have a test app using TabHost on an Android 4.x. It works ok, except that the TABS are hidden behind the title stuff (that should autohide).
If I enable autohide it's not good either, because even if I see that the title is hiding and the TABS are directly accessible... if I try to click on a TAB, then the titlebar comes down overlapping the TABS and preventing me from clicking on my TAB.
What should I do?
Here is my layout
<FrameLayout 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:background="#0099cc"
tools:context="ro.whatever.myapp.testtabhost.app.main">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#android:id/tabhost"
android:layout_gravity="center_horizontal|top">
<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"></TabWidget>
<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"/>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
So after I create the activity for this layout... I cannot click on the TABS that I can see on top of the screen because the titl-bar drops down and takes the click on itself ...
This is annoying.
How to solve this?
I solved it by doing this in onCreate of the Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); // this thing hides the title bar so I can access the TabHost
setContentView(R.layout.activity_main);
For whom it may concern ;)

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>

TabHost - aligned bottom, disabled and showing no view

I know this is a bit odd but bear with me.
I have a custom view, that extends TabHost and inflates a XML.
It includes several views, it is aligned to bottom and everything works great.
The problem is when I put the TabHost in a different activity, where I don't want to see any of the TabHost views. Only the TabHost tabs on top of the activity layout. Basically, I want the TabHost to be there as a disabled bottom bar.
here's the TabHost XML (which works great and aligned to bottom, as long as I toggle between one of the custom views):
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<CustomView#1
android:id="#+id/tab_watch_me"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<CustomView#2
android:id="#+id/tab_messages"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<CustomView#3
android:id="#+id/tab_search_results"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<CustomView#4
android:id="#+id/tab_my_profile"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<CustomView#5
android:id="#+id/tab_user_profile"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0" />
</LinearLayout>
</TabHost>
Here's the activity XML (the one that I don't want the tabhost views to be shown, only to align the tabhost buttons to the bottom):
<?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" >
<customview.HeaderView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
**//Notice that I don't want to show any of the customViews the tabhost hold. only the tabhost tabs/buttons**
<customview.MyTabHost
android:id="#+id/settings_tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Let me know if you need more code in order to help!
Thank you!
My RelativeLayout solution in case this helps anyone. I'm still looking for a better solution if anyone has to offer.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<customview.HeaderView
android:id="#+id/header_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Any other view
android:layout_below="#+id/header_view"
/>
<customview.MyTabHost
android:id="#+id/settings_tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" />
</RelativeLayout>

Categories

Resources