0

In my Android application, I am using FragmentTabHost. I have added 3 tabs in the tabhost now my problem is I could not change the tab icon dynamically when choose each tab.

My requirement is if I select a tab other tabs icon should be changed, I got null pointer exception when try to change the tab icon. This is the line I got null pointer exception. the following code under onTabChanged method

ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
                    .findViewById(android.R.id.icon); 

v1 is return null (checked by if condition)

    public class MainActivity extends FragmentActivity implements
            OnTabChangeListener {

        private static final String TAB_1_TAG = "FindTab";
        private static final String TAB_2_TAG = "AddPopUpTab";
        private static final String TAB_3_TAG = "MyAccoutTab";

        private FragmentTabHost mTabHost;

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

        private void InitView() {
            mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
            mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_1_TAG), R.drawable.tab1),
                    Tab1Container.class, null);
            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_2_TAG), R.drawable.tab2),
                    Tab2Container.class, null);
            mTabHost.addTab(
                    setIndicator1(MainActivity.this,
                            mTabHost.newTabSpec(TAB_3_TAG), R.drawable.tab3),
                    Tab3Container.class, null);

            mTabHost.getTabWidget().setDividerDrawable(null);
            mTabHost.setOnTabChangedListener(MainActivity.this);

        }
    private TabSpec setIndicator1(Context ctx, TabSpec spec, int genresIcon) {
        View v = LayoutInflater.from(ctx).inflate(R.layout.tab_item, null);
        ImageView img = (ImageView) v.findViewById(R.id.img_tabtxt);
        img.setBackgroundResource(genresIcon);
        return spec.setIndicator(v);
    }

    @Override
    public void onTabChanged(String arg0) {
        Log.e("tab change string", arg0);
                    ImageView v1 = (ImageView) mTabHost.getTabWidget().getChildAt(0)
                    .findViewById(android.R.id.icon);
            if (v1 == null) {
                Log.e("v1", "null");  // Line 
            } else {
                Log.e("v1", " Not null");
            }

            if (getResources().getDrawable(R.drawable.tab3) == null) {
                Log.e("resource", "null");
            } else {
                Log.e("resource", "not null");
            }

            v1.setImageDrawable(getResources().getDrawable(R.drawable.tab3));

            ImageView v2 = (ImageView) mTabHost.getTabWidget().getChildAt(1)
                    .findViewById(android.R.id.icon);
            v2.setImageDrawable(getResources().getDrawable(R.drawable.tab1));

            ImageView v3 = (ImageView) mTabHost.getTabWidget().getChildAt(2)
                    .findViewById(android.R.id.icon);
            v3.setImageDrawable(getResources().getDrawable(R.drawable.tab2));
    }

    }

Layout for tab item:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/img_tabtxt"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_centerHorizontal="true" />

</RelativeLayout>
M.A.Murali
  • 9,988
  • 36
  • 105
  • 182

1 Answers1

0

I think you need to you Actionbar is good.

If you want using Action bar then. You need to try this

Change Tab icon in Action bar

Girish Patel
  • 1,270
  • 4
  • 16
  • 30