Android tabHost

I have 2 questions regarding tabHost: I created a tabHost with 2 tabs and for the tab names I use setIndicator (TextView) (I work with api level 4) my background header is white. I use the header selector to choose between diff images for the header.

  • I want the title text to be in bold only when selected / pressed. I was not able to do this using the selector that I have. can i do this at all? the idea is that in cases I use drawable a, I want the text to be bold. other cases are not in bold. same question regarding textColor.

  • it looks like an error - when the tab opens first, the text on the selected tab (the one I used in tabHost.setCurrentTab (tabId)) does not appear at all. after the first press / focus / focus, any other item looks good. any idea why and how to solve it?

early

on tabActivity -

TextView title1 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL); TextView title2 = new TextView(MainActivity.getInstnace(), null, android.graphics.Typeface.NORMAL); title1.setText("teb11 title"); title1.setBackgroundResource(R.drawable.tabtitle); title1.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab1), null, null, null); title2.setText("tab22 title"); title2.setBackgroundResource(R.drawable.tabtitle); title2.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.tab2), null, null, null); TabSpec tab1 = mTabHost.newTabSpec("tab1").setIndicator(title1).setContent(R.id.list1); TabSpec tab2 = mTabHost.newTabSpec("tab2").setIndicator(title2).setContent(R.id.list2); mTabHost.addTab(tab1); mTabHost.addTab(tab2); mTabHost.setCurrentTab(0); 

tab1.xml selector

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/iconselect"/> <item android:state_pressed="true" android:drawable="@drawable/iconselect"/> <item android:drawable="@drawable/icon"/> </selector> 

selector for tabTitle

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/tabselected"/> <item android:state_selected="true" android:drawable="@drawable/tab" /> <item android:state_focused="true" android:drawable="@drawable/tab" /> </selector> 
+4
source share
4 answers

For your question number 1:
On the TabsAdapter use onPageSelected(int position) as follows:

  public void onPageSelected(int position) { //your logic here fixTitleText(); } 

and

  private void fixTitleText() { for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) { View view = mTabHost.getTabWidget().getChildAt(i); TextView tv = (TextView) view.findViewById(android.R.id.title); tv.setTextColor(getResources().getColor(R.drawable.text_selector)); tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); tv.setTypeface(null, Typeface.BOLD); } } 

just notice that this code is for HONEYCOMB and above, before that the hierarchy of the tab host representations is a little different

+1
source

Can I see your XML layout or can you only do this with Java commands. In any case, I would like to see more than just some pseudo-code in order to actually work on this.

This will be either your main.xml file or the OnCreate () function. IF you set your layout through XML, main.xml should have content similar to:

 <?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"> <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"> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff"> </RelativeLayout> <RelativeLayout android:id="@+id/RelativeLayout02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffffff"> </RelativeLayout> </FrameLayout> </LinearLayout> </TabHost> 

This can be done completely using Java commands, but especially when installing # 2, we most likely will need to learn more about your code. As part of the OnCreate () function, I would recommend initializing all the materials you want to display. Otherwise, if it is not in your main.xml and it is not initialized, it will not be shown.

0
source

this 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"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="60px" android:background="@drawable/headerbk" android:paddingTop="12px" android:orientation="vertical" android:gravity="center_horizontal"> </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ListView android:id="@+id/list1" android:background="@drawable/bk" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:scrollbars="vertical" android:divider="@drawable/separationline" android:cacheColorHint="#00000000" android:footerDividersEnabled="false" android:listSelector="@drawable/selected" /> <ListView android:id="@+id/list2" android:background="@drawable/bk" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:scrollbars="vertical" android:divider="@drawable/separationline" android:cacheColorHint="#00000000" android:footerDividersEnabled="false" android:listSelector="@drawable/selected" /> </FrameLayout> </LinearLayout> </TabHost> 
0
source

If bold is required,

There may be different ways, but I did it ...

to create a custom TextView, and then handle the touch event. How bold when you act down and set a normal action.

0
source

All Articles