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

Trying to share string

I am making a offshoot of the Fun Facts app and I was looking to add a share feature too the app. So far I believe the problem is coming from it being in the onCreateMenu method. Here is what I have:

public class main extends Activity {
    private Quotes mQuotes = new Quotes();
    private ColorChange mColorWheel = new ColorChange();
    public String shareQuote = "It sets it right away";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Declaration of all Variables
        final TextView quoteDisplay = (TextView) findViewById(R.id.quoteDisplay);
        final Button QuoteButton = (Button) findViewById(R.id.quoteButton);
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
        final Button dayButton = (Button) findViewById(R.id.dayButton);



        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Sets the text on the main screen
                String quote = mQuotes.getQuote();
                quoteDisplay.setText(quote);
                //Changes all of the color on background and text on buttons
                int color = mColorWheel.getColor();
                relativeLayout.setBackgroundColor(color);
                QuoteButton.setTextColor(color);
                dayButton.setTextColor(color);
                shareQuote = quote;


            }
        };
        QuoteButton.setOnClickListener(listener);

        //Quote of the day Listener
        View.OnClickListener dayListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }

        };
        dayButton.setOnClickListener(dayListener);

    }
    //Action bar code
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        //Share button support
        MenuItem shareItem = (MenuItem) menu.findItem(R.id.menu_item_share);

        ShareActionProvider mShare = (ShareActionProvider) shareItem.getActionProvider();

        //setting up the share
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        mShare.setShareIntent(shareIntent);
        //Sending the actual output to the other apps
        shareIntent.putExtra(Intent.EXTRA_TEXT, shareQuote);

        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();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

I have the shareQuote variable as a string that I would like to share, but it seems to just put what the string was initialized to and not what shareQuote is updated to. BTW shareQuote updates every time the main button is pressed. Any help would be awesome, I have been stuck for a long time now. Thanks a lot guys!!

edit: I updated to the whole java file

Just out of curiosity, could you change this line:

shareQuote = quote;

To:

shareQuote = new String(quote);

2 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

You want to share your quote in the onOptionsItemSelected() method, which is triggered when you tap on an item in the Action Bar. onCreateOptionsMenu() is only called when the menu is first created, which is why you get locked into the initial quote.

Check out this video for help in setting up a share button: http://teamtreehouse.com/library/build-a-blog-reader-android-app/using-intents-to-display-and-share-posts/easy-sharing-with-intents-2

Thank you Ben, I had not gotten that far yet. Thank you! -Jess

It is hard to tell from this code how shareQuote is updated.