How To Send The Data One Activity To Another Activity In Android Applications

Introduction

 
This article is about the basics of chat application message transfer function. Android is one of the most popular operating systems for mobiles. I am sure all the chat applications use this function to transfer the message user activity to another user activity. I will show you how to send the data one activity to another activity in an android application using the Android studio. Android is the kernel-based operating system. It allows the users to modify the GUI components and source code.
 
Requirements
  • Android studio
  • Little bit XML and JAVA Knowledge.
  • Android Emulator (or) Android mobile
Download link (Android Studio): https://developer.android.com/studio/index.html
 

Steps should be followed

 
Carefully follow my steps to send the data from one activity to another activity in Android applications using Android Studio and I have included the source code below.
 
Step 1
 
Open Android Studio and start a new project.
 
android
 
Step 2
 
Put the application name and company domain. If you wish to use C++ for coding the project, mark the "Include C++ support", then click Next.
 
android
 
Step 3
 
Select the Android minimum SDK. After you chose the minimum SDK, it will show the approximate percentage of people who use that SDK. Then, click Next.
 
android
 
Step 4
 
Choose the empty activity, then click Next.
 
android
 
Step 5
 
Put the activity name and layout name. Android Studio basically takes the Java class name that you provide in the activity name and click Finish.
 
android
 
Step 6
 
Go to activity_main.xml and click the text button. This XML file contains the designing code for the Android app. In the activity_main.xml, copy and paste the below code.
 
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     android:orientation="vertical"  
  8.     >  
  9.     <EditText  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:id="@+id/message_text"  
  13.         android:hint="@string/your_message"  
  14.         />  
  15.     <Button  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:hint="@string/button_text"  
  19.         android:onClick="sendmessage"  
  20.         />  
  21. </LinearLayout>   
android
 
Step 7
 
Into the MainActivity.java, copy and paste the below code. Java programming is the back-end language for Android. Do not replace your package name otherwise, the app will not run. The below code contains my package name. 
 
MainActivity.java code
  1. package ganeshannt.senddata;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.EditText;  
  8.   
  9. public class MainActivity extends AppCompatActivity {  
  10.   
  11.     EditText message_text;  
  12.   
  13.     public final static String MESSAGE_KEY ="ganeshannt.senddata.message_key";  
  14.   
  15.     protected void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.activity_main);  
  18.     }  
  19.   
  20.         public void sendmessage (View view)  
  21.     {  
  22.       message_text = (EditText) findViewById(R.id.message_text);  
  23.   
  24.         String message = message_text.getText().toString();  
  25.   
  26.         Intent intent= new Intent(this ,SecondActivity.class);  
  27.   
  28.         intent.putExtra(MESSAGE_KEY,message);  
  29.   
  30.         startActivity(intent);  
  31.     }}  
Step 8
 
You need another activity for transferring the message from one activity to another. So, you create SecondActivity.java and second_layout.xml.New->activity -> basic activity.
 
android
 
Step 9
 
Put the activity, layout, title name, and provide the hierarchical parent path. That means put your main activity path.
 
android
 
Step 10
 
Into the SecondActivity.java, copy and paste the below code. Do not replace your package name otherwise, the app will not run. The below code contains my package name.
 
SecondActivity.java code
  1. package ganeshannt.senddata;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class SecondActivity extends Activity {  
  9.   
  10.     public final static String MESSAGE_KEY ="ganeshannt.senddata.message_key";  
  11.   
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         Intent intent = getIntent();  
  16.         String message = intent.getStringExtra(MESSAGE_KEY);  
  17.         TextView textView = new TextView(this);  
  18.         textView.setTextSize(45);  
  19.         textView.setText(message);  
  20.         setContentView(textView);  
  21.   
  22.          }  
  23.   
  24. }   
Step 11
 
Go to second_layout.xml, then click the text button. This XML file contains the designing code for the Android app. Into the second_layout.xml, copy and paste the below code.
 
second_layout.xml code 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="ganeshannt.senddata.SecondActivity">  
  8.   
  9.     <android.support.design.widget.AppBarLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:theme="@style/AppTheme.AppBarOverlay">  
  13.   
  14.         <android.support.v7.widget.Toolbar  
  15.             android:id="@+id/toolbar"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="?attr/actionBarSize"  
  18.             android:background="?attr/colorPrimary"  
  19.             app:popupTheme="@style/AppTheme.PopupOverlay" />  
  20.   
  21.     </android.support.design.widget.AppBarLayout>  
  22.   
  23.     <include layout="@layout/content_second" />  
  24.   
  25. </android.support.design.widget.CoordinatorLayout>   
Step 12
 
Go to the values folder, then click the strings.xml. Copy and paste the below code into the strings.xml file.
 
strings.xml code
  1. <resources>  
  2.     <string name="app_name">SendData</string>  
  3.     <string name="your_message">Enter the message</string>  
  4.     <string name="button_text">Send</string>  
  5.     <string name="title_activity_second">SecondActivity</string>  
  6. </resources>   
Step 13
 
Add the below code into the androidminifest.xml within the application tab. Do not replace all the code. Just copy and paste the below code only.
 
Androidminifest.xml 
  1. <activity android:name=".SecondActivity"></activity>  
step 14
 
This is our UI of the application. Click the "Make Project" option.
 
android
 
Step 15
 
Run the application, then choose the virtual machine and click ok.
 
android
 
Deliverables
 
Here, we have successfully sent the data from one activity to another activity in the Android application.
 
Put your message and click the send button. The message will show in another activity.
 
android
 
android
 
Don’t forget to like and follow me. If you have any doubts, just comment below.


Similar Articles