Messenger Activity in Android to Chat Process

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.   
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:id="@+id/mainLayout"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical" >  
  8.   
  9.   <LinearLayout  
  10.   
  11.         android:layout_width="match_parent"  
  12.         android:layout_height="wrap_content" >  
  13.   
  14.         <TextView  
  15.             android:layout_width="wrap_content"  
  16.             android:layout_height="wrap_content"  
  17.             android:text="@string/message_prompt"  
  18.             android:textAppearance="?android:attr/textAppearanceMedium" />  
  19.   
  20.         <EditText  
  21.             android:id="@+id/message_field"  
  22.             android:layout_width="match_parent"  
  23.             android:layout_height="wrap_content"  
  24.             android:ems="10"  
  25.             android:hint="@string/message_hint" >  
  26.             <requestFocus />  
  27.         </EditText>  
  28.   
  29.     </LinearLayout>  
  30.   
  31.     <Button  
  32.         android:layout_width="match_parent"  
  33.         android:layout_height="wrap_content"  
  34.         android:text="@string/add_message_button_label"  
  35.         android:onClick="SendMessege" />  
  36.   
  37. </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.  }  
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


Similar Articles