How to customize tabs bar


I am using TabHost to display a tabs bar at the bottom of the screen. Every tab has the same width although text in each tab is different. However, some tabs have short text and other tabs have long text. I want the tabs that contain short text has small width and long-width tabs have long text.

By nana.cico asked on 2015-11-23
answer to this question

jacobcarl2016
By jacobcarl2016

i tried running this code but error occurred .






android app development companies in UK

2016-04-27 Reply

yuk.dara
By yuk.dara

First, you have to get an instance of TabWidget. From the instance, you are able to get reference to a tab in the tab bar using getChildAt(int index) method. To set width of a tab, you create a LayoutParams object. With the layout params object, you can specify the width, height, and weight of the tab. Then, you set the layout params object to the tab using setLayoutParams() method of a ViewGroup instance that references to it. If you want to change the font size, font name, or font color of the tab, you make a reference to the textview in the tab calling the getChildAt() method of the ViewGroup.
Here is an example. In the example code, there are three tabs. The first and second tabs have the width. The third tab has longer width. The first two tabs contain icons and the last one tab has text content.
 customize tab bar
Typeface font = Typeface.createFromAsset (context.getAssets(), "fontello.ttf");
//reference to TabHost
TabHost th=(TabHost)findViewById (android.R.id.tabhost);
th.setup();
// Create child tabs
TabHost.TabSpec tab1 = th.newTabSpec("DocumentsTab");
TabHost.TabSpec tab2 = th.newTabSpec("LibraryTab");
TabHost.TabSpec tab3 = th.newTabSpec("Moretab");
// Set labels, icons, and activities to open
tab1.setIndicator("Accounts", null);
tab1.setContent(new TContent());
tab2.setIndicator("Settings", null);
tab2.setContent(new TContent());
tab3.setIndicator("Powered by www.mybookloud.com", null);
tab3.setContent(new TContent());
// Add tabs to the TabHost
th.addTab(tab1);
th.addTab(tab2);
th.addTab(tab3);
th.setCurrentTab(0);
// Reference to TabWidget
TabWidget tw=(TabWidget)findViewById (android.R.id.tabs);
// Reference to the first tab
ViewGroup vg=(ViewGroup)tw.getChildAt(0);
// Create LayoutParams object
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(50, 70, 1);
// Set tab width, height and weigth
vg.setLayoutParams(params);
// Set font and text of the tab
TextView txt=(TextView)vg.getChildAt(1);
txt.setTypeface(font);
txt.setText("\uE810");
// Reference to the second tab
ViewGroup vg1=(ViewGroup)tw.getChildAt(1);
LinearLayout.LayoutParams params1=new LinearLayout.LayoutParams(50, 70, 1);
vg1.setLayoutParams(params1);
TextView txt1=(TextView)vg1.getChildAt(1);
txt1.setTypeface(font);
txt1.setText("\uE828");
LinearLayout.LayoutParams params2=new LinearLayout.LayoutParams(100, 70, 8);
ViewGroup vg2=(ViewGroup)tw.getChildAt(2);
vg2.setLayoutParams(params2);
vg2.setEnabled(false);


2015-11-23 Reply





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.