Amir Sadegh
Amir Sadegh

Reputation: 127

Get textfield content

I am new to android developing. In an XML file I have 2 numeric text fields and a button, by pressing the button I want to get the content of textfields that user entered and use it in another class, I know I should use button.OnClickListener method but what to put in this function?

Upvotes: 0

Views: 79

Answers (3)

Shobhit Puri
Shobhit Puri

Reputation: 26017

I think if you break your problem step by step and Google that, you'll be able to get you answers. Its all a matter of putting things together.

Questions you can ask yourself:

  1. How to get data from TextView?

  2. How to send it to another activity?

  3. how to receive the data in another activity?

For the sake of convenience, here is something that can help you:

Inside that function (button.OnClickListener) try to get the text from TextView like:

TextView tv1 = (TextView)findViewById(R.id.tv1);
String input1 = tv1.getText().toString(); //tv1 is textView
TextView tv2 = (TextView)findViewById(R.id.tv2);
String input2 = tv2.getText().toString();

Then you can transfer that text to another activity using intent:

Intent i = new Intent(CurrentClassName.this, DestinationClass.class);
i.putExtra("text1", input1); //include the strings that you got from textView
i.putExtra("text2", input2);
startActivity(i); 

Inside the destination activity you can extract these strings in onCreate() like:

Intent intent = getIntent();
String text1 = intent.getExtras().getString("text1");
String text2 = intent.getExtras().getString("text2");

Hope this helps.

Upvotes: 4

Kirill Kulakov
Kirill Kulakov

Reputation: 10255

The method SetOnClickListner of Button takes an instance of View.OnClickListener, this instance is usually implemented anonymously like so:

button.setOnClickListner(new View.OnClickListner(){
   public void onClick(View v){
      //code which runs when action performed
   }
}

inside this listener you can implement your logic, for instance adding up the two numbers

String s1 = editText1.getText();
String s2 = editText2.getText();
int sum = Integer.parseInt(s1) + Integer.parseInt(s2);

You can look at Shobhit Puri's for more details

see:

listeneres

View.OnClickListner

Upvotes: 0

kalelien
kalelien

Reputation: 432

Make sure you are setting android:id for the text fields in the layout file. Once you have done that from within button.OnClickListener you can get find the text view by id and get the text as follows:

TextView tv = (TextView)findViewById(R.id.id_of_text_field);
String text = tv.getText().toString();

Then to transfer to another activity add the string to a Bundle and add the Bundle to the Intent as follows:

Intent intent = new Intent(this, NextActivity.class);
Bundle bundle = new Bundle();

bundle.putString(KEY_TO_FIND_STRING, text);
intent.putExtras(bundle);
startActivity(intent);

Then in your new activity you get the string from the bundle as follows:

Bundle b = getIntent().getExtras();
String text = null;

if( b != null ){
     text = b.getString(KEY_TO_FIND_STRING);
}

Upvotes: 0

Related Questions