ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How To Make a Mad Libs App (for Android)

Updated on April 9, 2018

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();
	    
		
	}


}

This Tutorial Contains the Base Code For This App (The Final Product). It Is Called Epic Libs. It is free on Amazon and Google Play Stores. (but I'm sure som

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.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)