Click Listener

Intents-An Example(Part 2)

Posted on Updated on

Hi all!

Finally, I got some time to continue with the example. I am really very sorry for the long delay. From now on, I will be posting every two to three days. So, keep looking!

For a flashback,  we discussed the use of intents in achieving communication among activities. We also saw the basic code of the two activities which will communicate with each other in the last post. Without wasting more time, let’s discuss the final codes for both of these activities.

Remember! I told you that we will be playing with the onClickListeners of both the buttons in the respective activities to accomplish the task.

Let’s start with the code for SecondActivity.java. Remove the code for showing the toast and replace it with a function called finish(). Yes! That’s it.

On clicking the button on the second activity, we just need to finish this activity and move back to HelloWorldActivity.java. Since, we will be calling this activity only from HelloWorldActivity.java (How to call from others? We’ll see!), finishing this activity will automatically take us where we want. Hence, the code for SecondActivity.java is over.

How about HelloWorldActivity.java? It’s the place where we will be actually calling the intent.

So, replace the code for the toast in the onClickListener with the following lines of code:

Intent intent = new Intent(HelloWorldActivity.this, SecondActivity.class);
startActivity(intent);

I hope that you are able to understand it clearly. We are basically making an object of class Intent passing to it two parameters. The first parameter is a reference to the current activity. this is a keyword which always refers to the current object. Hence, HelloWorldActivity.this simply specifies the activity from which the intent is being called. The second parameter is the one which specifies the activity to which we want to move. Remember, we specify it as .class. Hence, we have specified the second parameter as SecondActivity.class. But, the first line declares an Intent object called intent. To actually accomplish the task,  we need to call a function called startActivity with the parameter as the Intent object, which transfers the control to the activity specified while declaring the Intent object.

So, just run the above application and check out what it does! You will see that you will be able to go back and forth every time you click the button on the screen.

But, there is no practical use of this communication. Intents become more meaningful when we need to transfer some data from one activity to another.

In the next post, we will be looking at an application which involves some useful transfer of data.

Till then, BYE!

 

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

android:id=”@+id/button1″

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!