0

I have an editText, How do I get the text typed in it by the user ?

example, this is my editText

<EditText
   android:id="@+id/editText3"
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:maxLength="10">
</EditText>

and the user typed Hi, in my next Activity how do I make if the user typed Hi show in the next Activity Hello.. etc if the user typed How are you next activity it shows I'm fine ?

What do I use? and is it related to Database ?

John Jared
  • 790
  • 3
  • 16
  • 40

3 Answers3

3

First, pick up the text in the current activity, i.e. the activity where the view hosts the EditText component.

EditText txt = (EditText)findViewById(R.id.editText3);
String input = txt.getText().toString();

Now, add it as an extra data and call your new activity.

Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("input",input);
startActivity(i);

In NewActivity inside onCreate() :

Intent i = getIntent();
String input = i.getStringExtra("input");

and, voila, you have the entered text in the new activity.

To set this to the new TextView :

TextView txtView = (TextView)findViewById(R.id.yourTextView);
txtView.setText(input);
Ayush
  • 41,754
  • 51
  • 164
  • 239
  • in `NewActivity`, change `String input = ...` to `final String input = ...` – Ayush Aug 19 '12 at 08:54
  • I get error from 'i.putExtra("input",input);' Cannot refer to a non-final variable input inside an inner class defined in a different method". and if I final the string input .. the newActivity gets blank – John Jared Aug 19 '12 at 08:58
1

The simplest way to go would be use Intent's getExtra() and putExtra() method, do a simple String comparison and return your results.

Here's something to get you started : How do I get extra data from intent on Android? and How do I compare strings in Java?.

But from what you have written, seems you are trying to make a chatting application wherein your application responds to the messages typed in by the user. For this, I would recommend you to use AIML. It's a long long shot, but worth it.

Community
  • 1
  • 1
Kazekage Gaara
  • 14,972
  • 14
  • 61
  • 108
1

Let's assume that your class's name is : Main

First declare a static public String :

public static String myText;

Now, in your OnCreate method type:

EditText et = (EditText) findViewById(R.id.editText3);

Finally, in you OnClick method type:

myText = getText().toString();
Intent x = new Intent(getApplicationContext(), NewActivity.class);
startActivity(x);

Now, in your next activity you can access the String only using :

Main.myText

For exemple:

TextView txt =(TextView) findViewById(R.id.textviewid);
txt.setText(Main.myText);
stanga bogdan
  • 724
  • 2
  • 8
  • 26