Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass edit text data in form of string to next activity?

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.

like image 800
Avi Dhiman Avatar asked May 31 '11 04:05

Avi Dhiman


2 Answers

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");

    }
}
like image 74
Squonk Avatar answered Oct 10 '22 21:10

Squonk


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);
like image 29
Andro Selva Avatar answered Oct 10 '22 22:10

Andro Selva