How to Make Android Apps 4

How to Make Android AppsIn this video tutorial I will cover how to make menus and dialog popups. I’ll specifically cover laying out menus in main.xml, DialogFragment, AlertDialog, Action Bars, Option Menus and more.

If you missed the first videos they are here How to Make Android apps 1, 2, and 3. Definitely watch them first. All of the code follows the video below. It is heavily commented to help you learn.

If you like videos like this, it helps me if you share on Google Plus with a click here [googleplusone]

Code from the Video


<menu xmlns:android=""
    tools:context="" >

    <!-- The Options or Overflow menu is located in the upper right hand
    corner for devices running Android 4.0 and above.
    To make this menu use the menu element and define each option in an
    item element.

    orderInCategory defines the order of appearance

    showAsAction defines that the item should appear on the action bar
    if there is enough room or not. If you want to support the Action Bar
    for versions of Android below 2.1 use your apps name space instead of
    android so you can use import;
    Use withText to show the title in the action bar
    icon defines a icon to use for the item

    checkableBehavior is used to allow the user to select a single or all
    items when the menu items are surrounded by group -->

    <group android:checkableBehavior="single">

    <item android:id="@+id/action_settings"

    <item android:id="@+id/exit_the_app"




import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends ActionBarActivity {

    protected void onCreate(Bundle savedInstanceState) {

    // This method creates the menu on the app
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(, menu);
        return true;

    // Called when a options menu item is selected
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        // We check what menu item was clicked and show a Toast
        if (id == {

            // A DialogFragment is a Fragment you can place over top
            // the current Activity. A Fragment is like an interface
            // block that you can place into an Activity.
            // The FrgamentManager allows you to interact with the
            // Fragment
            DialogFragment myFragment = new MyDialogFragment();

  , "theDialog");

            return true;

            // If exit was clicked close the app
        } else if (id == {
            return true;
        return super.onOptionsItemSelected(item);



// This will be used to create a dialog window

import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

// If you get an error that the minimum must be 11, change the minimum in the manifest
// and also change it in build.gradle

// To generate the onCreateDialog() right click on DialogFragment and Generate and
// select onCreateDialog()

public class MyDialogFragment extends DialogFragment{
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // We build the dialog
        // getActivity() returns the Activity this Fragment is associated with
        AlertDialog.Builder theDialog = new AlertDialog.Builder(getActivity());

        // Set the title for the Dialog
        theDialog.setTitle("Sample Dialog");

        // Set the message
        theDialog.setMessage("Hello I'm a Dialog");

        // Add text for a positive button
        theDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {

                Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();


        // Add text for a negative button
        theDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int i) {

                Toast.makeText(getActivity(), "Clicked Cancel", Toast.LENGTH_SHORT).show();


        // Returns the created dialog
        return theDialog.create();



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
    package="" >

        android:targetSdkVersion="19" />

        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


17 Responses to “How to Make Android Apps 4”

  1. swanand says:

    Hi derek ,

    you have posted so many android tutorials which are very greatly explained in details. I had seen some of them but as i am fresher i just want to know that is there sequence of those tutorials so that i can start it from the 1st tuts, if it is please tell me how the sequence is..?
    thank you in advance. hope so you will reply asap.
    thanks once again.

  2. Paula Caroline says:

    Hi Derek, I love your tutorial. But something happen when I tried to run the app in the kit kat version, the dialog appears without no text, and the buttons “OK” and “Cancel” don´t appear too. Do you know something about it? And what is the way to solve this problem. Thanks.

    • Derek Banas says:

      Hi Paula, I’m guessing there must me missing text. I’m running the apps with Kit Kat as well. Try comparing your code to mine using and you’ll find the error.

  3. Kevin says:

    Hi Derek
    In this tutorial I found these strange results hopefully you can shed some light on them:
    1. When running this app on the emulator the fragment did not look right
    Emulation Image
    The emulator was running Android 4.2.2 ARM
    2. When running this same code on my HTC One X i got the following result
    Device Image

    Thank you for these videos

  4. Paul says:

    Another great video.

    I had the same problem as Paula (it must be a name thing) where the text and buttons wouldn’t show.

    return super.onCreateDialog(savedInstanceState);
    return theDialog.create();

    in fixes this. This wasn’t shown in the video though.

    Thanks Derek.

  5. Ruben says:

    Hello Derek ,thank you so much for the tutorials..

    I am using Max api verion of 21 (lollipop) and was having problems with getfragmentmanager , had to replace



    Ones again , thank you so much for all these wonderful tutorials and making new tutorials with latest updates – developing for anything with Intellij rocks ..It is just so much more efficient , more thinking less chore

  6. TC says:

    I face the same problem of blank dialog, despite all corrections the message in dialog doesn’t show. I tried both on Emulator and Device it doesn’t work. Code is exactly same as on this page. What might be missing, any help.

    Thanks & regards,

Leave a Reply to Noob Dev Cancel reply

Your email address will not be published.