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 trialMUZ140584 Heredity Mbundire
4,979 PointsNow switch to the ArtistActivity.java file. This also has tabs, but we are going to use icons instead of text. Set the i
help
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
// Some code has been omitted for brevity!
public ActionBar mActionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
// Some code omitted for brevity...
for (int i = 0; i < pagerAdapter.getCount(); i++) {
ActionBar.Tab tab = mActionBar.newTab();
tab.setTabListener(this);
/*
* Add your code here to set the text!
*/
tab.setText(pagerAdapter.getPageTitle(i)); // changed this a little P to p
mActionBar.addTab(tab);
}
}
}
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class ArtistActivity extends FragmentActivity implements
ActionBar.TabListener {
// Some code has been omitted for brevity!
public ActionBar mActionBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_artist);
mActionBar = getActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
pagerAdapter pagerAdapter = new pagerAdapter(getSupportFragmentManager());
// Some code omitted for brevity...
for (int i = 0; i < pagerAdapter.getCount(); i++) {
tab.setTabListener(this);
/*
* Add your code here to set the icon!
*/
tab.setIcon(pagerAdapater.getIcon(i));
mActionBar.addTab(tab);
}
}
// These listener methods are intentionally blank
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { }
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { }
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { }
}
6 Answers
Steve Hunter
57,712 PointsHi,
The code here is pretty much the same for both tasks in the challenge.
In the first one you're setting the text - you've mainly got that right with your code; although you should probably use the instance of PagerAdapter rather than the class:
tab.setText(pagerAdapter.getPageTitle(i)); // first task code
In the next task, you just need to switch from Text to Icon, using the methods named in the question:
tab.setIcon(pagerAdapter.getIcon(i)); // second task code
That should see you through.
Steve.
MUZ140584 Heredity Mbundire
4,979 Pointsam not understand steve ts so hard am tried nd tried
Steve Hunter
57,712 PointsI'll amend the code above so that it is correct. Give me a minute!
Steve.
MUZ140584 Heredity Mbundire
4,979 Pointsowk
Steve Hunter
57,712 PointsRight - start the challenge again.
For each task, there's a comment in the code for where you need to write your answer.
I have commented the lines above as "First task code" and "second task code" - just paste those into the right part of the challenge.
Steve.
MUZ140584 Heredity Mbundire
4,979 Pointsthank yu steve you a such a bright