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.
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.
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.
Step 4
Choose the empty activity, then click Next.
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.
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
- <?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:orientation="vertical"
- >
- <EditText
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/message_text"
- android:hint="@string/your_message"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:hint="@string/button_text"
- android:onClick="sendmessage"
- />
- </LinearLayout>
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
- package ganeshannt.senddata;
-
- import android.content.Intent;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
-
- public class MainActivity extends AppCompatActivity {
-
- EditText message_text;
-
- public final static String MESSAGE_KEY ="ganeshannt.senddata.message_key";
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- public void sendmessage (View view)
- {
- message_text = (EditText) findViewById(R.id.message_text);
-
- String message = message_text.getText().toString();
-
- Intent intent= new Intent(this ,SecondActivity.class);
-
- intent.putExtra(MESSAGE_KEY,message);
-
- startActivity(intent);
- }}
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.
Step 9
Put the activity, layout, title name, and provide the hierarchical parent path. That means put your main activity path.
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
- package ganeshannt.senddata;
-
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.TextView;
-
- public class SecondActivity extends Activity {
-
- public final static String MESSAGE_KEY ="ganeshannt.senddata.message_key";
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Intent intent = getIntent();
- String message = intent.getStringExtra(MESSAGE_KEY);
- TextView textView = new TextView(this);
- textView.setTextSize(45);
- textView.setText(message);
- setContentView(textView);
-
- }
-
- }
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
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.design.widget.CoordinatorLayout 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"
- tools:context="ganeshannt.senddata.SecondActivity">
-
- <android.support.design.widget.AppBarLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:theme="@style/AppTheme.AppBarOverlay">
-
- <android.support.v7.widget.Toolbar
- android:id="@+id/toolbar"
- android:layout_width="match_parent"
- android:layout_height="?attr/actionBarSize"
- android:background="?attr/colorPrimary"
- app:popupTheme="@style/AppTheme.PopupOverlay" />
-
- </android.support.design.widget.AppBarLayout>
-
- <include layout="@layout/content_second" />
-
- </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
- <resources>
- <string name="app_name">SendData</string>
- <string name="your_message">Enter the message</string>
- <string name="button_text">Send</string>
- <string name="title_activity_second">SecondActivity</string>
- </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
- <activity android:name=".SecondActivity"></activity>
step 14
This is our UI of the application. Click the "Make Project" option.
Step 15
Run the application, then choose the virtual machine and click ok.
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.
Don’t forget to like and follow me. If you have any doubts, just comment below.