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.
newProject.jpg
Step 2
Write the following code in "values/string.xml":
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">MessengerActivity</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="menu_settings">Settings</string>
  6. <string name="title_activity_rotations_exercises">Rotations Exercises</string>
  7. <string name="message_prompt">Message:</string>
  8. <string name="font_size_prompt">Font size:</string>
  9. <string name="add_message_button_label">Send Messege</string>
  10. <string name="message_hint">Your Message</string>
  11. </resources>
Step 3
Write the following code in "activity_main.xml":
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/mainLayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content" >
  10. <TextView
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="@string/message_prompt"
  14. android:textAppearance="?android:attr/textAppearanceMedium" />
  15. <EditText
  16. android:id="@+id/message_field"
  17. android:layout_width="match_parent"
  18. android:layout_height="wrap_content"
  19. android:ems="10"
  20. android:hint="@string/message_hint" >
  21. <requestFocus />
  22. </EditText>
  23. </LinearLayout>
  24. <Button
  25. android:layout_width="match_parent"
  26. android:layout_height="wrap_content"
  27. android:text="@string/add_message_button_label"
  28. android:onClick="SendMessege" />
  29. </LinearLayout>
Step 4
Write the following code in MessengerActivity.java:
  1. package com.example.Messenger.Activity;
  2. import java.util.ArrayList;
  3. import com.message.sending.MainActivity.R;
  4. import android.annotation.SuppressLint;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.EditText;
  10. import android.widget.LinearLayout;
  11. import android.widget.TextView;
  12. public class MessengerActivity extends Activity
  13. {
  14. private LinearLayout mMainLayout;
  15. private EditText mMessageField;
  16. private ArrayList < String > mMessages = new ArrayList < String > ();
  17. @Override
  18. public void onCreate(Bundle savedInstanceState)
  19. {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22. mMainLayout = (LinearLayout) findViewById(R.id.mainLayout);
  23. mMessageField = (EditText) findViewById(R.id.message_field);
  24. }
  25. @Override
  26. protected void onSaveInstanceState(Bundle outState)
  27. {
  28. super.onSaveInstanceState(outState);
  29. outState.putStringArrayList("messages", mMessages);
  30. }
  31. @Override
  32. protected void onRestoreInstanceState(Bundle savedInstanceState)
  33. {
  34. super.onRestoreInstanceState(savedInstanceState);
  35. mMessages = savedInstanceState.getStringArrayList("messages");
  36. for (String message: mMessages) {}
  37. }
  38. @SuppressLint("NewApi")
  39. public void SendMessege(View clickedButton)
  40. {
  41. String message = mMessageField.getText().toString();
  42. if (!message.isEmpty()) {
  43. mMessages.add(message);
  44. }
  45. }
  46. private void SendMessege(String message)
  47. {
  48. TextView textV = new TextView(this);
  49. textV.setText(message);
  50. mMainLayout.addView(textV);
  51. }
  52. }
Step 5
Compile the project by "Android Virtual Device" and see the output.
Starting output window
output_display_1.jpg
Running output window
Display_output2.jpg