Skip to main content

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?

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. Learn to Build Android Weather App
  2. Build your own Android Piano App


Popular posts from this blog

How To Open PDF File in Android Application Example Included

Want to know How To Open PDF File in Android Application Example. To open a PDF File in Android Application, your app can take help from Free Android library available on Github.

Here are few apps made from PDF Library you can look at, click here to know more.

By using Android Library you are not only leveraging the hard work from another developer but also making it easier and quicker to develop an android app.
How To Open PDF File in Android Application Example  In this tutorial series, we will teach you how to open a pdf file in Android Application Example included with the help of Android PDFViewer library developed by Barteksc.

Get inspirtion from here: Top 7 PDF Download for Mobile Android Apps

Before I proceed further, here is the screenshot of the kind of PDF Viewer that’ll be created using Android Studio.

 How To Open PDF File in Android Application Example

The app will open a specified pdf file which will be copied in the assets folder of your Android studio project. I'v…

Open Weather Map API Example | Create Android Weather App

In this tutorial blog, using open weather map API example, we will create an Android weather app. The Android App will able to access Open weather map website server and access weather details of particular location.

The App will look similar to the image shown below. It will fetch real time weather of the location provided in MainActivity.Java file.

Software / Files used:Android Studio (Version 3.x) Files Edited / Created:activity_main.xmlMainActivity.JavaWeather.JavaAndroidManifest.xml Steps Involved in Open Weather Map API Example: Here are the Steps you need to follow on ‘Open Weather map API Example’ to create Android Weather App:

1. Open the Open Weather map API link provided here. Sign up for Current Weather Data API key here.

2. Once the key is received, save it to notepad or on stick notes.

3. Open Android Studio and create a new Project with name, ‘FIMC Weather App’, we will select an empty activity in the wizard and wait for Gradle build.

4. Open AndroidManifest.xml…

Android Live Wallpaper Tutorial (using Android Studio 3.x) | FaultInMyCode

The Android Live Wallpapers are the way to redesign your mobile background into something amazing animated GIF using different effects and motions. Many novice developers are making good money by Henceforth here is Android Live Wallpaper tutorial for you to build your own Live wallpaper and start earning today as a developer!

This tutorial is a part of our Blog series on ‘Roadway to Android Development Course’, its suitable for Beginners to programming as well as the android enthusiast to learn about Android development and build their own apps.

In this tutorial for beginners, you’ll learn how by using Android Studio 3.0 and by having basic knowledge of Java, we will teach you to build your own Live wallpaper for Android which you may later publish in the Google Play Store. We assume you have some experience with Android studio, if not please check how to build your own calculator app and how to create simple login app in Android Studio 3.x tutorial.
How to build Android …