Chapter 13. Anonymous Classes – Bringing Android Widgets to Life

This chapter could have been called "Even More OOP," as anonymous classes are very much still part of that subject, but as we will see, anonymous classes offer us so much flexibility, especially when it comes to interacting with the UI, that I thought they deserved a chapter title dedicated to them and one of their key uses in Android.

Now that we have a good overview of both the layout and coding of an Android app, as well as our newly acquired insight into object-oriented programming and how we can manipulate the UI from our Java code, we are ready to experiment with more widgets from the palette, alongside anonymous classes.

OOP is a tricky thing at times, and anonymous classes are known to sometimes be a bit awkward for beginners but, by gradually learning these new concepts and then practicing them repeatedly, over time, they will become our friend.

In this chapter, we will diversify a lot by going back to the Android Studio palette and looking around at half a dozen widgets that we have either not seen at all or have not used fully yet.

Once we have done so, we will put them all into a layout and practice manipulating them with Java code.

In this chapter, we will cover the following topics:

  • Refreshing our memories on declaring and initializing layout widgets
  • How to create widgets with just Java code
  • Looking at the EditText, ImageView, RadioButton (and RadioGroup), Switch, CheckBox, and TextClock widgets
  • How to use an anonymous class
  • Making a widget demo mini app using all of the preceding widgets and plenty of anonymous classes

Let's start with a quick recap.

Declaring and initializing the objects

We know that when we call setContentView in the onCreate method, Android inflates all the widgets and layouts, and turns them into real Java objects on the Heap.

We know that to use a widget from the heap, we must first declare an object of the correct type and then use it to get a reference to the UI widget object on the heap by using its unique id property.

For example, we get a reference to a TextView with an id property of txtTitle and assign it to a new Java object called myTextView, as follows:

// Grab a reference to an object on the heap
TextView myTextView = (TextView) findViewById(R.id.txtTitle);

Now, using our myTextView instance variable, we can do anything that the TextView class was designed to do. For example, we can set the text to appear as follows:

myTextView.setText("Hi there");

Make it disappear like this:

// Bye bye
myTextView.setVisibility(View.GONE)

And then change its text again and make it reappear:

myTextView.setText("BOO!");

// Surprise
myTextView.setVisibility(View.VISIBLE)

It is worth mentioning that we can manipulate any property in Java that we set using XML in the previous chapters. Furthermore, we have hinted at, but not actually seen, that we can create widgets from nothing, using just Java code.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.129.172