Search This Blog

Sunday 15 April 2012

Handling User Input

In the Maths Test App, the user inputs the answer to mathematical questions by pressing buttons on the user interface.  Feedback is provided to the user via a text box or TextView in Android speak.

The following is an extract from my main.xml layout file.  Layout files were discussed in the previous post (layout files define the user interface).  This extract is for the "ENTER" button on my user interface.  The extract tells the android system that I want a button, the id that I will use to get access to the button from my code is "ButtonEnter", the text on the button should say "ENTER", the button should be aligned with the Button which has id "ButtonZero" and should be below the Button with id "ButtonNine", the button should also only be big enough in terms of height and width to display the text "ENTER".

<Button 
android:id="@+id/ButtonEnter" 
android:text="ENTER" 
android:layout_alignBaseline="@+id/ButtonZero" 
android:layout_below="@+id/ButtonNine" android:layout_toRightOf="@+id/ButtonZero" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>

The following line declares a class variable of type Button.  The Button class inherits from the View class. (If you have learnt Java, then you should know what classes, variables and inheritance is.  If you are struggling with these concepts then you need to revise your Java).


Button ButtonEnter;

The following line is used in code to get hold of an instance of the "ENTER" button and assign the instance to the class variable created earlier.  What this line of code is doing is calling a function provided by the Android SDK called findViewById.  You pass this function an argument which is the id of the View object you want to get hold of (The id's of all Views on your user interface are stored in the R.id class).  We know that the View object returned by the function is a Button, so we specifically cast it to a button.

ButtonEnter = (Button) findViewById(R.id.ButtonEnter);

Once you have got hold of the Button object you can call functions on it.  The following function sets the OnClickListener( ).  Setting the OnClickListener( ) causes the onClick function to be called when the user presses the "ENTER" button.


ButtonEnter.setOnClickListener(this);


The this argument passed to the setOnClickListener is an instance of the current activity.  My application only has the one activity and it is the onCreate function in this class which is the first to be called.  The declaration of the Activity class is shown below, the implements OnClickListener says this class will implement an onClick( )  function.

public class TestActivity extends Activity implements OnClickListener 
{


}


The signature of the onClick function which the code must implement is shown below.  This function will be called when the Enter button is pressed.  The View passed into the function will be an instance of the button pressed.  If you have more than one View for which you have defined OnClickListeners( ) then you will have to perform a check in the onClick( ) function to determine which View was clicked (I have to do this in the Maths Test App).

public void onClick(View v) 
{


}


The following extract is again from my layout file and creates the TextView.

<TextView 
android:id="@+id/textViewQuestion" 
android:layout_width="wrap_content" 
android:text="Press any button to begin" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textViewHeader" 
android:layout_alignLeft="@+id/ButtonTwo" 
android:layout_marginLeft="14dp" 
android:layout_marginTop="14dp">
</TextView>


The following three lines of code declare a variable of type TextView, get hold of the TextView and then write "Hello World" in the TextView.

TextView tvQuestionAnswerString;

tvQuestionAnswerString = (TextView) findViewById(R.id.textViewQuestion);
tvQuestionAnswerString.setText("Hello World");


Todays post has been fairly heavy on the code, so I will leave you to digest it.  Try and create a simple application which uses buttons to get user input and TextViews to provide feedback to the user. Good bye.



No comments:

Post a Comment