banner



How To Change Icon That Displays In Tab

first create a layout xml file that has the structure you desire for the tab equally an instance a unproblematic icon at the top of a text. like then: enter image description here

1. create the navigation tab layout xml: in layout folder > nav_tab.xml

          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/nav_tab"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:gravity="center_horizontal">      <ImageView         android:layout_width="wrap_content"         android:layout_height="@dimen/nav_icon"         android:scaleType="centerInside"         android:id="@+id/nav_icon"         android:layout_marginBottom="@dimen/tiny_padding"/>      <TextView         android:id="@+id/nav_label"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:fontFamily="@string/font_fontFamily_medium"         android:shadowColor="@android:colour/black"         android:textColor="@color/dark_grey"         android:textSize="@dimen/nav_tab_label_font_size"         tools:text="@string/nav_home" />  </LinearLayout>                  

give your layout and id to inflate, and give the ImageView and the TextView ids likewise to reference later after inflating the parent layout.


2. Define your icons in drawable folder, and labels in strings.xml file

and reference them by their resource id in array ordered as y'all wish your icons to appear:

          individual int[] navIcons = {         R.drawable.ico_home,         R.drawable.ico_search,         R.drawable.ico_notification,         R.drawable.ico_profile }; individual int[] navLabels = {         R.cord.nav_home,         R.string.nav_search,         R.string.nav_notifications,         R.cord.nav_profile }; // another resouces array for active state for the icon individual int[] navIconsActive = {         R.drawable.ico_home_red,         R.drawable.ico_search_red,         R.drawable.ico_notification_red,         R.drawable.ico_profile_red };                  

3. setup your TabLayout with your ViewerPager:

          TabLayout navigation = (TabLayout) findViewById(R.id.navigation); navigation.setupWithViewPager(mainView/* the viewer pager object*/);                  

now customization function:

          // loop through all navigation tabs for (int i = 0; i < navigation.getTabCount(); i++) {     // inflate the Parent LinearLayout Container for the tab     // from the layout nav_tab.xml file that nosotros created 'R.layout.nav_tab     LinearLayout tab = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.nav_tab, zilch);      // get child TextView and ImageView from this layout for the icon and characterization     TextView tab_label = (TextView) tab.findViewById(R.id.nav_label);     ImageView tab_icon = (ImageView) tab.findViewById(R.id.nav_icon);      // gear up the characterization text past getting the actual string value by its id     // by getting the actual resource value `getResources().getString(string_id)`     tab_label.setText(getResources().getString(navLabels[i]));      // set the abode to be agile at start     if(i == 0) {         tab_label.setTextColor(getResources().getColor(R.color.efent_color));         tab_icon.setImageResource(navIconsActive[i]);     } else {         tab_icon.setImageResource(navIcons[i]);     }      // finally publish this custom view to navigation tab     navigation.getTabAt(i).setCustomView(tab); }                  

---

Final touch to set an active state and become the icon and text color changed when the tab is selected:

you can keep to my respond here

change epitome and color of the text to the tab when selected

that will reach: enter image description here

Source: https://stackoverflow.com/questions/37833495/add-iconstext-to-tablayout

Posted by: smitharing1997.blogspot.com

0 Response to "How To Change Icon That Displays In Tab"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel