Search code examples
androidfragment-tab-host

Change first tab icon


how to change tab icon which define in 1st fragment and i want to change it from 2nd fragment?

i assign tab to icon and title like below now i want to change 1st tab icon from another fragment

final TabWidget tabWidget = mTabHost.getTabWidget();
   for (int i = 0; i < tabWidget.getTabCount(); i++)
   {

        final View tab = tabWidget.getChildTabViewAt(i);

        final TextView title = (TextView) tab.findViewById(android.R.id.title);
        title.setTextSize(9);
        title.setTextColor(Color.WHITE);
        title.setGravity(Gravity.CENTER);

           if(i==0)
           {
            title.setCompoundDrawablesWithIntrinsicBounds(null, null,  null,  res.getDrawable(R.drawable.rsz_chat_blue));
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
               title.setText(""); // Landscape mode
            }
           }
           if(i==1)
           {
            title.setCompoundDrawablesWithIntrinsicBounds(null, null,  null,  res.getDrawable(R.drawable.rsz_contacts_blueg));
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
               title.setText(""); // Landscape mode
            }
           }
           if(i==2)
           {
            title.setCompoundDrawablesWithIntrinsicBounds(null, null,  null,  res.getDrawable(R.drawable.rsz_play_blue));
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
               title.setText(""); // Landscape mode
            }
           }
           if(i==3)
           {
            title.setCompoundDrawablesWithIntrinsicBounds(null, null,  null,  res.getDrawable(R.drawable.rsz_settings_blue));
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {
               title.setText(""); // Landscape mode
            }
           }


   }

Solution

  • in your second fragment do this:

    1)get tabWidget from you activity(getActivity().getTabWiget())

    2)View tab = tabWidget.getChildTabViewAt(1);

    3)find any view inside the tab and change it

    final ImageView iconView = (ImageView) tab.findViewById(android.R.id.icon); 
    iconView.setImageDrawable(mIcon);