In this post you will learn how to send data from one activity to another in android,
we will use intent to pass data from one activity to another in android.
Intents are very useful in term of communicating between activities
or between application and OS.
So let’s start our tutorial by creating new project in android studio
Basically in this tutorial we will get text from our edittext on button click and then we
will set that text to our textview, after setting the text we will pass the textview value to another
activity in our android application.
So first thing first create a new empty activity in your project name it ‘SecondActivity’ like below
Now open your activity_main.xml and paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"> <EditText android:id="@+id/etText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:ems="10" android:inputType="textPersonName" android:hint="some text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnGetText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="get text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/etText" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="TextView" android:textSize="40sp" android:textColor="@color/black" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnGetText" /> <Button android:id="@+id/btnPassText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="32dp" android:text="send text" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout> |
We need to initialize all these views in our java file, to do so open your MainActivity.java file and paste the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MainActivity extends AppCompatActivity { EditText etText; TextView textView; Button btnGetText, btnPassText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etText = findViewById(R.id.etText); textView = findViewById(R.id.textView); btnGetText = findViewById(R.id.btnGetText); btnPassText = findViewById(R.id.btnPassText); btnGetText.setOnClickListener(view -> { String ourInput = etText.getText().toString(); textView.setText(ourInput); }); btnPassText.setOnClickListener(view -> { String outPut = textView.getText().toString(); Intent intent = new Intent(MainActivity.this,SecondActivity.class); intent.putExtra("myText", outPut); startActivity(intent); }); } } |
In the above code we are getting text value from our edittext and setting it to our textview and then on the button
click we are getting text value from our textview and passing it to another activity using intent extras.
Now we will handle our intent extras in our newly created SecondActivity.
Open activity_second.xml file and paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".SecondActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="SoftCoding" android:textSize="50sp" android:textColor="@color/black" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> |
In our SecondActivity.java we will handle our intent extras coming from MainActivity.java, so open your SecondActivity.java
and paste the following code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class SecondActivity extends AppCompatActivity { TextView textView; Bundle intentExtras; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); textView = findViewById(R.id.textView); intentExtras = getIntent().getExtras(); if (intentExtras != null) { String myText = intentExtras.getString("myText"); textView.setText(myText); } } } |
The above is using Bundle class to get intent extras from MainActivity and then we are setting the intent extra value to our textview.
That’s it our application is ready to send data from one activity to another in android, just run your application and pass some data
if you have any question then comment below I will answer it.