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

Viewing the message in a new activity

I've made the ribbit app able to send and receive text messages. I made it so that when you click on the received text message it opens in a new activity. But i'm not sure how to view the message in the new activity now. Is anyone able to help me with this? This is part of my inbox fragment in onListItemClick:

switch (messageType) { case ParseConstants.TYPE_TEXT_MESSAGE: String messageText = message.getString(ParseConstants.KEY_TEXT);

            // View Text Message
            Toast.makeText(getActivity(), messageText, Toast.LENGTH_LONG).show();

            Intent textIntent = new Intent(getActivity(), ViewTextActivity.class);
            startActivity(textIntent);


            break;
        case ParseConstants.TYPE_IMAGE:
            ParseFile imageFile = message.getParseFile(ParseConstants.KEY_FILE);
            Uri imageFileUri = Uri.parse(imageFile.getUrl());

            // View Image
            Intent imageIntent = new Intent(getActivity(), ViewImageActivity.class);
            imageIntent.setData(imageFileUri);
            startActivity(imageIntent);
            break;
        case ParseConstants.TYPE_VIDEO:
            ParseFile videoFile = message.getParseFile(ParseConstants.KEY_FILE);
            Uri videoFileUri = Uri.parse(videoFile.getUrl());
            // View Video
            Intent videoIntent = new Intent(Intent.ACTION_VIEW, videoFileUri);
            videoIntent.setDataAndType(videoFileUri, "video/*");
            startActivity(videoIntent);
            break;
    }

1 Answer

Hello,

With how your code looks right now, you could just put the string in as an extra to the intent before calling startActivity and then use getStringExtra() in your new activity. If you need more assistance with this please let me know and I will glady help further.

Hello James, I'm still having trouble displaying the text. This is what I've added to my inboxFragment case ParseConstants.TYPE_TEXT_MESSAGE: String messageText = message.getString(ParseConstants.KEY_TEXT);

            // View Text Message
            Toast.makeText(getActivity(), messageText, Toast.LENGTH_LONG).show();
            Intent textIntent = new Intent(getActivity(), ViewTextActivity.class);
            textIntent.putExtra(messageText, ParseConstants.KEY_TEXT);
            startActivity(textIntent);


            break;

This is my activity to view the text message

public class ViewTextActivity extends Activity { protected String mMessageText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_text);
    TextView viewTextView = (TextView) findViewById(R.id.viewTextView);
    mMessageText = getIntent().getStringExtra(ParseConstants.KEY_TEXT);
    viewTextView.setText(mMessageText);





}

}

When I run the app and open the text message, the textView doesn't show. I'm not sure what I can do to make this work.

Thanks, Jordan

Hello,

There's a few things that could be causing a couple of problems. First, with putExtra takes the key, then the value. So that line should look like

textIntent.putExtra(ParseConstants.KEY_TEXT, messageText);

Second, could you verify that your TextView in activity_view_text.xml has an id of viewTextView?

If you're still having issues, can you post your activity_view_text.xml file? Also, are you at least swapping over to the new Activity and just not seeing any text?

Hello, I got it working now! The putExtra line of code was what was messing up. Thank you for helping me!