Things to know about Android findViewByID before you start coding!

When you open Android studio and start building up your User Interface (UI) in activity_main.xml file and later code stuff in Java file, findViewByID is one most common method that you would’ve called in onCreate() method of MainActivity.Java File. In this blog, I’ll teach you everything that you should know about findViewByID and how to code it!

What is findViewByID?

Things to know about Android findViewByID before you start coding!

findViewByID is a commonly used method for Android App development in Android Studio programmed in Java or Kotlin Programming Language.

It’s an Android view object used to get a reference to the UI components specified in the XML layout file. So how does it references a variable in Java file to a component in the XML file?

The simple answer to this is the R file. R is an automatically generated file by Android Studio (even after you delete it, its automatically generated!).

findViewByID finds the ID of a particular component in the R file and then looks for the related view. It must be done in the onCreate method when the Main Activity is called (i.e, when the app is started!)

The code of a particular view component in the R looks like this:

public static final int actionMenuTextColor=0x7f02000f;

The code to reference a Button View from activity_main.xml file is as follows:

Button button = (Button) findViewById(;

The button variable in the file is a reference to Button view component built-in activity_main.xml file.

The code for findViewByID in AppCompatActivity.Java file is as follows:

public <T extends View> T findViewById(@IdRes int id) {
    return getDelegate().findViewById(id);

Android ButterKnife Zelezny

The problem with findViewByID is that the code has to be repeated everytime you built a view component in the XML file. To avoid this duplication of work in your project you can use a simple plugin by Avast Antivirus team known as Android ButterKnife Zelezny. Its a Plugin built for generating ButterKnife injections from selected layout XML in activities, fragments or adapters.  You can see the tutorial on the image below.

Things to know about Android findViewByID before you start coding!

Download from the plugin from the link here.

You can get your hands on findViewByID by trying building some of these Android Projects:

  1. Build your own Android Calculator App
  2. Learn to Build Android Weather App
  3. Build your own Android Piano App
  4. Learn to Build Android Web Browser