how to pass string one activity to another in android studio
In this Tutorial, today learn how to pass string one activity to another in an android studio. there are sending and retrieve String to the intent data.
Sending String:
Intent intent = new Intent(getApplicationContext(), Second_Activity.class);
intent.putExtra("message", str);
startActivity(intent);
Retrieve String:
Intent intent = getIntent();
String str = intent.getStringExtra("message");
text.setText(str);
There is two methods on this Activity so let's start a code:
package com.akash.passstring;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText textBox;
Button passButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText)findViewById(R.id.textBox);
passButton = (Button)findViewById(R.id.passButton);
passButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = textBox.getText().toString();
Intent intent = new Intent(getApplicationContext(), Second_Activity.class);
intent.putExtra("message", str);
startActivity(intent);
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context="com.androidexample.First"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textBox"
android:hint="Enter Your Message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passButton"
android:text="Pass"/>
</LinearLayout>
package com.akash.passstring;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Second_Activity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_);
text = (TextView)findViewById(R.id.text);
Intent intent = getIntent();
String str = intent.getStringExtra("message");
text.setText(str);
}
}
In this Tutorial, today learn how to pass string one activity to another in an android studio. there are sending and retrieve String to the intent data.
Sending String:
Intent intent = new Intent(getApplicationContext(), Second_Activity.class);
intent.putExtra("message", str);
startActivity(intent);
Retrieve String:
Intent intent = getIntent();
String str = intent.getStringExtra("message");
text.setText(str);
There is two methods on this Activity so let's start a code:
1.MainActivity.java:
package com.akash.passstring;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText textBox;
Button passButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textBox = (EditText)findViewById(R.id.textBox);
passButton = (Button)findViewById(R.id.passButton);
passButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str = textBox.getText().toString();
Intent intent = new Intent(getApplicationContext(), Second_Activity.class);
intent.putExtra("message", str);
startActivity(intent);
}
});
}
}
2.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context="com.androidexample.First"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textBox"
android:hint="Enter Your Message"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/passButton"
android:text="Pass"/>
</LinearLayout>
3.Second_Activity.java:
package com.akash.passstring;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Second_Activity extends AppCompatActivity {
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_);
text = (TextView)findViewById(R.id.text);
Intent intent = getIntent();
String str = intent.getStringExtra("message");
text.setText(str);
}
}
4.activity_second_.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:id="@+id/text"/>
</LinearLayout>
5.AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.akash.passstring">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Second_Activity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments:
Post a Comment