Intro to Android

User Interaction

Layout

We inflate the layout in onCreateView(). If you'd like to access it class-wide, create a field.

Click listeners

When you want a user to interaction with a piece of your UI, add OnClickListener's to a view. You can add click listeners to all sorts of things! In other words, anything can be a "button".

Let's start with the number keys. I like to create functions when possible! I private void configureNumberKeys() { configureNumberKey( R.id.key1 ); // ... }

private void configureNumberKey( int id )
{
    layout.findViewById( id )
          .setOnClickListener( createNumberOnClickListener() );
}

private View.OnClickListener createNumberOnClickListener()
{
    return new View.OnClickListener()
    {
        @Override
        public void onClick( View view )
        {
            String number = ((Button) view).getText().toString();

            Toast.makeText( getActivity(),
                            "Clicked: " + number,
                            Toast.LENGTH_SHORT)
                 .show();
        }
    };
}

Call configureNumberKeys() from onCreateView().

When you run this, you should see a Toast with the text of the key that you clicked.

Post to bus

In your click listener, post an event to the bus:

postToBus( new NumberButtonEvent( number ) );

We don't have the number event, so generate one now and put it in the events package. We are passing in the number that is displayed on the button to the event's constructor.

Our event looks like this:

public class NumberButtonEvent extends BaseEvent
{
    String number;

    public NumberButtonEvent( String number )
    {
        this.number = number;
    }

    public String getNumber()
    {
        return number;
    }
}