A Simple App! (Part 2)

Posted on Updated on

So, it’s time to continue with the development of the app (which we started developing in the previous post). First of all, let’s look at the android:id attribute of the tag Button.

By now, we know that id is used to provide a unique value to a component which can be used to access it later anywhere in the project. As an attribute, its value starts with @id. In our code, we can see that @+id has been used. Why is it so? Any guesses!

Actually, when a particular ID is used first time for a component, we need to use @+id to tell the compiler to assign the given value as the ID of the component.

Thus, the following line of code


basically assigns the id button1 to the Button on the screen. Later, we can use @id/button1 to refer this component while defining a value of an attribute. What does that mean? Suppose, while defining the layout, I want to refer to this button in some attribute of some tag. In that case, I will be using @id/button1. But, We need to use something else in order to reference this button in the JAVA code. Let’s see!

We have understood the layout of the app. Now, let’s open HelloWorldActivity.java. So, we need to refer to that button. How will we do this? For simplicity, let us call a function setUpViews() in the onCreate funtion. Since, this function has not been created till now, Eclipse will show an error. To rectify, simply hover on it and Select “Create method”. It will automatically create this function as a member of the Activity.

In the function, write this line of code:

NotifButton = (Button) findViewById(R.id.button1);

Can you make out what this does? R.id.button is used to reference the button in the code. How? R.java is the file that acts as a link between the code and various components and resources. I think id.button1 is self-explanatory. (For curious readers, open the R.java file in gen folder and explore!).

findViewById is a function used to provide a handle to the component using which various functions provided by Android SDK related to a component can be accessed. Difficult? Don’t worry! Treat it simply as an identifier which can be used to interact with the component. You can also see that the output has been type-casted to type Button. This is required as this function is used for various components and returns a generic handle which must be type-casted according to the component.

After this long story, the conclusion is that NotifButton is a handle to the component Button (whose id is button1). Also, when you write this line, an error is shown since NotifButton has not been declared. Simply hover over it and select “create field NotifButton”. Why not others? Try to find out.

Now, it’s time to use a function which is executed every time button is clicked. This is done using the following syntax:

NotifButton.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


This is just a syntax used to access this kind of function. Anything written in the onClick block gets exceuted when the button is clicked. For now, write the following line of code in the onClick function.

Toast.makeText(getApplicationContext(), “Hello World!”,Toast.LENGTH_SHORT).show();

Save the file and run the project. What happens?

In the next post, we will look at the above line of code and discuss some other coding aspects in Android App Development.

Till then, BYE!

7 thoughts on “A Simple App! (Part 2)

    […] We discussed the use of ID and its usage as an attribute in the layout and as a reference in the JAVA code in the post titled A Simple App! (Part 2) . […]

    Varsha said:
    June 5, 2012 at 12:28 pm

    NotifButton = (Button) findViewById(R.id.button1); is giving error.
    Multiple markers at this line
    – Button cannot be resolved to a type
    – NotifButton cannot be resolved to a
    – NotifButton cannot be resolved to a

      Nikhil Gupta responded:
      June 5, 2012 at 12:35 pm

      You have not imported Button class. Hover on Button and choose import Button. This will add Button class to your project. Then, you can proceed as stated in the post.

    chetanblogs said:
    June 13, 2012 at 12:46 pm

    there is also some problem with the line :
    Toast.makeText(getApplicationContext(), “Hello World!”,Toast.LENGTH_SHORT).show();
    ERROR it shows is – syntax error on token, expression expected instead

      Nikhil Gupta responded:
      June 16, 2012 at 7:16 am

      If you have such a problem, don’t use Hello World! directly. Declare a string in strings.xml and then, reference it here.

    Abhishek said:
    June 15, 2012 at 9:45 pm

    Toast.makeText(getApplicationContext(), “Hello World!”,Toast.LENGTH_SHORT).show(); is giving error.
    The error is:
    Multiple markers at this line
    – Toast cannot be resolved to a variable
    – Toast cannot be resolved
    – Syntax error on tokens, Expression expected instead
    – Line breakpoint:HelloWorldActivity [line: 29] –

      Nikhil Gupta responded:
      June 16, 2012 at 7:07 am

      Hover above Toast and select import Toast from the quick fixes that are available.

Leave a Reply to Nikhil Gupta Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s