findViewById - Find a TextView using ID

How to Find a TextView using ID

added in API Level 1

findViewById: Look for a child TextView with the given id. If this view has a given id, return this TextView.

1. Add a TextView in your main.xml file.

2. Name the TextView, myTextView.

3. In the MainActivity.java file, add the TextView import to the imports section.

     import android.widget.TextView;

4. In MainActivity, java file, add the below highlighted code to the onCreate method. Where R.id.myTextView is the same as the android:id name in your main.xml file, and myTextView is the TextView name referred to in Java.

TextView myTextView = (TextView) findViewById(R.id.myTextView); 

5. Compile and run!

Resources: