Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12705

How to pass EditText to another activity?

Intent intent = new Intent(this,Name.class);
intent.putExtra("key", et.getText().toString());
startActivity(intent);

Intent intent = getIntent();
String str = intent.getStringExtra("key");
tv.setText(str);

From using above code I can get a string value at other activity, but I need editText object at another activity.

can anyone give Idea ?

Thanks

Upvotes: 2

Views: 7209

Answers (3)

Barmaley
Barmaley

Reputation: 16363

Well, in spite of all yells about uselessness of sending EditText to Activity I would say it can be done. For instance declare your own class which extends EditText and also implement Parcelable interface

public class MyEditText extends EditText implements Parcelable
{
       //a lot of things to be done...
}

In this case you will be able easily send MyEditText as Parcelable to another Activity

Intent intent = new Intent(this,Name.class);     
MyEditText et=new MyEditText(this);
intent.putExtra("key", et);     
startActivity(intent);    

For sure utility of this approach is still disputable.

Upvotes: 0

user817129
user817129

Reputation: 522

Why not send the ID and then use findViewByid() on the receiver Activity? Hope this helps:

Intent intent = new Intent(); 
String name="textView";
int value=EditText.getID();
intent.putExtra(name, value);
setResult(RESULT_OK, intent);

On receiver Activity:

private static final int REQUEST_CODE=1;
private int id;
private EditText et;
private Intent i;

i = new Intent(this,Name.class);
startActivityForResult(intent, REQUEST_CODE);
et = (EditText)findViewById(id);

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK && requestCode == REQUEST_CODE)
    {
        if (data.hasExtra("textView"))
        {
            //Get the selected file.
            id = data.getExtras().getInt("textView");
        }
    }
}

Upvotes: 5

success_anil
success_anil

Reputation: 3658

Hmm .. using a Singleton static class you can achieve the same. Store the reference to your edittext in the SingleTon class in one activity and the retrieve it back in another activity and achieve what you want to do. Thats' one of the possible work around in Java for your problem.

As far as android SDK is concerned you can't pass a view obj reference in Intent to another activity.

Upvotes: 0

Related Questions