My Blog List

Thursday 24 November 2011

Dialog in Android.


Hello Friends,
Here is another post from DeveloperSite. This post  is helpful for those who are messing with creating Dialog In their app to make it cool and user friendly.
Here we go  ……..
Dialog  in android  is usually a small window that appears in front of the current Activity. When it appears it accepts all the user interaction and current Activity loses focus. Dialogs are basically used for notifications  (such as a progress bar or a login prompt, alert box).
The Dialog class is used to create Dialog. But we typically can not instantiate Dialog class.
We use AlertDialog, ProgressDialog, DatePickerDialog, TimePickerDialog class to create dialog of its type.
Now here is the full project for creating  Alert Dialog, Dialog with radio button, progress Dialog, progress Bar
1. So Create new project   file->new->Android Project.
2. Create  your  main.xml file.
3. Now do some code in your MainActivity.java file
package com.bp.dialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class DialogDemo extends Activity {

    private static String  items[]={"Red","Green"};
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
     }
  
    public void buttonAlert(View view)
    {
      
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to exit?")
               .setCancelable(false)
               .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        DialogDemo.this.finish();
                   }
               })
               .setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                   }
               });
        builder.create();
        builder.show();
    }
    public void listDialog(View view)
   
    {AlertDialog.Builder ab=new AlertDialog.Builder(this);
    ab.setTitle("Pick a color");
    ab.setItems(items, new DialogInterface.OnClickListener() {
      
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_LONG).show();
        }
    });
    ab.create();
    ab.show();
      
    }
    public void radioDialog(View view)
    {
        AlertDialog.Builder ab=new AlertDialog.Builder(this);
        ab.setTitle("Pick a color");
        ab.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
          
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_LONG).show();
              
            }
        });
        ab.create();
        ab.show();      
      
    }
   
    public void progressDialog1(View view)
    {
        ProgressDialog dialog = ProgressDialog.show(DialogDemo.this, "","Loading. Please wait...", true);
      
    }
    public void progressDialog2(View view)
    {
      
        //it will just display the progressBar
        ProgressDialog progressDialog;
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Loading...");
        progressDialog.setCancelable(false);
        progressDialog.show();
      
       
       
    }
      
    }
  
   
  

   
4. Create your AVD and run your app in emulator
5. Here is the output
 
 

 



 Thats it for this post.
feel free to comment
for any query mail me :bpsingh216@gmail.com

No comments:

Post a Comment