Search code examples
javaandroidandroid-activityandroid-studioandroid-developer-api

How to display the EditText fields text in the TextView


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

Solution

  • Simply do this:

    if(topText.getText()!=null){
    top.setText(topText.getText().toString());
    }
    if(bottomtext.getText()!=null){
    bottom.setText(bottomtext.getText().toString());
    }