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 trialShariq Shaikh
13,945 PointsAction bar Disappears when I extend ListActivity, how do recreate the Action Bar without extending Action Bar Activity?
I'm starting a new project and the blank activity I created was extending Action Bar Activity. I decided to have it instead have extend ListActivity. When I do this and run the app the Action Bar Disappears. I'm assuming this has to do with the fact that I am no longer extending ActionBarActivity. Can someone explain to me how to utilize the action bar without extending ActionBarActivity? Here is my code for the main activity:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
6 Answers
Shariq Shaikh
13,945 PointsFigured it out, in the layout file you need to pick a theme that defaults the action bar, two of those themes include holo light and holo dark.
Ian Z
14,584 Pointsthis project is so annoying, why arent there project files for android studio, why dont the teachers notes explain a fix?!?! I seriously would have had like 20k more points if i hadnt wasted all my time being stuck on this action bar stuff!!
i tried extending ActionbarActivity and then doing: protected ListView mListview = (ListView) findViewById(R.id.receipientsListView);
but then i cant do onListItemClick method or mListview.onListItemClick
so i went back to doing extend ListActivity but now setupActionBar(); gives cannot resovle method
what do i do?
Ian Z
14,584 Pointstreehouse dosent realise 99% of us have no idea how to code yet, we rely on these videos being extremely precise and accurate so we can watch ben code as if we were sitting next to him asking him questions
Slava Fleer
6,086 Pointsstill didn't understand how to return action bar in emulator
Ben Jakuben
Treehouse TeacherSlava Fleer , is this still an issue for you? If so, let me know the details of your issue and what you are trying to use.
- Is your Activity extending ActionBarActivity?
- What is the theme listed in your styles.xml file (in res/values)?
Slava Fleer
6,086 Pointsto Ben: Hi. Yes. I still has the issue. 1) public class MainListActivity extends ListActivity 2) style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"
Ben Jakuben
Treehouse TeacherOkay, gotcha. This is a tricky situation because of the appcompat library that is automatically included. That's where ActionBarActivity comes from.
If you need to use the appcompat library for some reason, then you need to use an ActionBarActivity and use a ListFragment instead, which takes a little additional setup, like this: http://stackoverflow.com/a/20528156/475217
You may be able to get rid of appcompat, though. If you're targeting 4.0 and above then you might not need it at all. Try deleting the line in your build.gradle file that includes the appcompat library, like this:
compile 'com.android.support:appcompat-v7:21.0.3'
Then use a ListActivity, and you'll need to change the theme in your styles file to something like this:
<style name="AppTheme" parent="@android:style/Theme.DeviceDefault.Light.DarkActionBar">
Removing appcompat has a few other minor consequences, but probably nothing of importance. It just depends on your app. For a basic simple app, you will also need to change app:showAsAction="never"
to android:showAsAction="never"
because the app
namespace is for appcompat stuff.
Things get confusing with the backwards compatibility stuff. Hope this helps one way or the other!
Jared M
3,680 PointsI have the same issue and when I tried Ben's answer (where you delete the support library) it messed up the MainActivity a LOT. ViewPager didn't exist and a couple other imports didn't either.
Beatriz Macedo
9,903 PointsI tried removing the appcompat but then I got an error at the ViewPager declaration, how do I fix that?
Sam Coles
Courses Plus Student 13,887 PointsIan z - did you manage to fix this problem in the end? I am struggling with this at the moment. Any advice would be appreciated. I have tried working through the solutions that Ben has suggested but it still doesn't seem to work.
I totally agree with you regarding this project. The videos are too outdated which makes the project very frustrating, just when you seem to be making some progress you come across another issue with the code and spend twice as much time trying to find the solution.
Shariq - I also tried your solution but it doesn't work either - maybe I am doing it wrong. Could you copy/paste the code you input?
Ben - is there any plan for this project to be updated on Android Studio? If so are there any dates of when this will be available? This is an extremely big project and there is a lot to get through. These outdated videos are just making it harder for us students to learn.
Stephen Holland
449 Pointsye same here total mess when i did them changes as well
Jacob Bergdahl
29,118 PointsMy solution was to simply ignore the ActionBar for now and make a nice-looking button instead. It works just as well. Later, you can try using Toolbar, but a button works just as well.
Oron Ben Zvi
14,041 PointsOron Ben Zvi
14,041 PointsI also don't see action bar in ListActivity (Android Studio 21st SDK) in opposed to the BlogReader in eclipse as Ben shows, would love to get Ben Jakuben input on this.