-1

Of late I was just trying to create a mems geneator app, yes I had been following the new bostons and in that buckey created them using 2 fragments and here i just want to do in a single main activity, but I just can't figure out how to retrieve the text from the edit text field and set the text of Text view to it. I know it's pretty a newbie question but still I don't know how to code it so please help...

I had just imported some widgets,views,etc and done some modified the on create function and my on create function is:

public class MainActivity extends AppCompatActivity {

public static EditText topText;
public static EditText bottomtext;
public static TextView top;
public static TextView bottom;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    topText = (EditText) findViewById(R.id.topedit);
    bottomtext = (EditText) findViewById(R.id.bottomedit);
    top = (TextView) findViewById(R.id.top);
    bottom = (TextView) findViewById(R.id.bottom);
    Button myButton = (Button) findViewById(R.id.button);
    myButton.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick(View V) {
                    top.setText((CharSequence) topText);
                    bottom.setText((CharSequence) bottomtext);
                }
            }
    );
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Prudhvi Reddy
  • 117
  • 2
  • 17

4 Answers4

1

Simply do this:

if(topText.getText()!=null){
top.setText(topText.getText().toString());
}
if(bottomtext.getText()!=null){
bottom.setText(bottomtext.getText().toString());
}
Aakash
  • 5,181
  • 5
  • 20
  • 37
  • Also read full documentation of android before coming to the implementations, you will find almost everything in the documentation. You can refer to http://developer.android.com – Aakash Sep 17 '15 at 17:56
  • Thanks man it worked with just some small modification i.e inverting null and the topText.getText() and the same for bottom also like- if(null != topText.getText()){ top.setText(topText.getText().toString()); } if (null != bottomtext.getText()) { bottom.setText(bottomtext.getText().toString()); } – Prudhvi Reddy Sep 17 '15 at 18:01
  • glad to help, please mark as correct if my answer was helpful – Aakash Sep 17 '15 at 18:03
  • inverted like what? are you asking about changing topText.getText()!=null to null != topText.getText() ?? – Aakash Sep 17 '15 at 18:08
  • yes bro but just leave it,bcz at first it asked to invere it like i mention above but now it worked fine with out inverting also any way thanks for your help...!!! – Prudhvi Reddy Sep 17 '15 at 18:12
1

Try this to get text of the EditText field:

CharSequence text = topText.getText();

And set the text above for the textView:

top.setText(text);
Priya B
  • 96
  • 10
0

Use this short example to understand the concept

store=ret.getText().toString();

show.setText(store);

Explanation

ret: is the name of the edit text field;

store: is used to hold anything gotten from ret (text field)

show.setText(store) displays the result in the textview

james jelo4kul
  • 839
  • 4
  • 17
0

This question has already been answered:

"Use getText() on your EditText object".

Get Value of a Edit Text field

Next time do a quick search ;)

Community
  • 1
  • 1
MrMalt
  • 42
  • 7