How to send data to activities — Android Studio
Hi Readers,
Welcome to Android Grid Blog. In this tutorial I’ll show you “How to pass text to activities in Android Studio”. Via Intent we can pass data to activities. Without wasting time let’s start.
YouTube Video
Follow below steps
Step 1) Add below values in strings.xml-
<resources>
<string name="app_name">Send data to activity</string>
<string name="send_data">Send data to second activity</string>
<string name="type_your_message">Type your message here...</string>
<string name="fill_with_message">Please fill with your message.</string>
</resources>
Step 2) Create a new Android Studio project or choose an existing project to start our tutorial.
Step 3) You’ll get a MainActivity.java and activity_main.xml. Now create a new activity and name it SecondActivity.
Step 4) Type below code in activity_main.xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="example.devtips.senddatatoactivity.MainActivity">
<EditText
android:id="@+id/et_message"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="@string/type_your_message"
android:inputType="textMultiLine" />
<Button
android:id="@+id/btn_send_data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="@string/send_data" />
</RelativeLayout>
Step 5) Now add two variables in MainActivity.java-
private EditText etMessage;
private Button btnSendData;
Step 6) Now add below code in MainActivity.java in onCreate() method-
//--------
etMessage = (EditText) findViewById(R.id.et_message);
btnSendData = (Button) findViewById(R.id.btn_send_data);
btnSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!TextUtils.isEmpty(etMessage.getText().toString().trim())){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", etMessage.getText().toString().trim());
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, R.string.fill_with_message, Toast.LENGTH_SHORT).show();
}
}
});
//--------
Step 7) Open activity_second.xml and type below code in it-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="example.devtips.senddatatoactivity.SecondActivity">
<TextView
android:id="@+id/text_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="18sp" />
</RelativeLayout>
Step 8) Now open SecondActivity.java and two variables code in it-
private TextView textMessage;
private String intentMessage;
Step 9) In SecondActivity.java add below code in onCreate() method-
try {
intentMessage = getIntent().getStringExtra("message");
} catch (Exception e){
e.printStackTrace();
}
textMessage = (TextView) findViewById(R.id.text_message);
textMessage.setText(intentMessage);
Step 9) Build the project and test the app in an emulator or in a real device :)
Code Revision
Our MainActivity.java will look something like this-
package example.devtips.senddatatoactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText etMessage;
private Button btnSendData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etMessage = (EditText) findViewById(R.id.et_message);
btnSendData = (Button) findViewById(R.id.btn_send_data);
btnSendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!TextUtils.isEmpty(etMessage.getText().toString().trim())){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("message", etMessage.getText().toString().trim());
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, R.string.fill_with_message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
And our SecondActivity.java will look something like this-
package example.devtips.senddatatoactivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
private TextView textMessage;
private String intentMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
try {
intentMessage = getIntent().getStringExtra("message");
} catch (Exception e){
e.printStackTrace();
}
textMessage = (TextView) findViewById(R.id.text_message);
textMessage.setText(intentMessage);
}
}
Source Code
You might get some errors in the Source Code, you have to modify the gradle files.
Thanks for reading, please share this post. and subscribe to our YouTube channel —