Tuesday, September 4, 2012

Android | DialogBox Example

Here, i would like to show you how we can show confirmation dialog box.

Note: below code has been verified on Android Emulator 4.0.3.

Code: activity_dialog_example.xml




<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <Button
        android:id="@+id/btnyesno"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnyesno"/>

    <Button
        android:id="@+id/btnitems"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnitems"/>
 
    <Button
        android:id="@+id/btnsingleitem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btnsingleitem"/>
     
</LinearLayout>


Code: strings.xml


<resources>

    <string name="app_name">DialogExample</string>

    <string name="menu_settings">Settings</string>
    <string name="title_activity_dialog_example">Dialog Example</string>

    <string name="btnyesno">Yes or No</string>
    <string name="deletedialog">Confirmation</string>
    <string name="deletemessage">Are you sure, Do you want to exit ?</string>
 
    <string name="btnitems">Items</string>
    <string name="btnsingleitem">Single Item Selection</string>
</resources>

Code:DialogExample.java


package com.example.dialogexample;

import javax.crypto.spec.OAEPParameterSpec;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;



public class DialogExample extends Activity implements OnClickListener {

static final int YESORNO_DIALOG = 1;
static final int IITEMS_DIALOG = 2;
static final int SINGLEITEMSELECTION_DIALOG = 3;

Button btnYesNo, btnItems, btnSingleItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_example);
     
        btnYesNo = (Button) findViewById(R.id.btnyesno);
        btnYesNo.setOnClickListener(this);
     
        btnItems = (Button) findViewById(R.id.btnitems);
        btnItems.setOnClickListener(this);
     
        btnSingleItem = (Button) findViewById(R.id.btnsingleitem);
        btnSingleItem.setOnClickListener(this);
     
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_dialog_example, menu);
        return true;
    }

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
AlertDialog.Builder oAlert = new Builder(this);     //Here "this" is must if we use "getApplicationContext" then shows an error.
AlertDialog oDialog = (AlertDialog)  super.onCreateDialog(id);

    try
    {

    switch (id){
    case DialogExample.YESORNO_DIALOG:

    oAlert.setMessage(R.string.deletemessage);
    oAlert.setTitle(R.string.deletedialog);
    oAlert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
   
    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub
    DialogExample.this.finish();
    }
    });
   
    oAlert.setNegativeButton("No", new DialogInterface.OnClickListener() {
   
    public void onClick(DialogInterface arg0, int arg1) {
    // TODO Auto-generated method stub
    arg0.cancel();
    }
    });
   
    oDialog = oAlert.create();
    break;
    case IITEMS_DIALOG:
final String[] strColor = {"Red","Green","Blue"};
oAlert.setTitle("Colors");

oAlert.setItems(strColor, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), strColor[arg1].toString(), Toast.LENGTH_LONG).show();
}
});
oDialog = oAlert.create();
break;

    case SINGLEITEMSELECTION_DIALOG:
    final String[] strFruit = {"Apple","Grapes","Mango"};
    oAlert.setTitle("Fruits");
    oAlert.setSingleChoiceItems(strFruit, -1, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), strFruit[arg1].toString(), Toast.LENGTH_LONG).show();

}
});
    oDialog = oAlert.create();
    break;
    }

   }
    catch(Exception e){
    Log.d("Dialogbox",e.getMessage().toString());
   
    }
    return oDialog;
    }

public void onClick(View arg0) {
// TODO Auto-generated method stub
try{

switch(arg0.getId()){
case R.id.btnyesno:
showDialog(YESORNO_DIALOG);
break;
case R.id.btnitems:
showDialog(IITEMS_DIALOG);
break;
case R.id.btnsingleitem:
showDialog(SINGLEITEMSELECTION_DIALOG);
break;

}
}
catch(Exception e){
Log.d("Dialogbox",e.getMessage().toString());
}
}
}



Result of Clicking on "Yes or No" button :




Note: 
As per the code, when user selects "Yes" then application will be closed and nothing will happen when user selects "No".
Result of while user clicks on "Items" button


Result of while user clicks on "Select Single item".






No comments:

Post a Comment