Android Adventures - Activity And UI
Written by Mike James   
Monday, 27 May 2013
Article Index
Android Adventures - Activity And UI
Creating a UI
Connecting the Activity to the UI
Summary

Connecting the Activity to the UI

Now we have an event handler hooked up to the button click event we usually want to do something that affects the UI as a result. Let's suppose that when the button is clicked we want to change the text displayed in the Large Text widget to "You Clicked Me!".

We can do this by changing the Large Text widget;s text property to the new text. The only problem is how do we find the Large Text widget in code? 

This is a fairly standard problem when you use a markup language to define a UI. The markup language defines widgets or other UI objects and the code has to have a way of making the connection to those UI objects. For example in JavaScript you make use of the getElementById method to retrieve a DOM object corresponding to a particular HTML element. In Android we do something similar. 

First make sure you follow the idea that all of the XML generated by the designer gets converted into a set of Java objects, one for each widget or view placed on the design surface. These objects have the same range of properties as you saw in the Properties window and methods to get other things done. All you need to do if find a way to reference them.

In the case of the view object that caused the event this is very easy as it is passed to the event handler as the only argument of the call. So if the event handler is:

public void buttonOnClick(View v) {

and the event handler is only hooked up to the button then you can be 100% sure that v is the button object. So if you want to change the button's caption you could just use its setText method to change its value. 

Note: if you aren't used to Java then it is worth pointing out that generally all Java frameworks follow the rule that if an object has a property called myProperty, visible as such in the Property window then in code you set it using setMyProperty and retrieve it using getMyProperty. 

So all you have to write is:

v.setText("I've Been Clicked!");

but this doesn't work because v is a general View object which doesn't have a setText method. To call the Button's setText method we have to cast v to its correct type - a Button. 

Note: casting is where you tell the system the type fo the object you are working with. If classB is a subclass of classA then you can treat a classB object as a classA - after all it has all of the methods and properties that classA does by inheritance. However if you want to make use of a property or method that only classB has then you need to cast the reference to the  classB object to make its type clear.

For example. assuming classB inherits from classA: 

classA myObject = new classB();

creates an instance of classB but myObject is decleared to be of type classA.  This is fine but you can only access the methods and properties of the classA object. If you try

myObject.classBMethod();

then it will fail if classBMethod only exists in classB. To use the classB method you have to cast myObject to its real type:

(classB) myObject

You can store a reference to the cast in a new variable:

classB myClassBObject = (classB) myObject;

and then call the method

myClassBObject.classBMethod();

or you can just do the cast on the fly at the cost of an extra pair of parentheses:

((classB) myObject).classBMethod();

 

However if you simply use add the cast to Button you will discover that Android Studio flags an error by showing Button in red. 

 

 cast

 

If you look at the error message it suggests pressing Alt+Enter which is always good advice because it produces a list of possible fixes for the problem. In this case you have to add the class definition to the start of the program. 

import android.widget.Button;

 

It will also offer to split the declaration and the assignment as being better style - for the moment let's stick with the single line.

Button button=(Button) v;

Now we have the button object we can call its setText method:

button.setText("I've Been Clicked!");

Now if you run the program you will see the button's caption change when you click the button. 

Notice that this approach only works if you know the type of the object that caused the event and called the event handler. If you don't then you can use the getId method to discover the view's id and write a switch to deal with each possible type in turn.

Now suppose we want to do something to one of the other widgets in the view. In this case we have to find the object that represents the widget without the help of the event handler's argument. For example how do we find the TextView that we placed below the button? 

This is a very standard pattern in working with view objects from the Activity. First you make use of the R object to find the widget's id. In this case the TextView has been given the id textView by default so we can use:

R.id.textView:

and this returns a unique integer that identifies the widget. Next we use the Activities findViewById(int) method which takes the integer id and returns the Java object corresponding to id. Of course this is returned as a View object because findViewById doesn't know the type of the object it returns any more accurately. So we have to cast to make use of the TextView object. 

Putting all this together gives:

TextView myTextView=(TextView)
                   findViewById(R.id.textView);

Once again Android Studio will complain about TextView and if you press Alt+Enter it will add the class import to the start of the program for you:

import android.widget.TextView;

Now that you have the TextView object you can use the setText method to change what it displays:

 myTextView.setText("You Clicked Me!");

The complete event handler is:

public void buttonOnClick(View v) {
 Button button=(Button) v;
 button.setText("I've Been Clicked!");
 TextView myTextView=(TextView)
                 findViewById(R.id.textView);
 myTextView.setText("You Clicked Me!");
}

If you now run the program you will see that you are informed twice of the fact that this very important button has been clicked:

 

buttonfinal

 

You may think that this is all very small stuff and nothing like a real app but this is how building a UI works in Android. You now know how to design a single screen app using the widgets available in the designer toolbox and how to hook them up to handle their events, find Java object that represent with them and how to call methods that modify them. 

Apart from the fine detail of how each of the widgets works - radio buttons, checkboxes and so on you now have the general outline of how to build a single screen app. 



Last Updated ( Friday, 20 June 2014 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.