Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does findViewById work?

package com.example.dell.helloworld;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showToast(View view)
    {
        Toast t= new Toast(this);
        LayoutInflater inflater=getLayoutInflater();
        View v=inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toastViewGroup));
        t.setView(v);
        t.show();


    }
}

From the android developer site findViewById searches for child views with give id.

In the above code, who is the parent view, whose children are being searched for given id?

like image 929
q126y Avatar asked Feb 12 '16 08:02

q126y


3 Answers

in the XML file when we add an element and set the properties to it we only set the values for these properties, now for each element, there is a class that draws it at the screen, this class has attributes with the same names of the properties in the XML file now using algorithms of read-write from files these values from the XML file are transmitted to the java file (class) of the element, and then the class draws that element on the screen.

Before the class of the element draws it, there is a superclass of all elements, the parent for all of them it is called View (every element in the activity is called view) this class has the basic properties of all elements and this class is the one which the properties from the XML file will be transmitted to it.

Method findViewById() returns an object of View type, this object holds the properties values then we need to cast it to the specific element, for example, TextView which is a class to draw the text view this class and all elements' classes are subclasses of View class so what we do is downcasting, that when this method returns the object of View type we downcast it to the element class

How it finds the properties? it finds the properties of the element using the id if the tag in the XML file first it searches in the XML file for the tag that holds the element's name and then it looks at the id if it is the id which it wants then it takes it otherwise it searches for another tag (with the same element name).

We give it the id by this approach. there is a class is called R (resources), this class has nested classes (id, string, colors) these classes have attributes from the same type and hold specific values, for instance, the id class it has attributes that stores each id of each element in the XML file, so when we want to give the method findViewById() the id we go to this class and tell it to enter id class and choose the id of the element we want.

ِِAnd the process goes that it enters to the XML file and look for the element that has this id and it takes the properties and passes them to the class view object and when it returns the object we downcast it to the element's class that we want to draw it and deal with it.

like image 179
Ahmad Abu Sultan Avatar answered Oct 20 '22 22:10

Ahmad Abu Sultan


The root view in an Activity is determined by setContentView(int layoutId) or setContentView(View rootView).

In your case, it is

setContentView(R.layout.activity_main);

Therefore, any call you make to findViewById will lookup the id from activity_main.xml.

If it is unable to find the id that you have specified, it will return null.


It is worth mentioning that that you aren't calling that method and this is typically how a Toast is made.

Toast.makeText(getApplicationContext(), "Hello toast!", Toast.LENGTH_SHORT).show();
like image 26
OneCricketeer Avatar answered Oct 20 '22 23:10

OneCricketeer


In the case of an Activity, findViewById starts the search from the content view (set with setContentView) of the activity which is the view hierarchy inflated from the layout resource.

like image 41
Gerstmann Avatar answered Oct 20 '22 23:10

Gerstmann