0

I have the following TextView object in an XML file:

<TextView
        android:id="@+id/display"
        android:text="Hello!" />

I'm trying to write Java code to replace the "Hello!" with other text. For the purposes of this question, it doesn't matter what that text is. I don't know how to edit the TextView text in a Java file, given the ID. Any help would be greatly appreciated. Thanks!

  • 2
    Does this answer your question? [Android - Set text to TextView](https://stackoverflow.com/questions/19452269/android-set-text-to-textview) – jeprubio Apr 26 '20 at 01:32

1 Answers1

1
//in your java class (activity)

class MainActivity extends ......{

    private TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //find your textView
    textView = (TextView) findViewById(R.id.display);

    //edit your text view

    textView.setText("What ever text you want to add");

    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22