Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Newbie: How to change tab font size?

I follow the android develops tutorial of tab layout to implement a simple tab layout.

Based on that tutorial, I got an question in my mind, that's how to change the tab font size??

I tried to change the tab font size by adding the attribute android:textSize="8dip" in <TabWidget ...> of the layout xml file :

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:textSize="8dip" 
/>

but it does not take any effect.

Anyone can provide the right way to change the font size on the tab?

like image 825
Leem Avatar asked Dec 04 '22 08:12

Leem


2 Answers

Here you go in your tabactivity use

Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = (TabHost) getTabHost(); // The activity TabHost

TabHost.TabSpec spec; // Resusable TabSpec for each tab
Typeface localTypeface1 = Typeface.createFromAsset(getAssets(),
        "fonts/arial.ttf");

Edit Starts

tabHost.getTabWidget().setBackgroundDrawable( getResources().getDrawable(R.drawable.bluenavbar));

Edit Ends

TextView txtTab = new TextView(this);
txtTab.setText(getString(R.string.top_news));
txtTab.setPadding(8, 9, 8, 9);
txtTab.setTextColor(Color.WHITE);
txtTab.setTextSize(14);
txtTab.setTypeface(localTypeface1);
txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
txtTab.setBackgroundResource(R.drawable.tab_news);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("topNews").setIndicator(txtTab).setContent(new Intent(this, TopNewsGroup.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
tabHost.addTab(spec);

Now you'll be able to change the color, size and typeface of the text

like image 137
Rahul Sharma Avatar answered Jan 06 '23 21:01

Rahul Sharma


if you want to achieve it you should inflate layout of tab.

    tabHost = getTabHost(); // The activity TabHost
    tabHost.setOnTabChangedListener(this);
    Resources res = getResources(); // Resource object to get Drawables
    tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
    TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
    TabSpec thirdTabSpec = tabHost.newTabSpec("tid3");
    TabSpec fourthTabSpec = tabHost.newTabSpec("tid4");
    TabSpec fifthTabSpec = tabHost.newTabSpec("tid5");

    viewCache[0] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[1] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[2] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[3] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);
    viewCache[4] = LayoutInflater.from(this).inflate(R.layout.tabs1, null);

    firstTabSpec.setIndicator(viewCache[0]);
    secondTabSpec.setIndicator(viewCache[1]);
    thirdTabSpec.setIndicator(viewCache[2]);
    fourthTabSpec.setIndicator(viewCache[3]);
    fifthTabSpec.setIndicator(viewCache[4]);

    firstTabSpec.setContent(new Intent(this, HomeTabActivityGroup.class));
    secondTabSpec
            .setContent(new Intent(this, ProfileTabActivityGroup.class));
    thirdTabSpec.setContent(new Intent(this,
            NotificationTabActivityGroup.class));
    fourthTabSpec.setContent(new Intent(this,
            FavoritesTabActivityGroup.class));
    fifthTabSpec
            .setContent(new Intent(this, MoreTabActivityGroupNew.class));

    tabHost.addTab(firstTabSpec);
    tabHost.addTab(secondTabSpec);
    tabHost.addTab(thirdTabSpec);
    tabHost.addTab(fourthTabSpec);
    tabHost.addTab(fifthTabSpec);

    currentTabvalue = tabHost.getCurrentTab();
    C2DMessaging.register(TennisAppActivity.mContext,
            "[email protected]");
    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {

        // tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
        switch (i) {
        case 0:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.home);
            break;
        case 1:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.profile);
            break;
        case 2:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.notifications);
            break;
        case 3:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.fav);
            break;
        case 4:
            tabHost.getTabWidget().getChildAt(i)
                    .setBackgroundResource(R.drawable.more);
            break;
        }
    }

//**************************************

This is the tab1.xml

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout android:id="@+id/LinearLayout01" 
 android:layout_width="wrap_content" android:layout_height="wrap_content"
  xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center">
 <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content"  android:layout_height="50dip"></ImageView>
   </LinearLayout>

Your should put a testview in the place of image view and settest text size property according to you. I hope this is help.

like image 27
DynamicMind Avatar answered Jan 06 '23 19:01

DynamicMind