Starting Another Activity - Activity not showing my message
Question: I'm new to android developing but have experience with python and java, so I
decided I want to make apps, thus i began the tutorial from
developer.android.com.

I'm in the Build your first app > Starting Another Activity section.

I made it to the final step without errors, BUT the message I type in is not
showing up in the second activity created when i press the send button!

(<https://developer.android.com/training/basics/firstapp/starting-
activity.html#DisplayMessage>) if you click that link to see the tutorial, and
you scroll down to the bottom, you should see that the message you type should
pop up, and with a bigger font size. The message i find when i type ANYTHING
is the small font, default "Hello world!"

Here's my MyActivity.java

    
    
        package com.example.android.myfirstapp;
    
    import android.app.Fragment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.content.Intent;
    import android.view.View;
    import android.widget.EditText;
    
    public class MyActivity extends AppCompatActivity {
        public final static String EXTRA_MESSAGE = "com.mycompany.myfirstapp.MESSAGE";
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
    }
    
    @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_my, 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);
    }
    
    /* Called when the user clicks the Send button */
    public void SendMessage(View view) //View must be the parameter of onClick, View is what is pressed.
    {
        // Do something in response
        Intent intent = new Intent(this, DisplayMessageActivity.class); //A Context as its first parameter (this is used because the Activity class is a subclass of Context)
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);
    }
    

}

Here is the DisplayMessageActivity.java

    
    
    package com.example.android.myfirstapp;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    
    public class DisplayMessageActivity extends AppCompatActivity {
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
    
        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);
    
        // Set the text view as the activity layout
        setContentView(textView);
    
        }
    
    @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);
    }
    

}

I'm not sure if you guys would need AndroidManifest.xml, activity_my.xml, or
strings.xml but it's better to be safe than sorry!

AndroidManifest.xml below:

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.myfirstapp" >
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayMessageActivity"
            android:label="@string/title_activity_display_message" >
        </activity>
    </application>
    
    </manifest>
    

activity_my.xml below:

    
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation = "horizontal">
    
    <EditText
    
        android:id = "@+id/edit_message"
        android:layout_weight = "1"
        android:layout_width = "0dp"
        android:layout_height = "wrap_content"
        android:hint = "@string/edit_message"
        />
    
    <Button
    
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:text = "@string/button_send"
        android:onClick = "SendMessage"
        />
    

strings.xml below:

    
    
    <resources>
    <string name="app_name">My First App!</string>
    <string name = "edit_message">Enter a Message!</string>
    <string name = "button_send">Send</string>
    
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="title_activity_display_message">DisplayMessageActivity</string>
    
    <string name="hello_world">Hello</string>
    
    </resources>
    

I apologize some of the things are not indented properly if they didn't indent
properly when I copy pasted them over here.

If you could help me, I would really appreciate it, so i can continue becoming
an android developer!

Again, the app is a simple type a message and send, to see your message pop up
with bigger font size in another activity. Though mine is only showing the
default, small-font-sized "Hello world!" when the new activity pops up when i
press send.

Thank you for your time, and sorry for the long post!


Answer: First of all, replace

    
    
    intent.putExtra(EXTRA_MESSAGE, message);
    

with

    
    
    intent.putExtra("EXTRA_MESSAGE", message);
    

in your `MyActivity.java`

as the first argument for `putExtra()` should be a String name, which is used
further in the application. Please refer the
[docs](<http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String>,
android.os.Bundle))

Now, in your `DisplayMessageActivity.java`

write your `onCreate()` as follows

    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Set your parent view
        setContentView(R.layout.disp_msg_layout);
    
        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra("EXTRA_MESSAGE");
    
        // Get the reference to the TextView and update it's content
        TextView textView = (TextView)findViewById(R.id.my_text_view);
        textView.setText(message);
    }
    

and declare an xml file named `disp_msg_layout` in your layouts and it should
be like

    
    
    <?xml version="1.0" encoding="utf-8"?>
    <TextView  xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:textSize="40sp"
               android:id="@+id/my_text_view"/>
    

