-1

How to set text in the Text View by using button on click both are present in the same Activity?

I have already used the Text View on using the same Text View the Stopped working

Richard
  • 560
  • 1
  • 8
  • 17
vinay
  • 11
  • 3

2 Answers2

0

Use the setter setText() in the button's on click listener.

myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v)     {

        myTextView.setText("new text");

    }
});
Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27
0

Here is the code that can help you with this.

Button button;
TextView textView;

button = (Button) findViewById(R.id.main_button);
textView = (TextView) findViewById(R.id.main_text);
// set the text view inside the button's OnClickListener() method

button.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v){
      textView.setText("Hello World");
   }
});
Aditya
  • 1,172
  • 11
  • 32