12

I am developing an android application in which i have taken two buttons and one edit text box. i want to pass the data of edit text box in from of string to the next activity on click of one of the buttons, how can i pass the text to the next activity and receive that text in the new launched activity so could use the text in that.

my code for first activity is

EditText Urlis=(EditText)findViewById(R.id.entry);
final Button button = (Button) findViewById(R.id.ok);
final Intent i=new Intent(this , RSSReder.class);
final String choice=Urlis.getText().toString();

i.putExtra("key", choice);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startActivity(i);
    }    
});  

and for called activity is

public class RSSReder extends Activity implements OnItemClickListener {

    public String RSSFEEDOFCHOICE;
    public final String tag = "RSSReader";
    private RSSFed feed = null;

    /** Called when the activity is first created. */

    public void onCreate(Bundle abc) {
        super.onCreate(abc);
        setContentView(R.layout.next1);

        Intent i = getIntent();
        RSSFEEDOFCHOICE =i.getStringExtra("key");

        // go get our feed!
        feed = getFeed(RSSFEEDOFCHOICE);

        // display UI
        UpdateDisplay();

    }
}

is there anything i need to change or remove.

Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
  • 1
    Try putting "String choice=Urlis.getText().toString();" inside the "onClick()" method. Also put "i.putExtra("key", choice);" in "onClick()" after it. Where you get the text at the moment, the chances are that you are setting 'choice' to be a blank String. You need to get the text when the user presses the Button to start the new Activity. – Squonk May 31 '11 at 07:27
  • thanx Mister my code application is working it was really helpful – Avi Kumar May 31 '11 at 07:40

4 Answers4

16

After you have used setContentView(...) you need to reference your EditText and get the text such as...

EditText et = (EditText) findViewById(R.id.my_edit_text);
String theText = et.getText().toString();

To pass it to another Activity you use an Intent. Example...

Intent i = new Intent(this, MyNewActivity.class);
i.putExtra("text_label", theText);
startActivity(i);

In the new Activity (in onCreate()), you get the Intent and retrieve the String...

public class MyNewActivity extends Activity {

    String uriString;

    @Override
    protected void onCreate(...) {

        ...

        Intent i = getIntent();
        uriString = i.getStringExtra("text_label");

    }
}
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • +1, Its nice. Mister has mentioned about Sending and Receiving data as well. – Paresh Mayani May 31 '11 at 04:18
  • 2
    `String theText = et.getText();` would throw a compile time error since `getText()` returns an `Editable` object. You have to call the `toString()` method as well. – Octavian Helm May 31 '11 at 05:11
  • 1
    @Octavian: Right you are sir! It's early in the morning here and I hadn't had my first cup of coffee. Error corrected, thanks for pointing it out. – Squonk May 31 '11 at 05:18
  • Thanx Mister i passed the string and it worked but now the problem is that the text of edit box is a url which i am passing i want it to be public is there a way i can get it before oncreate because in on create it removes the public modifier from it and the site is not accessed it is because the url is a rss feed which is to be passed further for parsing. – Avi Kumar May 31 '11 at 05:43
  • @Avi: So just make the string an instance variable and it will be accessible by all methods in your Activity. See the last code block in my answer. – Squonk May 31 '11 at 05:59
  • Mister my code is not giving the desired output and it is showing no error i am giving the code in my question if you could suggest me something please – Avi Kumar May 31 '11 at 07:01
3

Inside your Button's onClick Listener try the following,

String str=editText.getEditableText().toString();

Now use your intent,

Intent intent=new Intent(this,nextActivity.this);
intent.putExtra("editText_value",str);
startActivity(intent);
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
2

You can use intents for the purpose. Here's a tutorial for the same.

Also check How to pass the values from one activity to previous activity

Reading the contents of a String on Button click.

 EditText mText = (EditText)findViewById(R.id.edittext1);

Button mButton = (Button)findViewById(R.id.button1);

mButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                          String text = mText.getText.toString();
                          Intent intent = new Intent(this, Activity2.class);
                          intent.putExtra("key", text);
                          startActivity(intent);
                    }
                });
Community
  • 1
  • 1
Primal Pappachan
  • 25,857
  • 22
  • 67
  • 84
2

You have to use Intent to pass data to the next activity.

Intent intent = new Intent(CurrentActivity.this,NextActivity.class);
intent.putExtra("sampleString", "youstringdata");

In NextActivity:

String sampleData = getIntent().getExtras().getLong("sampleString");

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Swapna
  • 2,245
  • 2
  • 19
  • 22