---
# System prepended metadata

title: Mobile Application - Messenger

---

# Mobile Application - Messenger

# Messenger
activity_create_message.xml
<pre><code>
<?xml version="1.0" encoding="utf-8"?>
<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:padding="16dp"
    android:orientation="vertical"
    tools:context=".CreateMessageActivity"
    >
    <EditText
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="@string/hint"
        android:ems="10"
        />
    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:onClick="onSendMessage"
        android:text="@string/send"
        />
</LinearLayout>
</code></pre>

strings.xml
<pre><code>
<string name="send">Send Message</string>
<string name="hint">Enter a message</string>
</code></pre>

###### [hint] implements a public method "onSendMessage" of CreateMessageActivity class
<pre><code>
import android.content.Intent;

    public void onSendMessage(View view) { }
</code></pre>

CreateMessageActivity.java
<pre><code>
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_message);
    }

    public void onSendMessage(View view) {
        Intent intent = new Intent(this, ReceiveMessageActivity.class);
        startActivity(intent);
    }
</code></pre>
### Send Message from the activity (CreateMessageActivity) to other activity (ReceiveMessageActivity)
create a Empty activity and give a name "ReceiveMessageActivity.java"
activity_receive_message.xml
<pre><code>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical"
    tools:context=".ReceiveMessageActivity">
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
</code></pre>

### ReceiveMessageActivity.java
get the information of the intent
<pre><code>
public class ReceiveMessageActivity extends Activity {
    public static final String EXTRA_MESSAGE = "message";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive_message);
        Intent intent = getIntent();
        String messageText = intent.getStringExtra(EXTRA_MESSAGE);
        TextView messageView = (TextView) findViewById(R.id.message);
        messageView.setText(messageText);
    }
}
</code></pre>

### CreateMessageActivity.java
put extra message and send the intent
<pre><code>
public void onSendMessage(View view) {
        EditText messageView = (EditText) findViewById(R.id.message);
        String messageText = messageView.getText().toString();
        Intent intent = new Intent(this, ReceiveMessageActivity.class);
        intent.putExtra(ReceiveMessageActivity.EXTRA_MESSAGE, messageText);
        startActivity(intent);
    }
</code></pre>

# Trigger by Extra Activity with a action of the intent
<pre><code>
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, messageText);
</code></pre>
