简体   繁体   中英

Android How To change Tab Icons on tab click

Android:

I am new to android I want to change tab icons on tab click. i have followed may tutorials that guide how to change tab icons via selecter xml file like

http://developer.android.com/resources/tutorials/views/hello-tabwidget.html http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

but in my cause why all these not work it change only first tab icons selected to unselected and unselected to selected but not work for all others where is the problem in my code please help me plz plz plz

my code is as follow

my drawable icons setting file is as follow with names as

becel_setting.xml under drawable folder

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- When selected, -->
    <item android:drawable="@drawable/menu_promenera_selected"
          android:state_selected="true"
          android:state_pressed="false" />

    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_promenera"
         />

    <!-- When selected, -->
    <item android:drawable="@drawable/menu_varaframsteg_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_varaframsteg"
         />

    <!-- When selected,  -->
    <item android:drawable="@drawable/menu_minapromenader_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected,-->
    <item android:drawable="@drawable/menu_minapromenader"
         />

    <!-- When selected,  -->
    <item android:drawable="@drawable/menu_information_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_information"
         />

</selector>

and my tab host host class is as follow

public class BECEL extends TabActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{
            setMenuTabs();
        }catch(Exception ex){
            String message = ex.getMessage();
        }
    }

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

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

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

        intent = new Intent(this, BecelFacebookConnection.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.becel_setting)).setContent(intent);  /**** xml file name becel_setting ******/
        tabHost.addTab(spec);        



        intent = new Intent(this, OurProgress.class);
        Drawable mySelector = getResources().getDrawable(R.drawable.menu_varaframsteg);
        spec = tabHost.newTabSpec("").setIndicator("",mySelector).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, MyWalks.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.menu_minapromenader)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, WalkInformations.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.menu_information)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}

Please help me where is problem in my code

you need to provide a unique string value for your each newTabSpec("") . for example tab1 , tab2 , tab3 and tab4 in the following example:

public class BECEL extends TabActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{
            setMenuTabs();
        }catch(Exception ex){
            String message = ex.getMessage();
        }
    }

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

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

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

        intent = new Intent(this, BecelFacebookConnection.class);

        spec = tabHost.newTabSpec("tab1").setIndicator("tab1",
        res.getDrawable(R.drawable.becel_setting)).setContent(intent);  /**** xml file name becel_setting ******/
        tabHost.addTab(spec);        



        intent = new Intent(this, OurProgress.class);
        Drawable mySelector = getResources().getDrawable(R.drawable.menu_varaframsteg);
        spec = tabHost.newTabSpec("tab2").setIndicator("tab2",mySelector).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, MyWalks.class);

        spec = tabHost.newTabSpec("tab3").setIndicator("tab3",
        res.getDrawable(R.drawable.menu_minapromenader)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, WalkInformations.class);

        spec = tabHost.newTabSpec("tab4").setIndicator("tab4",
        res.getDrawable(R.drawable.menu_information)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM