How to Use getExtra and putExtra in Android for String Data
Use getExtra and putExtra
You want to get some information like name, surname, email when user login on Android, and show on second activity these information , we can use getExtra and putExtra . There are a lot of method for getting information from first to second activity, I try to explain getting with getExtra and putExtra on getting user name and surname to show second activity, let ’s start,
Firstly I created two label(TextView) for definition to name and surname and EditText for enter name and surname to users on
activity_main.xml
,Then we created new class under
src
folder and under the package nameSecondActivity.java
, and under the/res/layout/
folder createdsecondactivity.xml
.For definite and connect between main and second activity open
AndroidManifest.xml
and create newactivity
for second .
1 2 3 4 5 6 7 8 9 |
|
- After that we need to create button on
activtiy_main.xml
after click this button goes the second activity .
1 2 3 4 5 6 7 8 9 |
|
On MainActivity.java
1 2 3 |
|
Now when you click on Sign in
button, you should go to second activity,
- After you need to add TextView to
secondactivity.xml
1 2 3 4 5 6 7 8 |
|
Now we can getting to username and surname from EditTexts and show instead of TextView on Second Activity
.
Firstly open MainActivity.java
and write below
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
After write above on SecondActivity
1 2 3 4 5 6 7 8 9 10 11 12 |
|
That ’s it . Now you should enter your username and surname to editText after you can see your name and surname
on Second Activity
.
I hope helped to you.