For various reasons that are too complicated (and mostly forgotten) I recently ended up installing Android Studio. The original reasons I did this have frittered away into the mists of time, however one of my experiments has stuck around.

One of the things I’ve been looking at doing for some time is building a small app for myself which would have certain parts that are dynamically generated/refreshed (aren’t they all?). So I started with the Building Your First App tutorial and came through it reasonably quickly and unscathed. I moved on to the Adding the Action Bar tutorial, and decided “yeah, I know what I’m doing”.

I quickly moved to the Building a Dynamic UI with Fragments, but quickly ran into problems (uh-oh, running before I can walk!):

  • The tutorial is very fragmentary, it only shows the absolute bare bones
  • The tutorial actually does something slightly different to what I was expecting (it swaps part of an activity for a different activity, and then adds data through an adapter, something I didn’t want to do)
  • For the life of me I couldn’t open the tutorial in Android Studio, which made navigating it hard (I suck, I know)

So I started hunting around the internet and the best tutorial I found was from Lars Vogel - Multi-pane development in Android with Fragments - Tutorial. So armed with these tutorials and various bits of found information I set off on enhancing my application…

The first step (for me at any rate), was to remove the support library. The reason I wanted to do this was that a lot of the information I was finding was based on the later Android SDKs, and not using the support library. The support library, amongst many other things, adds fragment support to older versions of Android, but necessarily names and method signatures are slightly different.

The next step in the process was to add a message fragment, the first part is the MessageFragment class. A couple of points about the class: The class has a static factory function that takes the information to be displayed in the fragment, and then returns an instance of the class:

One point to notice is that the string is put into an argument Bundle, that is then fed to the Fragment setArguments. Of course it would be possible to simply put the arguments into the class and hold them as members, but this doesn’t seem to be The Done ThingTM. (Beginner, remember…?)

The other thing of importance is the way that the string is populated into the fragment UI. This is done in the onStart override:

The reason this is done in onStart, rather than onCreate is because of the way the activity lifecycle works.

As well as adding the MessageFragment class a layout is created, and the referenced static string (to supply default content) is added to the strings.xml.

Once the fragment was ready I moved on to adjust the main activity layout. First I moved the text input and button into a LinearLayout:

Doing this isolates these static parts of the display from the parts that will change.

I also added a second LinearLayout, this one is empty and has an orientation of vertical so that new fragments appear at the bottom. The height of the layout is set to fill_parent so that it occupies all space that is available.

At this point it’s important to note that the height for the fragment is set to wrap_content (on both the FrameLayout and TextView) (lines 3 and 8). This allows the display of the fragment to be large enough to show the required text, but not take up the entire area. Originally I had both set to match_parent and was wondering why only one fragment ever displayed, even though the code was working correctly.

The final step is to update the Main Activity. There are two updates that are needed, first it has to implement the OnFragmentInteractionListener interface defined in the message fragment:




Declaring an interface like this allows the fragment to accept user input, but delegate it’s action to containing class(es). In this case there is (currently) nothing I want to deal with.

The other part of MainActivity that needs to be changed is make the SendMessage method create and display the fragment, easy right?

Actually yes, this particular part of the process is reasonably well documented. The method simply retrieves the string the user has entered, uses the Activity’s getFragmentManager Method to get a FragmentManager, and then includes the newly created fragment into the display.

And this is what the built app looks like:

Screen shot 1 Screen shot 2 Screen shot 3

Text is entered in the text box, press Send and it is added to the area below. Why? Because I wanted to find out how to do it…