Change icon in Tablayout on selected and deselected state
Asked Answered
A

5

5

Sorry if this question have been asked before.

I want to change icon when it is selected in tab of tab layout. How can I do this using selector?.

I have two tabs in my application on selected state icon should change.

Almost answered 29/7, 2016 at 5:47 Comment(4)
Possible duplicate of TabLayout selected Tab icon is not selected on start up:Carangid
Pease try this tabLayout.getTabAt(0).setIcon(R.drawable.selector);Deconsecrate
Thanks @Deconsecrate it is workingAlmost
I will add this as answer pls acceptDeconsecrate
D
5

Please try this

tabLayout.getTabAt(0).setIcon(R.drawable.selector);
Deconsecrate answered 29/7, 2016 at 6:24 Comment(0)
G
6

1. create a custom tab selector- You need to add both state selected true and false

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/home_fill"
            android:state_selected="true"/>
        <item android:drawable="@drawable/home_line"
            android:state_selected="false"/>
    </selector>

2. Add the custom tab selector drawable as the icon for tabItem

<android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/footer"
        app:tabIndicatorColor="@color/female_colour">

        <android.support.design.widget.TabItem
            android:id="@+id/tab_home"
            android:layout_width="@dimen/_25dp"
            android:layout_height="@dimen/_25dp"
            android:padding="@dimen/_4dp"
            android:icon="@drawable/tab_home"
            />
 </android.support.design.widget.TabLayout>

Code is Tested on Android version 7.1.1

Glaswegian answered 16/4, 2018 at 9:54 Comment(0)
D
5

Please try this

tabLayout.getTabAt(0).setIcon(R.drawable.selector);
Deconsecrate answered 29/7, 2016 at 6:24 Comment(0)
S
3
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.setIcon(selectedImageResources[tab.getPosition()]);
                getSupportActionBar().setTitle(pageTitles[tab.getPosition()]);
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setIcon(imageResources[tab.getPosition()]);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
Sukkoth answered 29/7, 2016 at 5:54 Comment(0)
K
2

To make tab selector and deselector you can use this way

1.Create Custom view and Inflate it:

private View getTabView(int imgDrawable) {
        View view = getLayoutInflater().inflate(R.layout.tab_view, null);
        ImageView imgTab = (ImageView) view.findViewById(R.id.imgTab);
        imgTab.setImageDrawable(getResources().getDrawable(imgDrawable));

        return view;
    }

2.Create drawable Selector

tab_home_selector.xml

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/ic_home_selected" android:state_selected="true" />
        <item android:drawable="@drawable/ic_home_deselected" />
    </selector>

3.Insert in tab:

tabDashboardLayout = (TabLayout) findViewById(R.id.tabDashboardLayout);        
        //Adding the tabs using addTab() method
        View tabView = getTabView(R.drawable.tab_home_selector);;
        tabDashboardLayout.addTab(tabDashboardLayout.newTab().setCustomView(tabView));

For individual tab you to have create individual drawable selector and add to tab

Keeley answered 29/7, 2016 at 6:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.