findViewById - Find an ImageView using ID

How to Find an ImageView using ID

findViewbyId was added in API Level 1

findViewById: Look for a child ImageView with the given id. If this view has a given id, return this ImageView. The findViewById() method is available to all activities in Android. findViewById allows you to find a view inside an activity layout.

1. Name an ImageView myImageView.

2. Declare an ImageView, named myImageView.

3. In MainActivity, java file, add the below code to the onCreate method. Where R.id.myImageView is the same as the android:id name in your main.xml file, and myImageView is the ImageView name referred to in Java. This returns a ImageView View class and is casted to a ImageView type so you can work with it.

myImageView = (ImageView) findViewById(R.id.myImageView); 

4. Compile and run!

Resources: