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 trial

Android Build a Blog Reader Android App Using Intents to Display and Share Posts Easy Sharing with Intents

Priyesh Tungare
Priyesh Tungare
13,168 Points

Not able to get the Share Action icon in the Action bar

Hi All,

I followed the steps mentioned by Ben in the video for adding Share icon to the Blog Reader app. But when I run the app on emulator, the action bar has the Three Dots menu and the Share option inside it. And when tested on device, I don't see the three dots as well. The share option appears on click of the menu button on the device. (Tested on Samsung Galaxy Note 3).

How could I replace the Three dots menu on action bar with the Share Icon?

Thanks, Priyesh.

6 Answers

Harry James
Harry James
14,780 Points

I managed to find a fix for this problem after a lot of testing :P (Seriously, it took me ages for an easy fix).

Anyhow, I downloaded the project files from the penultimate video (Makes it sound a whole lot cooler) and then tested some things out and managed to find that the code differed.

Here is how to fix the problem:

CHANGE the code below:

// This code:
public class BlogWebViewActivity extends ActionBarActivity {
// Should become this code:
public class BlogWebViewActivity extends Activity {

Hope it helps! :)

Will Schwarz
Will Schwarz
2,795 Points

Can anyone explain why that works or is the issue?

Harry James
Harry James
14,780 Points

Hello,

The reason for this is because we inherit methods from different classes using the extends keyword. So, instead of writing the whole piece of code to display something in the action bar, we use a method that does most of the important work for us. This is explained in further detail at http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

So, what we're actually doing is reusing a method from the Activity class. Now, as there was no errors in the code, it leads me to think that ActionBarActivity and Activity both contain methods of the same name but both do different things. I'm not sure what method gets inherited (I'm writing this from my phone) but this method would do a different thing depending on which class we inherit (Or, if that method doesn't exist in the class, it would display an error).

Noah Schill
Noah Schill
10,020 Points

Hey Harry,

I did the fix, but it just got rid of the ActionBar completely :/

If you could help me out that would be great.

Here's my xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_share" android:title="@string/action_share" android:icon="@drawable/ic_action_share" android:showAsAction="always"> </item> </menu>

Much thanks,

-Noah

Pablo Rocha
Pablo Rocha
10,142 Points

please see my answer below, I had the same issue but my fix worked.

Harry James
Harry James
14,780 Points

** FIXED - See my separate solution **

I have the exact same problem. My blog_web_view.xml has exactly the code that you put in (Apart from the typo, haha) and my share icon is also not displaying. It was working before however.

XX Maybe it was ever since I added the second menu with the dark icon as well? XX - No, this isn't the case. After removing the second menu folder I still had the problem. XX It could possibly be the deletion of activity_main_list.xml - I'm going to investigate this now so, expect an update within the hour to this post. XX - Nope, it wasn't this either.

I'll take a look into it here and update my post if I find anything.

In case anyone wants to have a look, here are my files: | blog_web_view.xml - menu | blog_web_view.xml - menu-v11 | BlogWebViewActivity.java | MainListActivity.java |

Priyesh Tungare
Priyesh Tungare
13,168 Points

Hi Steve and Harry,

Thanks a lot for your time and help.

I was able to get the share icon in the action bar. I followed 2 posts to find my solution. First was Ben's answer to Steve post regarding the same topic.

From Ben's reply:

"Anyhow, there was a silly problem hiding your real problem. Follow these steps and you'll see what I mean:

1) Delete libs/android-support-v4.jar. There is one in the appcompat_v7 project, and having 2 versions causes problems. I don't know why the 2nd one gets pulled in sometimes.

2) Clean your project, and then you will see this error: BlogReader/res/menu/blog_web_view.xml:3: error: Error: No resource found that matches the given name (at 'title' with value '@string/action_share').

3) That means you need to add the following line to your strings.xml file: <string name="action_share">Share</string>"

I followed the 1st and 2nd step, which gave error in all of my eclipse projects.

To remove those error I followed the answer for the question on stackoverflow.com : [http://stackoverflow.com/questions/22769016/how-to-remove-support-library-appcompat-v7-eclipse]

I was able to get the Share icon as required by doing this steps.

I would surely try Harry's suggestion for replacing ActionBarActivity with ActionActivity.

Thanks, Priyesh

Harry James
Harry James
14,780 Points

Thanks for the mention :)

Glad to hear you got it fixed!

Hi Priyesh,

The menu icon is held in the drawable folders; have you got the ic_action_share.png file in there?

Inside the onCreateOptionsMenu method within BlogWebViewActivity.java you should have the line:

getMenuInflater().inflate(R.menu.blog_web_view, menu);

That brings the .xml file called blog_web_view.xml into the menu. Within that file, you should have a few lines, such as:

<item
    android:id="@+id/action_share"
    android:icon="@drawable/ic_action_share_dark"
    android:showAsAction="a;ways"
    android:title="@string/action_share">
</item>

Do you have all that in place? The menu item is called within method above - I suggest you start there to see if we can get to the bottom of the problem!

:-)

Steve.

Glad you got it sorted - sorry I couldn't help directly!!

Pablo Rocha
Pablo Rocha
10,142 Points

Make sure you have "always" as an app, not android.

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