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(R.id.btn1);
 
The button variable in the MainActivity.java 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:

@Override
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

Comments

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…

Python Turtle Example: Flappy Bird Development in under 10 mins!

Python Turtle Example | Develop Flappy Bird Clone in 10 mins In the following tutorial, we have used following Python libraries:
1. random: To generate obstacles at random position. 2. turtle: To help provide directions to the player. 3. vector from freegames: To 2-dimensional vectors.
Before developing the game, here is a screenshot of what you'll be developing in 10 mins.
Steps to create Python Flappy Bird Game:
1. Open Pycharm and install 'freegames' package by going to Files > Settings > Project Interpreter. 2. Click on '+' sign on right and search for 'freegames' package. 3. Import packages in your project by writing following lines of code:
from random import * from turtle import * from freegames import vector 4. There are two variables required in the game, one for Bird object and another for an array of obstacles:
bird = vector(0, 0) balls = []
5. Create function for the player response on screen tap:
def tap(x, y): "Move bird up in res…

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…