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.
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.
Please try this
tabLayout.getTabAt(0).setIcon(R.drawable.selector);
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
Please try this
tabLayout.getTabAt(0).setIcon(R.drawable.selector);
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) {
}
});
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
please Check these question you will get your answer
TabLayout selected Tab icon is not selected on start up:
Change icon and title color when selected in android design library TabLayout
© 2022 - 2024 — McMap. All rights reserved.