Monday, June 18, 2012

How To Create Dialog Box In Android Application


The project describes how to draw a Dialog Box in your application.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a Project DialogExample
2.) Make sure to add following in your res/values/strings.xml file:
<resources>
    <string name="hello">Hello World, DialogExample!</string>
    <string name="app_name">DialogExample</string>

 
    <string name="new_game_title">Difficulty</string>
    <array name="difficulty">
        <item name="easy_label">Easy</item>
        <item name="medium_label">Medium</item>
        <item name="hard_label">Hard</item>
        </array>
</resources>
3.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. DialogExample.
2.) Enter following information:
Project name: DialogExample
Build Target: Android 2.3.3
Application name: DialogExample
Package name: org.example. DialogExample
Create Activity: DialogExample
On Clicking Finish DialogExample code structure is generated with the necessary Android Packages being imported along with DialogExample.java. Following code must be added in DialogExample class to get the menu to work.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class DialogExample extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        openNewGameDialog();
    }
    private void openNewGameDialog()
    {
                new AlertDialog.Builder(this)
                .setTitle(R.string.new_game_title)
                .setItems(R.array.difficulty,
                new DialogInterface.OnClickListener()
                {
                public void onClick(DialogInterface dialoginterface,int i)
                {
                }
                })
                .show();
        }
}
Output – The final output:

No comments:

Post a Comment