Reputation: 79
I made a Tic-tac-toe game, I currently have 3 activities. I want to pass edit text string values to another activity for later to use as names.
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Input Player Name"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editTextName2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Input Player 2 Name"
android:inputType="textPersonName" />
I have two edit texts for two names and I want to pass those names as strings to another activity to use them as names.
if (firstPlayer.contains(1) && firstPlayer.contains(2) && firstPlayer.contains(3)) {
winnerPlayer = 1
score++
scoreText.text = "Player 1: $score"
}
if (secondPlayer.contains(1) && secondPlayer.contains(2) && secondPlayer.contains(3)) {
winnerPlayer = 2
score2++
scoreText2.text = "Player 2: $score2"
}
as you can see I have manually inputted Player 1
and Player 2
. I'm trying to replace them with name1 and name2 which are strings that have been inputted before. I wrote some code with intent but could get it to work. App just crashed and crashed.
Upvotes: 2
Views: 261
Reputation: 765
Intent intent = new Intent(getApplicationContext(), AnotherActivity.class);
String string1 = editTextName.getText();
String string2 = editTextName2.getText();
intent.putExtra("string1", string1);
intent.putExtra("string2", string2);
startActivity(intent);
and get it in the AnotherActivity.class like below
String string1 = getIntent().getStringExtra("string1");
String string2 = getIntent().getStringExtra("string2");
Upvotes: 1