Learn Android Development For Beginners


Views are the UI elements that are basic building blocks of user interface commonly known as control or widget. For Example view can be a button, Text editor, Label and lot of other UI elements. Views are same as controls in ASP.Net. View is the base class of every UI element.

Access View in Activity Class

While developing the android application, you will need to access the views in the class using code. Because in programming languages the controls values get and set from code file. The concept will get clearer after understanding the next example. In the example one the click of the button the text of the label will be changed.

Open the activity_main.xml file in your first allocation and add the button in the layout file from the widgets Palette.

Android Button Properties
(Figure 4.2.1)

Select the button and in the property palette set the text = “Click on Button” and set id=”@+id/btnChange”. The id of the button is “btnChange” but what is the “@+id” at the start, what does it means. “@” sign means in the android development that you want to access any Resource. And after the “@” sign write the name of resource e.g. @string/hello_world, @drawable/logoImage etc. @string/hello_world means use the hello_world in the string resource.

We will learn about the resources in detail later. But in brief it is recommended in android application development to use resources for your string, images etc. Using resource you can easily manage images for different device resolution, language etc.

So come back to the point what does “@+id” means. The + sign with @ means if the resource exist then use it otherwise create it. @+id/btnChange mean use the btnChange in id resource and if it does exists then create it.

Select the TextView and assign it id ="@+id/tvTitle". Now the change in the layout file has been completed and the final xml is this.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Hi It's My First Program"

        android:text="Click on Button"
        android:layout_centerHorizontal="true" />


Now we have to write some code in the activity class to access the views there. Open the “MainActivity.java” file and get the button view and register click event and get the TextView to set its text message.

findViewById(int id) method is available for accessing the View in the layout that matches the id. “id” is the unique identifier for the view in the layout. But why it is accepting “id” as int type. But the name of our button is “btnChange”. Actually Android compiler generates a unique ids for the view in the in the “id” resource and you can access it using “R.id”. “R.id” contains the all your ids that are defined in the layout file. To get the id of the “btnChange” type the “R.id.btnChange”. The complete statement will be findViewById(R.id.btnChange). The view that is being gotten from the statement is a Button. It is better to cast it in the Button.

 Button button = (Button)findViewById(R.id.btnChange);

As in most of the programming language, you need to add or import the reference of the library whose functionality is going to use in the file. Same when you will write “Button” the Android Studio will make the color and ask you to add the Button’s library by pressing “Alt + Enter”

Android Button Properties
(Figure 4.2.2)

Or you will see the Bulb in Red color on the left side of the line to add the class reference. Press on the Bulb and in the opened drop down click on “Import Class” it will import the Button class in your code.

Android Button import class
(Figure 4.2.3)

The both either Pressing “Alt + Enter” on the Button or by clicking on the “Import Class” adds the following statement on the page.

import android.widget.Button;

After getting the View in the code, now it is easy to play with it. Suppose if we want to change the text of the TextView on the click of the button. Then we need to get the Button View in the code and register the click event and on the click event change the text. See the code below

    protected void onCreate(Bundle savedInstanceState) {

        Button button = (Button)findViewById(R.id.btnChange);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                TextView textView = (TextView)findViewById(R.id.tvTitle);
                textView.setText("Yes! You pressed the button.");



Register the click event of the button using button.setOnClickListener() statement and in the click event get the TextView using the statement TextView textView = (TextView)findViewById(R.id.tvTitle);

Java provides a different ways to register the events and we will learn about it later in detail.