12. GUI Objects

Now that we’ve set up the generic card game classes to work in an Android application, we can focus on making our UI respond to user interaction.

The first thing we need is all the items we added to the UI through our “main.xml” file require a Java variable that links to them. Change your StartActivity class so that it looks like this:

public class StartActivity extends Activity {

    // CREATE THE GUI OBJECT VARIABLES
    //================================
    private ArrayList<ImageView> img_d_cards;
    private TextView txt_instruct;

    /** #########################################################
        #                                                       #
        # onCreate: This is the starting method for an Activity #
        #                                                       #
        # - - - - - - - - - - - - - - - - - - - - - - - - - - - # */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //INSTANTIATE THE GUI OBJECT VARIABLES
        //====================================
        img_d_cards = new ArrayList<ImageView>();
        img_d_cards.add((ImageView) findViewById(R.id.d_card1_img));
        img_d_cards.add((ImageView) findViewById(R.id.d_card2_img));
        img_d_cards.add((ImageView) findViewById(R.id.d_card3_img));
        img_d_cards.add((ImageView) findViewById(R.id.d_card4_img));
        img_d_cards.add((ImageView) findViewById(R.id.d_card5_img));

        txt_instruct = (TextView)  findViewById(R.id.instruct_txt);
    }
}

GUI Objects
Here you can see we’ve added two starting objects. An arraylist of “ImageView” objects called “img_d_cards” and a “TextView” object called “txt_instruct“. These are simply Java member variables that are going to link to their respective items in our XML file. You can see inside the onCreate method we are instantiating those objects so that they “point to” the correct items on our UI.

findViewById
The method “findViewById” is fairly self-explanatory, it simply allows you to refer to any element you’ve named in your XML. Notice that the name we used in our XML file has the type of object as a suffix and the object reference here in the Java class has it as a prefix? This makes the reference procedure a bit easier to remember. I suggest you stick with this naming scheme in future projects, it’ll make your life much easier!

Casting
When you see an object type inside brackets like in this line:

txt_instruct = (TextView)  findViewById(R.id.instruct_txt);

What’s happening is we’re telling the code on the right-hand side of the bolded phrase to become (in this particular instance) a “TextView” object. This is what’s known as casting which basically converts one object type into another. So in our case, the output from “findViewById” is being converted into a “TextView” object before it goes into the “txt_instruct” object.

Okay! Now it’s time to see if you can create the other required objects and instantiate them. It’s really no different to what we’ve already done here, the only unknown element is how you create buttons, they will simply be “Button” objects. If you get stuck, as usual, ask below.

One thought on “12. GUI Objects

  1. hi there!
    I am stuck at this part, “hard tutorials, android app development 12.GUI Objects”.
    I am confuse with what other required objects do we need. Is it just the button?

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>