2

How i can change the text color in android tab,anyone help me out:

when tab is selected the text color is red as u can see an image:

enter image description here

and when tab is unselected then the text will be white as mentioned in an image:

enter image description here

This is my Tab_Car_Shop.java class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    final TabHost mTabHost = getTabHost();
    createHomeTab(mTabHost);
//  createContactTab(mTabHost);
    createnotificationTab(mTabHost);
//  createEmergencyTab(mTabHost);
    createFavouritesTab(mTabHost);
//  createMoreTab(mTabHost);
    createProfileTab(mTabHost);

//      DealerEmergencyNumbers = getIntent().getExtras().getString("DealerEmergencyNumbers");


//       csHeader_logo = getIntent().getExtras().getString("csHeader_logo");




     getTabHost().setOnTabChangedListener(new OnTabChangeListener() {


        @Override
           public void onTabChanged(String tabId)
           {

               if (getTabHost().getCurrentTabTag().equals("tab_contact"))
               { 
                   UpdateList();

                   if(csSavedDealerID == null)
                       showMessage("To benefit from the full features of this app please fill in your profile.");
                   else
                       showCutomMessage("Would you like to call for emergency assistance"); 
                   final TabHost mTabHost = getTabHost();
                   mTabHost.setCurrentTab(tabClick);

               }
               tabClick = getTabHost().getCurrentTab();


             }
           });



}



public void switchTab(int tab)
 {
    getTabHost().setCurrentTab(tab);
}

public void createHomeTab(TabHost mTabHost)
{
    // Create Intent for My Info



    Intent intent_settings = new Intent(Tab_Car_Shop.this,Car_shop_group_activity.class);
    //intent_settings.putExtras(CarData);

    intent_settings.putExtra("csDealerID", csDealerID);
    intent_settings.putExtra("csMakeID","-1");
    intent_settings.putExtra("csMakeName","Carshop");
    intent_settings.putExtra("csImageUrl", "http://cmh.dealer-app.co.za/cmh_v1/images/home-cons/logo-carshop.png");
    intent_settings.putExtra("csHeader_logo", "http://cmh.dealer-app.co.za/cmh_v1/images/headers/carshop-header.png");
    intent_settings.putExtra("csAdvert","http://cmh.dealer-app.co.za/cmh_v1/images/advert/advert-carshop.gif");
    intent_settings.putExtra("UsedVehicleLabel","Carshop");
    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("DealerEmergencyNumbers","");


//  startActivity(intent_settings);

    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_home");        

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

        TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title.setText("Home");
        // for Home text will be red
    //  title.setTextColor(Color.parseColor("#FF0000"));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_home);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);


        int height = mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height;

        //Log.e("CMH", "tab Hight " + height);
}


//  public void createContactTab(TabHost mTabHost)
public void createnotificationTab(TabHost mTabHost)
{
    // Create Intent for My Info

    Intent intent_settings = new Intent(Tab_Car_Shop.this,
            Promotions.class);

    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("csHeader_logo", "http://cmh.dealer-app.co.za/cmh_v1/images/headers/carshop-header.png");
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_notifications");       

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

        TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title.setText("Notifications");
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_contact);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);
}

//  public void createEmergencyTab(TabHost mTabHost)
public void createFavouritesTab(TabHost mTabHost)
{
    // Create Intent for My Info


    Intent intent_settings = new Intent(Tab_Car_Shop.this,
            Favourities.class);

    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("csHeader_logo", "http://cmh.dealer-app.co.za/cmh_v1/images/headers/carshop-header.png");
    intent_settings.putExtra("Previous_Activity", "More_group_activity");
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_favourites");      

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

        TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title.setText("My favourites");
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_favourites);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);


}


//  public void createMoreTab(TabHost mTabHost)
public void createProfileTab(TabHost mTabHost)
{
    // Create Intent for My Info

    Intent intent_settings = new Intent(Tab_Car_Shop.this,
            MyProfile.class);

    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("csHeader_logo", "http://cmh.dealer- app.co.za/cmh_v1/images/headers/carshop-header.png");
    intent_settings.putExtra("Previous_Activity", "More_group_activity");
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_profile");     

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

        TextView title = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title.setText("My profile");
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_profile);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);



}

 public void showCutomMessage(String message)
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message).setCancelable(true).setPositiveButton(
                "Yes", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {

                        dialog.cancel();


                        if(csEmergencyNumber.length() <= 0 && emergencyNumberList.size() <= 0)
                        {
                            showMessage("No Emergency Number found.");
                         }
                         else if (csEmergencyNumber.length() > 0)
                         {
                             String url = "tel:" + csEmergencyNumber;
                             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                             startActivity(intent);
                         }
                         else
                         {
                             if (emergencyNumberList.size() == 1)
                             {
                                 String url = "tel:" + emergencyNumberList.get(0);
                                 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                                  startActivity(intent);
                             }
                             else
                             {
                                 showEmergencyNumberDialog();
                             }
                         }
                    }
                }).setNegativeButton(
                        "No", new DialogInterface.OnClickListener()
                        {
                            public void  onClick(DialogInterface dialog, int id)
                            {

                                dialog.cancel();

                            }
                        });


        AlertDialog alert = builder.create();
        alert.setMessage(message);


        alert.show();

    }

 public void showMessage(String message)
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setMessage(message).setCancelable(true).setPositiveButton(
                "OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {

                        dialog.cancel();
                    }
                });


        AlertDialog alert = builder.create();
        alert.setMessage(message);


        alert.show();

    }

 public void UpdateList()
 {
     try
        {
            emergencyNumberList.clear();
            jsonNumberArray = new  JSONArray(DealerEmergencyNumbers.toString());

            SharedPreferences app_preferences =  PreferenceManager.getDefaultSharedPreferences(Tab_Car_Shop.this);
            csSavedDealerID = app_preferences.getString("DealerID", null);

            for(int nJasonIndex = 0; nJasonIndex < jsonNumberArray.length(); nJasonIndex++)
            {
                JSONObject emergencyObj =jsonNumberArray.getJSONObject(nJasonIndex);
                String csNumber = emergencyObj.getString("EmergencyNumber");
                String csNumber2 = emergencyObj.getString("office_hours_num"); 
                /*Log.e("Usman String1", csNumber);
                Log.e("Usman String2", csNumber2);*/
                String csMakeDealerID = emergencyObj.getString("DealerID");
                if(csSavedDealerID != null && csSavedDealerID.equalsIgnoreCase(csMakeDealerID))
                {
                    csEmergencyNumber = csNumber;
                }
                else
                {
                    if(!csNumber.equals(""))
                        emergencyNumberList.add("After Hours: " + csNumber);
                    if(!csNumber2.equals(""))
                        emergencyNumberList.add("Office Hours: " + csNumber2); 
                }

            }

            if(emergencyNumberList.size() > 1)
            {
                myEmergencyAdapter =new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item,emergencyNumberList); 
                 myEmergencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            }




        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }

 void showEmergencyNumberDialog()
 {
     ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item, emergencyNumberList);

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("");
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int item)
         {
             if (item >= 0)
             {
                 String emergencyNumber = emergencyNumberList.get(item);
                 String[] parts = emergencyNumber.split(": ");
                 String number = parts[1]; // 034556
                 String url = "tel:" + number; 
                 Intent intent = new Intent(Intent.ACTION_CALL,  Uri.parse(url));
                 startActivity(intent);
             }

         }
     });

     AlertDialog alert = builder.create();
     alert.show();
 }


}

this is my tab_layout.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="0dip"
android:layout_height="50dp"
android:layout_weight="1"
android:background="@drawable/tab_indicator"
android:orientation="vertical"
android:padding="5dp" >

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/tab_title"
    style="?android:attr/tabWidgetStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:textSize="12sp" />

</RelativeLayout>

this is my tab_home.xml file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use this -->
<item android:drawable="@drawable/img_home_up_new"
      android:state_selected="true" >

</item>
<!-- When not selected, use that-->
<item android:drawable="@drawable/img_home_new"
    android:state_selected="false" />
</selector>

and this is my tab_indicator.xml file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Pressed -->
<item android:state_selected="true" android:state_pressed="true"  android:drawable="@drawable/img_bottombar_new" />
 <item android:state_selected="true" android:state_pressed="false"  android:drawable="@drawable/img_bottombar_new" />
<!--<item android:state_pressed="true" android:drawable="@drawable/bottom_bar_selected"  />-->
</selector>
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

2 Answers2

4
public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
    {
        View view = mTabHost.getTabWidget().getChildTabViewAt(i);
        if ( view != null ) {

            // get title text view
            TextView textView = (TextView)view. findViewById(R.id.tab_title);
            textView.setTextColor(Color.WHITE);
        }

    }
    title.setTextColor(Color.RED); 
}
3

Try this

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
    @Override
    public void onTabChanged(String id) {
        int tab = tabHost.getCurrentTab();
        View v = tabHost.getTabWidget().getChildAt(tab).setBackgroundColor(Color.RED);
    }
});

You can also see if this post helps.

Other alternative is Actionbar Style generator. You can specify your requirements, download zip and use in project.

To change text color, you can give this a try:

TextView tv = (TextView) tabWidget.getChildAt(i).findViewById(android.R.id.title);        
tv.setTextColor(Color.RED);

Edit:

You can use this logic in order to change color to White on unselect of a tabbar.

public void onTabChanged(String tabId) {
    // TODO Auto-generated method stub
    for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
    {
        View view = mTabHost.getTabWidget().getChildTabViewAt(i);
        if ( view != null ) {

            // get title text view
            TextView textView = (TextView) findViewById(R.id.tab_title);
            textView.setTextColor(Color.WHITE);
        }

    }
    title.setTextColor(Color.RED); 
}

Hope this helps.

Community
  • 1
  • 1
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • bro it's not working,it's give me the error like "Type mismatch: cannot convert from void to View".. – Farhan Shah Jun 23 '14 at 12:56
  • Try to remove `View v = ` and check if works @FarhanShah I just wrote it. Didn't get time to test, so. – MysticMagicϡ Jun 23 '14 at 12:57
  • and i dont want to change the tab bar color,i want to change the tab bar text color.. – Farhan Shah Jun 23 '14 at 12:57
  • bro kindly check my "public void createHomeTab(TabHost mTabHost)" method in this method i am successfully change the text color Red,but when i unselected the tab,then the text color will be still Red,and i want when i unselect the tab,then the text color will be white,hope u have get my point.. – Farhan Shah Jun 23 '14 at 13:05
  • this is my code in CreateHomeTab methos "title.setText("Home"); // for Home text will be red title.setTextColor(Color.parseColor("#FF0000")); ImageView icon = (ImageView) " – Farhan Shah Jun 23 '14 at 13:06
  • I got your point @FarhanShah . Editing my answer in a minute. – MysticMagicϡ Jun 23 '14 at 13:10
  • @FarhanShah check the edit. And [a reference link](http://www.technotalkative.com/android-change-tab-bar-background-color/). The link is for changing background color. You can change text color instead. :) – MysticMagicϡ Jun 23 '14 at 13:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/56114/discussion-between-purpledroid-and-farhan-shah). – MysticMagicϡ Jun 23 '14 at 13:29