Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Vsevolod Kaganovych
Vsevolod Kaganovych
3,801 Points

Can't add a new item in Adroid Menu

I did everything, that was advised here https://teamtreehouse.com/forum/android-menu-action-bar. But it doesn't work. I still cannot add an item.

4 Answers

Matheus G Oliveira
Matheus G Oliveira
9,682 Points

Hey Vsevolod,

Did you add a new item to the menu option over your res folder?

try checking out this documents

http://developer.android.com/guide/topics/ui/menus.html

Hope it helps

Harry James
Harry James
14,780 Points

It seems that this is a new issue coming out. I along with other users are having it too:

https://teamtreehouse.com/forum/not-able-to-get-the-share-action-icon-in-the-action-bar

Harry James
Harry James
14,780 Points

Hello again, if you check the link I provided earlier (https://teamtreehouse.com/forum/not-able-to-get-the-share-action-icon-in-the-action-bar), I have provided a fix there.

Hope it helps! :)

Vsevolod Kaganovych
Vsevolod Kaganovych
3,801 Points

Nope, it didn't help too. But thanks for your trying :)

Harry James
Harry James
14,780 Points

Ok. I'll still try and help you out. Can you make sure that your blog_web_view.xml file looks exactly like the code here?

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="always"
        android:title="@string/action_share">
    </item>

</menu>

And that the 3 methods in BlogWebViewActivity look like this:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_blog_web_view, 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 itemId = item.getItemId();
        if (itemId == R.id.action_share) {
            sharePost();
        }
        return super.onOptionsItemSelected(item);
    }

    private void sharePost() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl);
        startActivity(Intent.createChooser(shareIntent, getString(R.string.share_chooser_title)));
    }

Or, you can post your blog_web_view.xml and BlogWebViewActivity.java on here using a site like www.pastebin.com and I'll take a look and see if anything jumps out.

Vsevolod Kaganovych
Vsevolod Kaganovych
3,801 Points

This is BlogWebViewActivity

        @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.blog_web_view, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId == R.id.action_share) {
            sharePost();
        }

        return super.onOptionsItemSelected(item);
    }

    private void sharePost() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, mUrl);
        startActivity(Intent.createChooser(shareIntent, getString(R.string.share_chooser_title)));

    }

And this is blog_web_view.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="always"
        android:title="@string/action_share">
    </item>

</menu>

Sorry, didnt reply to you, little mistake.

Harry James
Harry James
14,780 Points

I don't see anything wrong with this code and no problem about the reply. I do it to sometimes :)

What emulator are you running on that is not allowing you to see the action bar? It will only be displayed on Android 3.0 (API 11) or above. If you have tried on Android 3.0 or above, I'd be happy to take a look at your Project Files (You can export them by right clicking on your project >> Export >> General >> File System and then share them using Dropbox or a similar service). Then, I'd be able to test it from my side and see if I get your problem as well.

Harry James
Harry James
14,780 Points

I'm going to be heading off for today now so I'll be able to help out tomorrow if you don't get your problem fixed. There'll probably be a few other forum members on right now in a different timezone who may be able to help out too so, hopefully someone may be able to fix the problem whilst I'm gone.

Best of luck!

Vsevolod Kaganovych
Vsevolod Kaganovych
3,801 Points

Thanks for you patience and help, It actually works today morning, don't know what happened. Answering your question, i'm using a Genymotion from the beggining.

Harry James
Harry James
14,780 Points

Haha. Glad to hear it's fixed. Eclipse must've just have been buggy.