How To Make a Mad Libs App (for Android)

DIsclaimer: To make this kind of app, it is extremely helpful if you have some understanding of the Android Development Environments for example Eclipse or Android Studio. It is also helpful if you have some understanding of basic level Java. However, in case you don't have a clue what I just said;;for your convenience, all essential programming terms/jargon are highlighted and once clicked, provide a link to other explanatory material and/or definitions. I aim to please :-)


Wireframing:

Figure 1. ) Very simple wireframing done using Balsamiq.
Figure 1. ) Very simple wireframing done using Balsamiq.

The term wireframing has a few distinctive definitions floating out there in cyberspace, but one of the best I have seen goes as follows: "It is a visual guide that represents the skeletal framework of a website, app, program, anything that involves a UI (User Interface). In figure 1 (located above), I have done some very simple wireframing to clearly depict what we will be doing for this app.

At the start of the app, we will have an Input Form that asks the user to input some words on the given textfields (lines). After the user is done with the input, they press a button labelled "Get My Mad Lib" and this makes the program display another page....the Output Form. As you would expect, the Output Form has some text (maybe a paragraph or two), and within this text you will find the words that you just input into the Input Form.

To create this very simple example of wireframing I used Balsamiq (a very popular wireframing tool). However, there are many others that you can use. Check out this list to see what I mean. Whether you are part of a development team, a solo programmer, or a client looking to have a program/app developed for you; you can use wireframing as a great communication and organization tool.

Designing The UI

Figure 2)  Actual Input Form (Left) ---------------- Figure 3.)  Actual Mad LIbs Output (Right)
Figure 2) Actual Input Form (Left) ---------------- Figure 3.) Actual Mad LIbs Output (Right)

To better follow along with the rest of the tutorial, I suggest placing the following xml code in your project. The first piece of XML code, once placed in your project, will display an input form much like the one seen in Figure 2.). The second piece of XML code, once placed in your project, will display an input form much like the one seen in Figure 3.). The input form is obviously the more complicated of the two, and for good reason. There are about 7 to 8 variables that you need from the input form to make computations, while the output form just has a single textview where you display your text.

Also, as a note; in this project I have named the input file...main_class_activity_in.xml. And the output file is named main_class_activity_out.xml. These file names will also serve as id when calling (R.id.[file name]).

Furthermore, while sometimes you do not need to give the various components in the xml files (views) ids; in order to get the program to work properly you should give ids to the at least these 2 viewswhich I have already named.

"anskey_clover"

"outview14"

Lastly, it is difficult to see from Figure 3.) but the text within the textview with id -- "outview14" located in main_class_activity_out.xml has the occasional string "wx" inserted where normally you would find proper english word. These "wx's" will be replaced with entries for your mad lib.

Mad Libs Input Form Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.39" >

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="184dp"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/outview1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="1.) Adjective to Describe:" />

            <TextView
                android:id="@+id/lvheader_song"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="2.) ing-verb (i.e. running):" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="3.) Negative Adjective:" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="4.) Negative Verb:" />

            <TextView
                android:id="@+id/textView5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="5.) Adjective" />

            <TextView
                android:id="@+id/textView6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="6.) Plural Noun:" />

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="7.) Verb (ending in &apos;-ed&apos;):" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/anskey_clover"
            android:layout_width="match_parent"
            android:layout_height="234dp"
            android:orientation="vertical" >

            <AutoCompleteTextView
                android:id="@+id/auview1"
                android:layout_width="141dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.40"
                android:ems="10"
                android:text="la dee dah" />

            <AutoCompleteTextView
                android:id="@+id/auview2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <AutoCompleteTextView
                android:id="@+id/auview3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <AutoCompleteTextView
                android:id="@+id/auview4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <AutoCompleteTextView
                android:id="@+id/auview5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <AutoCompleteTextView
                android:id="@+id/auview6"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

            <AutoCompleteTextView
                android:id="@+id/auview7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:ems="10" />

        </LinearLayout>
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="222dp"
        android:layout_height="93dp"
        android:layout_weight="0.25"
        android:onClick="onClick"
        android:text="Get My Mad Lib" />

</LinearLayout>

Mad Libs Output Form Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/outview14"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="As it happens, &quot;Cloverfield&quot; clocks in at 84 minutes, a running time that includes the usual interminable wx .  It starts under the premise that we are watching an official, classified government video detailing a critical wx or events.  Then we are introduced to the cast, a hodgepodge of late wx-something young things who consistently do any and everything that is wx to basic logic and human nature throughout the film.  Take for example Hud,  the hapless best friend of the film&apos;s  main character who inexplicably documents each and every single events despite his admitted dislike of wx (and oh yeah while he&apos;s running for his life).   If there is anything controversial or thought-provoking about this film, it would have to be the movie&apos;s allusions to the events of 9/11.  But don&apos;t worry, &quot;Cloverfield&quot; is far too dumb to offend everything except your wx.  While the film is nominally a monster movie, in truth it is mostly a feature length gimmick.  Like too many big-studio productions, &quot;Cloverfield&quot; works as a showcase for impressively realistic-looking speical effects.  As a consequence, the movie&apos;s best actor is the 40 foot monster who systematically trashes downtown Manhattan.  The rest of the case is about as smart as tater tots and half as wx.  Rarely have I rooted for a monster with such enthusiasm. " />

</LinearLayout>

Code: That ties it all together

package com.example.apptestactivity;

import java.io.IOException;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;


public class MainClass extends Activity {

  int inLayout; // will hold the id number for layout file main_class_activity_in.xml.
  int outLayout; // will hold the id number for layout file main_class_activity_out.xml.
  int outviewid;   // will hold the id number for the textview found in main_class_activity_out.xml
  Activity activity = this;
  String test;
  AutoCompleteTextView autoview;
  StringBuffer stringbuffer = new StringBuffer();
  String[] stringviews = new String[16];

     protected void onCreate (Bundle savedInstanceState){
	super.onCreate(savedInstanceState);
		
	setContentView(R.layout.main_class_activity_in);
	outLayout = (R.layout.main_class_activity_out);
	inLayout = (R.layout.main_class_activity_in);
	outviewid = (R.id.outview14); 
		
      }
	
      
      public void gather(){
		
	ViewGroup layout = (ViewGroup)findViewById(R.id.anskey_clover);
	View[] views = new View[layout.getChildCount()];
	int[] ids = new int[layout.getChildCount()];
		
	for (int i = 0; i < layout.getChildCount(); i++){
	      ids[i] = layout.getChildAt(i).getId();
	}
		
	for (int i =0; i < layout.getChildCount(); i++){
	      AutoCompleteTextView au =(AutoCompleteTextView)findViewById(ids[i]);
	      stringbuffer.append(au.getText().toString() + "\n");
		
			
		}

	setContentView(outLayout);
	TextView outview = (TextView)findViewById(outviewid);
	outview.setText(stringbuffer.toString());
		
       }
	
	public void postIt(){
	   String str = "let's go to the park";
	   String str2;
       	   String newstr = null;
		
	   setContentView(outLayout);
	   TextView outview = (TextView)findViewById(outviewid);
           str = (String) outview.getText();
	   stringviews = stringbuffer.toString().split("\n"); // turns the stringbuffer from getAllXml() into an array and assigns to stringviews.
	   
           for (int i=0;i<stringviews.length;i++){
		str2 = stringviews[i];
		newstr = str.replaceFirst("wx",str2); // replaces all the "wx"s in the main_class_activity_out textview with values from strinvgviews.
		str = newstr;
		}
	    outview.setText(newstr);
	    stringbuffer.delete(0,stringbuffer.length());
	}
	
	

   public void onClick(View view) {

	    gather();
	    postIt();
	    
		
	}


}

I just wanted to take time to provide a quick summary of the above code. After declaring all of our essential variables and initializing some layouts and other views in onCreate(), The following three methods do the actual work of the program.

First, when the user clicks the button on main_class_activity_in.xml, it calls the onClick method located in the code above. This method then in turn calls the methods gather() and postIt().

The gather() method first initializes the layout "anskey_clover" to a ViewGroup which I have called "layout." Once you have a ViewGroup object defined, you can search its child views (views/widgets contained within another view). In this case, the layout "anskey_clover" has a bunch of child views --- 7 AutoCompleteTextviews to be exact. The code will gather the values from these AutoCompleteTextviews and put them in a StringBuffer object called stringbuffer. All of this is done using for loops.

Then if we go back to the onClick method, we see that it calls the postIt() method. And in a nutshell what the postIt() medthod does is

  • Sets the screen to the layout file main_class_activity_out.xml using the setContentView() method.
  • Use the stringbuffer in conjunction with the split() method to create an array containing the inputs from the AutoCompleteTextviews. The array object is called "stringviews."
  • Create a TextView object that is linked to the TextView in main_class_activity_out.xml. The one that has id -- "outview14". In the code, this TextView object is simply called "outview."
  • Get the string from "outview" and replace all the "wx"'s in it, with the values from the object "stringviews."

And that should be it. You are now done with a simple mad libs app! :) If you have any questions about implementation, leave them in the comments section below.

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working