Reputation: 78
I am building a Tab Layout with custom text and icon. The tab layout is setup with viewpager. I followed this reference for setting custom view. https://developer.android.com/reference/android/support/design/widget/TabLayout.Tab.html#setCustomView(android.view.View)
But the text and icon are not updating when I set with setText() and setIcon().
custom_tab.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:textColor="#ffffff"
android:textSize="10dp" />
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
MainActivity.java - setUpTabIcons()
private void setupTabIcons() {
private int[] tabIcons = {
R.drawable.ic_art_track_black_24dp,
R.drawable.ic_notifications_black_24dp,
R.drawable.ic_chat_bubble_black_24dp,
R.drawable.ic_person_add_black_24dp
};
for (int i = 0; i < 4; i++) {
tabLayout.getTabAt(i).setCustomView(R.layout.custom_tab);
tabLayout.getTabAt(i).setIcon(tabIcons[i]);
tabLayout.getTabAt(i).setText("Random");
}
}
Am I doing it wrong? Any alternative method will be appreciated.
Upvotes: 1
Views: 1367
Reputation: 76
For your TextView element you should use android:id="@android:id/text1"
this will be the proper int value (16908308). The same for the icon - id should be '@android:id/icon'
Upvotes: 0
Reputation: 162
private void setupTabs() {
String[] titles = getResources().getStringArray(R.array.titles);
int[] icons = ----
for (int i = 0; i < titles.length; i++) {
if (mTabLayout != null) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null)
tab.setCustomView(prepareTabView(titles[i], icons[i]));
}
}
}
private View prepareTabView(String title, int icon) {
View view = View.inflate(getContext(), R.layout.list_item_tab, null);
TextView tabTitle = (TextView) view.findViewById(R.id.tab_title);
ImageView tabIcon = (ImageView ) view.findViewById(R.id.tab_icon);
tabTitle.setText(title);
tabIcon.setImageResouce(icon);
return view;
}
Upvotes: 1