Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRoberts L
185 PointsChange tab name to icon
Hello. I'm trying to create Ribbit application. And I wanted to know how can I change tab name from Inbox of Friends to appropriate Icon.
If I try to write
mContext.getString(R.drawable.ic_action_place).toUpperCase(l);
then tab shows the tab name with the link but not the image.
Can someone help me?
2 Answers
Ben Jakuben
Treehouse TeacherI'll be covering this in the next project that updates the UI for Ribbit!
Your code from above is trying to get a String from a drawable resource, which won't work.
You want to set the icon for the tab using something like the following in MainActivity.onCreate():
actionBar.addTab(actionBar.newTab()
.setIcon(mSectionsPagerAdapter.getIcon(i))
.setTabListener(this));
Note that the setText()
method from this code in the Ribbit project is replaced with the setIcon()
method. I also added a getIcon() method to the adapter:
// from SectionsPagerAdapter.java
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return mContext.getString(R.string.title_section1).toUpperCase(l);
case 1:
return mContext.getString(R.string.title_section2).toUpperCase(l);
}
return null;
}
public int getIcon(int position) {
switch(position) {
case 0:
return R.drawable.ic_tab_inbox;
case 1:
return R.drawable.ic_tab_friends;
}
return R.drawable.ic_tab_inbox;
}
CANER DAΔLI
2,871 PointsHi Ben, What if we want to change the active tab icon? How can we refresh the touched tab's icon every time?
Thanks,