Adirai Maji Adirai Maji - 1 month ago 7
Android Question

I need to return EditText string to MainActivity as Toast how can I achieve that?

Okay This is what I'm trying to make. 3 Activities. MainActivity,ActivityA & ActivityB from Main activity I'm going to ActivityA or B by Intent (Using Button). There is an EditText & a Button on each Activities A&B.. I just want to Toast the String typed on the textbox to MainActivity when I press the button on activities A or B. I provided everything below. Please someone explain what did I do wrong in the code.

Main Activity.java

package com.training.threeactivities;
import android.app.Activity;
import android.content.DialogInterface;
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.Toast;
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button gotoA = (Button) findViewById(R.id.gotoA);
gotoA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentA = new Intent(MainActivity.this, ActivityA.class);
startActivity(intentA);
}
});
Button gotoB = (Button) findViewById(R.id.gotoB);
gotoB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentB = new Intent(MainActivity.this, ActivityB.class);
startActivity(intentB);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
// check if the request code is same as what is passed here it is 2
if (requestCode == 1) {
String MsgA = intent.getStringExtra("FrmA");
Toast.makeText(MainActivity.this, MsgA, Toast.LENGTH_LONG).show();
} else if (requestCode == 2) {
String MsgB = intent.getStringExtra("FrmB");
Toast.makeText(MainActivity.this, MsgB, Toast.LENGTH_LONG).show();
}
}
}


activity_main.xml

<TextView
android:id="@+id/txtm"
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="MAIN ACTIVITY"
android:textSize="30dp" />
<Button
android:id="@+id/gotoA"
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/txtm"
android:layout_marginTop="10dp"
android:textSize="50dp"
android:text="GOTO A" />
<Button
android:id="@+id/gotoB"
android:layout_below="@+id/gotoA"
android:layout_centerHorizontal="true"
android:layout_width="350dp"
android:layout_height="150dp"
android:layout_marginTop="10dp"
android:textSize="50dp"
android:text="GOTO B" />


ActivityA.java

public class ActivityA extends AppCompatActivity {
EditText txtA;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button btnA = (Button) findViewById(R.id.btnA);
btnA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtA = (EditText) findViewById(R.id.txtA);
String MsgFrmA = txtA.getText().toString().trim();
Intent intentA = new Intent();
intentA.putExtra("FrmA", MsgFrmA);
setResult(1, intentA);
finish();
}
});
}
}


activity_a.xml

<TextView
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ACTIVITY A"
android:textSize="30dp" />
<EditText
android:id="@+id/txtA"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="@+id/fa"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#ffffff"
android:hint="Type The Message" />
<Button
android:id="@+id/btnA"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="@+id/txtA"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="SEND" />


ActivityB.java

public class ActivityB extends AppCompatActivity {
EditText txtB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
Button btnB = (Button) findViewById(R.id.btnB);
btnB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtB = (EditText) findViewById(R.id.txtB);
String MsgFrmB = txtB.getText().toString().trim();
Intent intentB = new Intent();
intentB.putExtra("FrmB", MsgFrmB);
setResult(2, intentB);
finish();
}
});
}
}


activity_b.xml

<TextView
android:paddingTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="ACTIVITY B"
android:textSize="30dp" />
<EditText
android:id="@+id/txtB"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_below="@+id/fa"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#ffffff"
android:hint="Type The Message" />
<Button
android:id="@+id/btnB"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_below="@+id/txtB"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="SEND" />

Answer

use startActivityForResult from your main activity

Comments