TabWidget - How to set position of indicator text? - android

i'm trying to use TabHost and TabWidget in my Android application. This is my layout main.xml:
<TabHost
android:id="#+id/mainTabHost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TabWidget android:id="#android:id/tabs" android:layout_width="fill_parent" android:layout_height="65px"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/contentTags"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="#+id/contentPreferences"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
</LinearLayout>
</FrameLayout>
</TabHost>
And my code:
final TabHost mainTabHost = (TabHost) this.findViewById(R.id.mainTabHost);
mainTabHost.setup();
final TabHost.TabSpec tabSpecTags = mainTabHost.newTabSpec("tabTags");
tabSpecTags.setIndicator(this.getString(R.string.tab_name_tags));
tabSpecTags.setContent(R.id.contentTags);
mainTabHost.addTab(tabSpecTags);
final TabHost.TabSpec tabSpecPreferences = mainTabHost.newTabSpec("tabPreferences");
tabSpecPreferences.setIndicator(this.getString(R.string.tab_name_preferences));
tabSpecPreferences.setContent(R.id.contentPreferences);
mainTabHost.addTab(tabSpecPreferences);
The problem is that i do not want my tabs to be so tall (65px). However, if i set the layout_height of the TabWidget to e.g. 30px, i can't see the tab labels (indicator) on the tabs at all.
Seems like there is a "minimum required" height for the TabWidget (65px?) and the indicator is positioned at the bottom of this 65px?
Is there a way to adjust the positioning of the indicator?
Thanks!

However, if i set the layout_height of
the TabWidget to e.g. 30px, i can't
see the tab labels (indicator) on the
tabs at all.
Note that the techniques for doing this won't necessarily be reliable in future Android releases, if they change up how a TabHost is constructed.
Is there a way to adjust the
positioning of the indicator?
Starting with API Level 4 (a.k.a., Android 1.6), TabHost.TabSpec accepts a View as the indicator via setIndicator(). I haven't tried it, but it may give you greater control over the layout of an individual tab's indicator.

i see...
when u addTab, usually we use setIndicator like this:
QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").bla bla....
u can use TextView to replace "TAB 2", became like this:
tview=new TextView(this);
tview.setText("Title here");
QTabHost.addTab(QTabHost.newTabSpec("tab_test2").setIndicator(tview).bla bla....
all u need is just modify the textview.
Thanks... ^^

int iH = getTabWidget().getLayoutParams().height;
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = iH;
}

for (int i = 0; i < 4; i++) {
tabhost.getTabWidget().getChildAt(i).getLayoutParams().height = 50;
}
by using this we can do very easily

Related

TabLayout : Set custom color for each tab

I saw a lot of questions that say how to set the color differently for selected(active) and unselected(inactive) tabs. I also know that google provides void setTabTextColors (int normalColor, int selectedColor) to achieve this.
My requirement is a little different, I am working on a quiz app with a TabLayout and CardView. TabLayout allows the user to navigate between questions and CardView is used to display the questions.
I need to set the color of the tabs for which the user has already selected an answer differently than that for which the user has not answered yet. By default the TextColor is black but if the user selects an answer then the tabcolor should change to blue (just for eg.) and it should remain that way till the user exits. I have a int array called Select that will hold the value of the option that the user has selected (The values range between 1 - 4). While allocating the Select array I also initialize it with -1. I thought of setting up a loop and then if the array is -1 leave the tab as it is or set the tabcolor to blue.
How can I achieve this functionality?
You can work with TabLayout internals by querying for this children and changing TextViews manually. This can break your code when you upgrade to another support library version, but as long as you keep track and test when updating, it should work:
private void updateTabTextColors() {
LinearLayout tabsContainer = (LinearLayout) mTabLayout.getChildAt(0);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
LinearLayout item = (LinearLayout) tabsContainer.getChildAt(i);
TextView tv = (TextView) item.getChildAt(1);
tv.setTextColor(Select[i] == -1 ? Color.BLACK : Color.BLUE);
}
}
Just enhancing Marcelo Liberato's answer to support custom background for each tab item.
LinearLayout tabsContainer = (LinearLayout) mTabLayout.getChildAt(0);
LinearLayout childLayout1 = (LinearLayout)tabsContainer.getChildAt(2);
LinearLayout childLayout2 = (LinearLayout)tabsContainer.getChildAt(3);
LinearLayout tabView = (LinearLayout) childLayout1.getChildAt(0).getParent();
tabView.setBackgroundResource(R.drawable.ic_blue_selector);
tabView = (LinearLayout) childLayout2.getChildAt(0).getParent();
tabView.setBackgroundResource(R.drawable.ic_red_selector);
Custom xml file:
<?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:id="#+id/ll_tab_holder"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_tab_icon_title_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/tab_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="#style/lasuCustomTabText" />
</LinearLayout>
<TextView
android:id="#+id/tab_only_title"
android:layout_width="match_parent"
android:layout_height="56dp"
android:textAllCaps="true"
android:textSize="12sp"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#drawable/ic_tab_text_color_selector" />
</LinearLayout>
Output:
If you are interested in using a library for this functionality, this library works well.
https://github.com/astuetz/PagerSlidingTabStrip
As in the doc getTabTextColors() -> Gets the text colors for the different states (normal, selected) used for the tabs. the tabs can only have 2 states. The only way to achieve what you want if to inherit Tab class and add a new state, something like: tabAlreadyVisited. Then #Override the draw method to change background color based on the tabAlreadyVisited attribute value. Or change the text color with setTabTextColors
It's possible to set custom view for your tab
TabLayout.Tab yourTab = tabLayout.newTab();
yourTab.setCustomView(R.layout.red_text_view);
And red_text_view.xml is
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:textColor="#f44336"/>
If you use the #android:id/text1 default Tab's settext should work. You could do whatever you want with your custom view.

Tab Divider issue

I have an application where I have 5 tabs set up, and I it works perfectly except for one issue I noticed recently. On large screen sizes, for some reason, the tabs are transparent, I only noticed this when I tried out the application on one of my colleagues large android device. So I then set up the emulator to a large screen size of 10 inches, and its the same thing.
So in order to fix this, I used this code
public static void setTabColor(TabHost tabhost) {
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
tabhost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#292929")); // unselected
// }
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#8b8b8b")); // selected
}
}
Then I use OnTabChangeListener and place
setTabColor(tabHost);
under each tab change. This fixed the issue so that the tab colours are the same for all screen sizes. However the problem now is that the divider is gone. I tried using
tabHost.getTabWidget().setDividerDrawable(R.drawable.separator);
but the problem is this piece of code must be called before you set the content of the tabs and seeing I have set up
setTabColor(tabHost);
on every tab change, it makes it rather useless as it works when application loads up, but when you switch tab, it is gone again, and I can't call it again otherwise application will crash.
Does anyone have any suggestions as to how I can fix this issue? And also could someone explain to me why the tabs are transparent on larger screens? Why does Android do this?
Thanks in advance for any assistance
This is my solution for divider lines in tab bar,
this is the tab bar sample code.
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/tab_bar_bg_full"
android:divider="#null"
>
</TabWidget>
this is the first tab
<?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:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/shuffle_start_btn" />
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/tab_bar_divider_new" />
</LinearLayout>
Here i included the divider line in tab xml and it worked for me
I fixed this issue, turns out I could just color the tabs in and did not need to use the tab divider after all. Used this method here
public static void setTabColor(TabHost tabhost) {
for (int i = 0; i < tabhost.getTabWidget().getChildCount(); i++) {
tabhost.getTabWidget().getChildAt(i)
.setBackgroundColor(Color.parseColor("#292929")); // unselected
// }
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab())
.setBackgroundColor(Color.parseColor("#8b8b8b")); // selected
}
}
To color in tabs

'Unselect' a tab in a TabHost

I have a TabHost holding 5 tabs.
As far as I can see, there has to be one tab selected at all times.
I need a way to unselect all my tabs so none will be selected.
If the tabhost is meant by general to have one tab selected at all times,
how can I make it appear (UI speaking) as if the tab isn't selected?
try this:
final TabWidget tabWidget = tabHost.getTabWidget();
final int n = tabWidget.getChildCount();
for (int i = 0; i < n; ++i) {
tabWidget.getChildAt(i).setSelected(false);
}
or you can add hidden tab and select it when you want unselect a tab
tabHost.addTab(
tabHost.newTabSpec("hiddenTab").setIndicator(""),
MyFragment.class,
null
);
tabHost.getTabWidget().getChildTabViewAt(hiddenTabIndex).setVisibility(View.GONE);
and select this tab when you want
tabHost.setCurrentTab(hiddenTabIndex);
This is not possible AFAIK. but yes,you can set the selected tab's color to look like it is unselected and set a blank layout over it by managing a global variable when you make it 'unselected' and setting up normal layout when you want it to be shown normally to user. But this is kind of a trick.
Hope,you get my point!
EDIT :
Suppose you have set String what="disappear" somewhere in your code to show it 'unselected',then you can use this function to change color of tab:
Main.class:
//Change The Backgournd Color of Tabs
public void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++)
{
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"))); //unselected white colored
}
if(!what.equals("disappear"))
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FF0000"))); // selected red colored
else
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("FFFFFF"))); // selected but show as unselected with white color
}
And in your activity class(which is opened by that selected tab):
FirstActivity.class:
if(what.equals("disappear"))
setContentView(R.layout.blank);
else
setContentView(R.layout.first_layout);
blank.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/layout"
android:background="#ffffff"
android:gravity="center">
<!-- You can make background transperent by setting it to "00ffffff" -->
<!-- You can also add this textview to guide user -->
<!--
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click Any Tab To Start
/>
-->
</LinearLayout>
For this purpose, maybe using tabHost is not the proper way ?

Android Tabs in Bottom of the Layout [duplicate]

I've seen some chatter about this, but nothing definite.
Is there a way to put the tabs in a TabWidget to the bottom of the screen?
If so, how?
I've tried the following, but didn't work:
a) setting the tabwidget below the framelayout
b) setting the tabwidget's gravity to "bottom"
Thanks!
llappall
Here's the simplest, most robust, and scalable solution to get tabs on the bottom of the screen.
In your vertical LinearLayout, put the FrameLayout above the TabWidget
Set layout_height to wrap_content on both FrameLayout and TabWidget
Set FrameLayout's android:layout_weight="1"
Set TabWidget's android:layout_weight="0" (0 is default, but for emphasis, readability, etc)
Set TabWidget's android:layout_marginBottom="-4dp" (to remove the bottom divider)
Full code:
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp"/>
</LinearLayout>
</TabHost>
Try it ;)
Just watch the content of the FrameLayout(#id/tabcontent), because I don't know how it will handle in case of scrolling... In my case it works because I used ListView as the content of my tabs. :)
Hope it helps.
<?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">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#android:id/tabs" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
There is a way to remove the line.
1) Follow this tutorial:
android-tabs-with-fragments
2) Then apply the RelativeLayout change that Leaudro suggested above (apply the layout props to all FrameLayouts).
You can also add an ImageView to the tab.xml in item #1 and get a very iPhone like look to the tabs.
Here is a screenshot of what I'm working on right now. I still have some work to do, mainly make a selector for the icons and ensure equal horizontal distribution, but you get the idea.
In my case, I'm using fragments, but the same principals should apply to a standard tab view.
Not sure if it will work for all versions of Android (especially those with custom UI stuff), but I was able to remove the gray bar at the bottom by adding
android:layout_marginBottom="-3dp"
to the TabWidget XML...
For all those of you that try to remove the separating line of the tabWidget, here is an example project (and its respective tutorial), that work great for customizing the tabs and thus removing problems when tabs are at bottom.
Eclipse Project: android-custom-tabs ;
Original explanation: blog;
Hope this helped.
There are two ways to display tabs at the bottom of a tab activity.
Using relative layout
Using Layout_weight attribute
Please check the link for more details.
This may not be exactly what you're looking for (it's not an "easy" solution to send your Tabs to the bottom of the screen) but is nevertheless an interesting alternative solution I would like to flag to you :
ScrollableTabHost is designed to behave like TabHost, but with an additional scrollview to fit more items ...
maybe digging into this open-source project you'll find an answer to your question. If I see anything easier I'll come back to you.
I was having the same problem with android tabs when trying to place them on the bottom of the screen. My scenario was to not use a layout file and create the tabs in code, I was also looking to fire activities from each tab which seemed a bit too complex using other approaches so, here is the sample code to overcome the problem:
adding-tabs-in-android-and-placing-them
Yes, see: link, but he used xml layouts, not activities to create new tab, so put his xml code (set paddingTop for FrameLayout - 0px) and then write the code:
public class SomeActivity extends ActivityGroup {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
tab_host.setup(this.getLocalActivityManager());
TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
ts1.setIndicator("tab1");
ts1.setContent(new Intent(this, Registration.class));
tab_host.addTab(ts1);
TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
ts2.setIndicator("tab2");
ts2.setContent(new Intent(this, Login.class));
tab_host.addTab(ts2);
TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
ts3.setIndicator("tab3");
ts3.setContent(new Intent(this, Registration.class));
tab_host.addTab(ts3);
tab_host.setCurrentTab(0);
}
}
I recomend use this code for stable work, it optimized for nested fragments in tab (for example nested MapFragment) and tested on "do not keep activities": https://stackoverflow.com/a/23150258/2765497

Android: Tabs at the BOTTOM

I've seen some chatter about this, but nothing definite.
Is there a way to put the tabs in a TabWidget to the bottom of the screen?
If so, how?
I've tried the following, but didn't work:
a) setting the tabwidget below the framelayout
b) setting the tabwidget's gravity to "bottom"
Thanks!
llappall
Here's the simplest, most robust, and scalable solution to get tabs on the bottom of the screen.
In your vertical LinearLayout, put the FrameLayout above the TabWidget
Set layout_height to wrap_content on both FrameLayout and TabWidget
Set FrameLayout's android:layout_weight="1"
Set TabWidget's android:layout_weight="0" (0 is default, but for emphasis, readability, etc)
Set TabWidget's android:layout_marginBottom="-4dp" (to remove the bottom divider)
Full code:
<?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">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="-4dp"/>
</LinearLayout>
</TabHost>
Try it ;)
Just watch the content of the FrameLayout(#id/tabcontent), because I don't know how it will handle in case of scrolling... In my case it works because I used ListView as the content of my tabs. :)
Hope it helps.
<?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">
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#android:id/tabs" />
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
There is a way to remove the line.
1) Follow this tutorial:
android-tabs-with-fragments
2) Then apply the RelativeLayout change that Leaudro suggested above (apply the layout props to all FrameLayouts).
You can also add an ImageView to the tab.xml in item #1 and get a very iPhone like look to the tabs.
Here is a screenshot of what I'm working on right now. I still have some work to do, mainly make a selector for the icons and ensure equal horizontal distribution, but you get the idea.
In my case, I'm using fragments, but the same principals should apply to a standard tab view.
Not sure if it will work for all versions of Android (especially those with custom UI stuff), but I was able to remove the gray bar at the bottom by adding
android:layout_marginBottom="-3dp"
to the TabWidget XML...
For all those of you that try to remove the separating line of the tabWidget, here is an example project (and its respective tutorial), that work great for customizing the tabs and thus removing problems when tabs are at bottom.
Eclipse Project: android-custom-tabs ;
Original explanation: blog;
Hope this helped.
There are two ways to display tabs at the bottom of a tab activity.
Using relative layout
Using Layout_weight attribute
Please check the link for more details.
This may not be exactly what you're looking for (it's not an "easy" solution to send your Tabs to the bottom of the screen) but is nevertheless an interesting alternative solution I would like to flag to you :
ScrollableTabHost is designed to behave like TabHost, but with an additional scrollview to fit more items ...
maybe digging into this open-source project you'll find an answer to your question. If I see anything easier I'll come back to you.
I was having the same problem with android tabs when trying to place them on the bottom of the screen. My scenario was to not use a layout file and create the tabs in code, I was also looking to fire activities from each tab which seemed a bit too complex using other approaches so, here is the sample code to overcome the problem:
adding-tabs-in-android-and-placing-them
Yes, see: link, but he used xml layouts, not activities to create new tab, so put his xml code (set paddingTop for FrameLayout - 0px) and then write the code:
public class SomeActivity extends ActivityGroup {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
tab_host.setup(this.getLocalActivityManager());
TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
ts1.setIndicator("tab1");
ts1.setContent(new Intent(this, Registration.class));
tab_host.addTab(ts1);
TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
ts2.setIndicator("tab2");
ts2.setContent(new Intent(this, Login.class));
tab_host.addTab(ts2);
TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
ts3.setIndicator("tab3");
ts3.setContent(new Intent(this, Registration.class));
tab_host.addTab(ts3);
tab_host.setCurrentTab(0);
}
}
I recomend use this code for stable work, it optimized for nested fragments in tab (for example nested MapFragment) and tested on "do not keep activities": https://stackoverflow.com/a/23150258/2765497

Categories

Resources