Messenger Activity in Android
I am going to tell you what Messenger is and how to create a Messenger Activity in an application in Android.
Also how to work with it when we are on chat.
Messenger is a Java class and we use this in Android by extending the Activity class.
This class allows us to implement message-based communication between processes
by creating a Messenger pointing to the handler in one process and handing that Messenger in another process.
Step 1
Create a new project by "File" -> "New" -> "Android Application Project". Its name is MessengerActivity.
Step 2
Write the following code in "values/string.xml":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">MessengerActivity</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_rotations_exercises">Rotations Exercises</string>
<string name="message_prompt">Message:</string>
<string name="font_size_prompt">Font size:</string>
<string name="add_message_button_label">Send Messege</string>
<string name="message_hint">Your Message</string>
</resources>
Step 3
Write the following code in "activity_main.xml":
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/message_prompt"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/message_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/message_hint" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_message_button_label"
android:onClick="SendMessege" />
</LinearLayout>
Step 4
Write the following code in MessengerActivity.java:
package com.example.Messenger.Activity;
import java.util.ArrayList;
import com.message.sending.MainActivity.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MessengerActivity extends Activity {
private LinearLayout mMainLayout;
private EditText mMessageField;
private ArrayList<String> mMessages = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMainLayout = (LinearLayout)findViewById(R.id.mainLayout);
mMessageField = (EditText)findViewById(R.id.message_field);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList("messages", mMessages);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mMessages = savedInstanceState.getStringArrayList("messages");
for(String message: mMessages) {
// SendMessege(message);
}
}
@SuppressLint("NewApi")
public void SendMessege(View clickedButton) {
String message = mMessageField.getText().toString();
if (!message.isEmpty()) {
mMessages.add(message);
}
}
private void SendMessege(String message) {
TextView textV = new TextView(this);
textV.setText(message);
mMainLayout.addView(textV);
}
}
Step 5
Compile the project by "Android Virtual Device" and see the output.
Starting output window
Running output window