Monday, June 18, 2012

java technical questions


1.
public class Example1 {
    int i=1000;
    public static void main(String[] args) {
        System.out.println("Mr. "+args[1]+"get salary "+i);
        //compile time error:saying
        //non static variable i can not be referenced from static area
     
    }
 
}

2.class Base
{
    public void m1()
    {
     
    }
}
 class Example2 extends Base{
     protected void m1()//m1() in Example2 can not m1() in Base
             //attempting to assign weaker access privileges;was public
     {
       
     }
 
}

3.class B
{
    final private void m1()
    {
     
    }
}
 class Example3 extends B{
     void m1()
     {
         //no error because private methods are not inherited
     }
 
}

4.public class Example4 {
   public static int m1(boolean b)
    {
     
         try
         {
        if(b)
        {
            return 1;
        }
        return 0;
         }
         finally
         {
             System.out.println("om sai");
         }
     
    }
    public static void main(String[] args) {
        m1(true);
    }
//output: om sai
 
}

5.public class Example5 {

     Example5() {
         this(10,10);
         //consrtuctor Example5 in class Example can not be applied to given types
         //required: no argument
         //found:int,int
    }
    public static void main(String[] args) {
        Example5 e=new Example5();
     
    }
 
}

6.class Ba
{
    void fr(int x)
    {
        if(x>10)
        {
            System.out.println("super : "+x);
        }
        else
           fr(++x);
         
    }
}
 class Example6 extends Ba{
   void fr(int x)
    {
        if(x>10)
        {
            System.out.println("sub : "+x);
        }
        else
            super.fr(x);
     
    }
     public static void main(String[] args) {
         Example6 ex=new Example6();
         ex.fr(10);
     }
 
}
//output: sub 11


7.class Bas
{
    void m1()
    {
        System.out.println("base class");
    }
}
 class Example7 extends Bas {
     void m1()
     {
         System.out.println("derived class");
     }
     public static void main(String[] args) {
     
         Example7 e=new Example7();
           Bas b= (Bas)e;
           b.m1();
     }
    //output:derived class
}


upto here perfect questions

last one just guess

8.public class Example9 {
    int x;
    public static void main(String[] args) {
        int x=10;
        Example9 e=new Example9();
        e.m1(x);
        System.out.println("x"+x);
     
    }
    public  void m1(int y)
    {
        x=y*y;
    }
 
}
//output: 10

Monday, June 11, 2012

android concepts and code

http://androidmohan.blogspot.in/2012/06/android-concepts-and-code.html

Android Tools


Android Tools
Emulator –
-          Virtual Device
-          Can develop test and debug apps
-          Allows to run simultaneous apps
ADT – Android Development Tools
-          Plugin for eclipse integrated development environment
-          Increases speed and efficiency for developing android apps.
-          Allows the apps to access DDMS tool from eclipse.
-          Gives the wizard for basic new project creation.
DDMS – Dalvik Debug Monitor Service
-          Works with the emulator or the mobile device
-          Gives the information about the virtual devices and physical devices.
-          Thread and Heap tab for easy display.
-          Screen capture
-          Exploring the files
-          Causes Garbage collection
-          Running Log cat
ADB – Android Debug Bridge
-          Three core components
o   Client
o   Server
o   Daemon – A computer program that runs in the background like a thread. Don’t have user control.
-          Used to manage  the state of an emulator or a device.
-          Used to install any application directly from  PC to mobile or emulator
-          To manage SQLite DB.
AAPT – Android Asset Packaging Tool
-          Used to package the .apk file
-          Developers can use it directly to view , create and update  .zip, .jar and .apk files.
AIDL – Android Interface Description Language
-          Used for Inter process communication.
-          Interface based. Saves developers time.
Sqlite3
-          Allows to access the SQLite data files created and used by android apps
TraceView
-          Provides a graphical viewer for execution logs saved by your app
MKSDCARD
-          Helps you to create a disk image that you can use with the emulator
DX
-          Converts .class files to Android byte code.
ActivityCreator
-          Creates basic files that the developer needs, when the developer not using Eclipse.
Hierarchy Viewer
-          Gives the user interface to the projects and emulator

INSTALLATION

Required SWs
JDK 1.5 or higher
                Eclipse 3.5 or higher
                ADT Plugin for Eclipse
                Android SDK.
Process
                Install JDK and set path up to bin in to environment variables.
                Extract Eclipse and open eclipse.exe file.
                Then  helpàinstall new SW à give path to ADT pluginàthen install ADT plugin.
                Install Android SDK installer file. Then select the files and install.
                Then go to Windowsà preferencesàAndroidà give path of Android SDK, up to the root folder of tools folder.
                Then go to Windowsà Android SDK and AVD managerà Virtual Devicesà Create a new Virtual Device.

Now the Set up is completed.


Generate map key steps in android

https://developers.google.com/maps/documentation/android/mapkey



c:\>keytool.exe -list -alias androiddebugkey -keystore "C:\android\debug.keystore" -storepass android -keypass android

MD5 Key .............................

https://developers.google.com/android/maps-api-signup?hl=en-US

Basic Android Terminal and ADB Shell Command List


While rooting your Android Phone or installing custom ROM on your mobile or partitioning your android device to install applications, requires some android terminal command and ADB (Android Debug Bridge) shell command knowledge. Below is the list of some basic android commands that are being used on Android platform frequently and that every android user
http://kona.kontera.com/javascript/lib/imgs/grey_loader.gif
must know.
 android adb shell

Basic Android Terminal and ADB Shell Command List
1. How to open a cmd in Android Phone
Method 1: “Start” – “”Program”-” “Accessories” – “” Command Prompt ”
Method 2: “Start” – “” Run “, type cmd ENTER
2. How to restart Android Phone
When the phone and Computer is connected to the data cable, you can enter the following command
1
adb shell reboot === ENTER
3. Restart Android into Recovery Mode
With the data cable connected to your phone and computer, enter the following command
1
adb shell reboot recovery === ENTER
4. Convert back to ext2 partition
Restart the phone into Recovery mode, press “Alt + X” into the console. Open cmd and enter the following command
1
2
3
adb shell === ENTER
tune2fs-O ^ has_journal / dev/block/mmcblk0p2 === carriage return
e2fsck / dev/block/mmcblk0p2 === carriage return (optional, can be a problem area in section 2, when used)
5. Pulling applications from Android phone to computer
1
2
adb pull /system/sd/app app
adb pull /system/sd/app-private app-private
6. Pushing applications back to android phone from the computer
1
2
adb push app /system/sd/app
adb push app-private /system/sd/app-private
7. Delete existing apps on Android SD
1
2
adb shell rm -r /system/sd/app
adb shell rm -r /system/sd/app-private
8. Repair gravity System or switch to screen
Sometimes frequent brushing of phone can cause gravity system or switch to screen failure. Just follow the steps below-
Restart the phone into Recovery mode, press “Alt + X” into the console
Open cmd and enter the following command
1
2
mount / data === carriage return
rm / data / misc / akmd * / data / misc / rild * === ENTER
9. Ext2/ext3/ext4 formatted partition
Enter the following command in the cmd
1
2
3
adb remount === ENTER
adb shell === ENTER
rm-r / system / sd / * === carriage return
10. Remove/ system / app under the application
Under normal circumstances / system / app is not under an application. Use the following methods to remove these applications.
Open cmd and enter the following command
1
2
adb remount === ENTER
adb rm / system / app / Stocks.apk === Enter
11. If the start Time is too Long
Just enter the following command in order to view the boot process.
1
adb logcat === ENTER
12. Through Terminal Partition SD card
It will erase everything on your SD card
1
2
3
4
5
$  su
# cd /data
# chmod 555 sdsplit
# /data/sdsplit -fs *size* (add -nc to the end for JFv1.5ADP)
13. From the Recovery Screen, send an update file to your SD card.
1
2
3
adb shell mount /sdcard
adb shell rm /sdcard/update.zip
adb push *filename* /sdcard/update.zip
14. Restoring a nandroid backup via Fastboot
Start command-prompt/terminal cd to the nandroid folder and enter following commands
1
2
3
4
5
6
7
fastboot erase boot
fastboot erase recovery
fastboot flash system system.img
fastboot flash boot boot.img
fastboot flash userdata data.img
fastboot flash recovery recovery.img
fastboot reboot
15. Clear Search History in Android
Search History is accounted for Mobile Memory. It can also leak your privacy information as well. Just follow the steps below to clear android history.
Steps are as follows:
1. Make sure your mobile phone has Root authority.
2. Open the super-terminal.
3. Enter the following command
1
2
su
rm / data / data / com.android.vending / databases / suggestions.db
4. Exit Hyper Terminal and restart the phone.
If you have a good one to add please post it in comment section

Thursday, June 7, 2012

ExampleOnTimePickerDialog



public class ExampleontimepickerdialogActivity extends Activity {
   
/**Private members of the class*/
private TextView displayTime;
    private Button pickTime;
   
    private int pHour;
    private int pMinute;
   
    /** This integer will uniquely define the dialog to be used for displaying time picker. */
    static final int TIME_DIALOG_ID = 0;


    /** Callback received when the user "picks" a time in the dialog */
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                pHour = hourOfDay;
                pMinute = minute;
                updateDisplay();
                displayToast();
            }
        };
   
    /** Updates the time in the TextView */
    private void updateDisplay() {
        displayTime.setText(
            new StringBuilder()
                    .append(pad(pHour)).append(":")
                    .append(pad(pMinute)));
    }
   
    /** Displays a notification when the time is updated */
    private void displayToast() {
        Toast.makeText(this, new StringBuilder().append("Time choosen is ").append(displayTime.getText()),   Toast.LENGTH_SHORT).show();
           
    }
   
    /** Add padding to numbers less than ten */
    private static String pad(int c) {
        if (c >= 10)
            return String.valueOf(c);
        else
            return "0" + String.valueOf(c);
    }
   
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        /** Capture our View elements */
        displayTime = (TextView) findViewById(R.id.timeDisplay);
        pickTime = (Button) findViewById(R.id.pickTime);

        /** Listener for click event of the button */
        pickTime.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(TIME_DIALOG_ID);
            }
        });

        /** Get the current time */
        final Calendar cal = Calendar.getInstance();
        pHour = cal.get(Calendar.HOUR_OF_DAY);
        pMinute = cal.get(Calendar.MINUTE);

        /** Display the current time in the TextView */
        updateDisplay();
       
    }
   
    /** Create a new dialog for time picker */
   
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case TIME_DIALOG_ID:
            return new TimePickerDialog(this,
                    mTimeSetListener, pHour, pMinute, false);
        }
        return null;
    }
}

=============== Xml file ===============

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/timeDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Time will appear here after being selected"
        android:textSize="30sp" />

     <Button
        android:id="@+id/pickTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change the time" />
   
</LinearLayout>


Dialog box examples


  • An alert box with a single button
  • A dialog box with two buttons
  • A dialog box containing a list
  • A dialog box containing checkboxes
Android Platform 1.5




Note:
  • We will be using Android Platform 1.5 for developing our application.
To demonstrate how to create dialog boxes firstly we will be creating a basic application with four buttons and clicking on each of these buttons will display a different kind of dialog box. We will be using XML based layout for our Application's User Interface. Create a new Android project with the following properties -
  • Project Name - DialogBox
  • Name of the Application - Dialog Box
  • Package name - com.botskool.DialogBox
  • Activity Name - Dialog Box 
There will be four buttons and a string as listed below -
  • information string
  • alert button (This button will be used to display an alert box)
  • yesno button (This will be used to display a dialog box with Yes/No buttons)
  • selectlist button (This will be used to display a dialog box which contains a list)
  • selectlistwithcheckbox (This will be used to display a dialog box which contains a list along with checkbox)
Copy the following code in DialogBox.java -







import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class DialogBox extends Activity implements OnClickListener{
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** We need to set up a click listener on the alert button */
        Button alert = (Button) findViewById(R.id.alertbutton);
        alert.setOnClickListener(this); 
        /** We need to set up a click listener on the yesno button */
        Button yesno = (Button) findViewById(R.id.yesnobutton);
        yesno.setOnClickListener(this);
        /** We need to set up a click listener on the selectlist button */
        Button selectlist = (Button) findViewById(R.id.selectlist);
        selectlist.setOnClickListener(this);
        /** We need to set up a click listener on the selectlistwithcheckbox button */
        Button selectlistwithcheckbox = (Button) findViewById(R.id.selectlistwithcheckbox);
        selectlistwithcheckbox.setOnClickListener(this);
    }

    public void onClick(View view) {
     /** check whether the alert button has been clicked */
        if (view == findViewById(R.id.alertbutton)) {
             // Code here to display alert box
        }
         /** check whether the yesno button has been clicked */
        if (view == findViewById(R.id.yesnobutton)) {
            // Code here to display the dialog box with Yes/No button
        }
        /** check whether the selectlist button has been clicked */
        if (view == findViewById(R.id.selectlist)) {
         //Code here to display the dialog box containing a list.   
        }
        /** check whether the selectlistwithcheckbox button has been clicked */
        if (view == findViewById(R.id.selectlistwithcheckbox)) {
         //Code here to display the dialog box with checkbox
        }
    }
}

Copy the following code in main.xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/information"
    />
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Show Alert" 
android:id="@+id/alertbutton"
/>
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Show Yes/No" 
android:id="@+id/yesnobutton"
/>
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Show List" 
android:id="@+id/selectlist"
/>
<Button 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Show List with Checkbox" 
android:id="@+id/selectlistwithcheckbox"
/>
</LinearLayout>

Copy the following code in strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="information">Various kind of dialog boxes -  by www.bOtskOOl.com</string>
    <string name="app_name">Dialog Box</string>
</resources>

Compile and run the Android application and you will get this - 
Dialog Box Application in Android  
Now we need to write the code to create and display different dialog boxes. Let's start with the simple Alert box with single button. Copy the following code inside the if condition which checks whether the alert box has been clicked or not in the OnClick() function as shown below -

/** check whether the alert button has been clicked */
        if (view == findViewById(R.id.alertbutton)) {

             // Create the alert box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

            // Set the message to display
            alertbox.setMessage("This is an alert box.");

            // Add a neutral button to the alert box and assign a click listener
            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                // Click listener on the neutral button of alert box
                public void onClick(DialogInterface arg0, int arg1) {

                    // The neutral button was clicked
                    Toast.makeText(getApplicationContext(), "'OK' button clicked", Toast.LENGTH_LONG).show();
                }
            });

             // show the alert box
            alertbox.show();
        }
To create an Alert box we first use the AlertDialog.Builder subclass. We create an instance of this class with AlertDialog.Builder(Context) and then we set the different properties of alert box such as message and button to be displayed by using setMessage() and setNeutralButton()respectively. After this we display the alert box by using show() function.
setMessage() takes only one argument and that is the string to be displayed to the user. InsetNeutralButton() function the first argument is the name of the button
Also we add a click listener to the (neutral) OK button by passing a function as second argument of setNeutralButton() function and we display a toast notification whenever this button is clicked/activated. Check out the screenshots below.
An alert box in android
ok button clicked in android
You must be wondering what a neutral button is? Well in Android we have three types of button -
  • Positive or Yes
  • Negative or No
  • Neutral or Cancel
You can ONLY add one of each button type to your AlertDialog box. So this limits the number of possible buttons to three: positive, neutral, and negative. As quoted on Android website - These names are technically irrelevant to the actual functionality of your buttons, but should help you keep track of which one does what
Now if we want to create a dialog box with two buttons you just need to use some other type of button apart from neutral button. Say for Yes/No dialog box we use a positive and a negative button then we can their property by calling setPostiveButton() and setNegativeButton() as shown below -

 /** check whether the yesno button has been clicked */
        if (view == findViewById(R.id.yesnobutton)) {
            // Create the dialog box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            // Set the message to display
            alertbox.setMessage("This is a dialog box with two buttons");
             // Set a positive/yes button and create a listener
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                // Click listener 
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
                }
            });
            // Set a negative/no button and create a listener
            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                // Click listener
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                }
            });
            // display box
            alertbox.show();
        }
The functions setPostiveButton() and setNegativeButton() are similar to setNeutralButton(). Check out the screenshots below.
Dialog box with two buttons in android
Yes button of dialog box in android 
Yes No buttons of dialog box in android
No button clicked in android


  • A dialog box containing a list
  • A dialog box containing checkboxes
Lets start with dialog box having a list in it. There is an onClick() function in the DialogBox.java. In this onClick() function we have a if condition to check whether Show List button has been clicked or not as shown below.





 /** check whether the selectlist button has been clicked */
        if (view == findViewById(R.id.selectlist)) {
         //Code here to display the dialog box containing a list.   




        }
We will write our code inside this if condition to create and display the dialog box containing a list. For this we will use Builder sub-class of AlertDialog as we had in the previous two dialog boxes.
To create a list we will use setItem() method of this Builder sub class. The first argument of this setItem() function is an array containing the elements of the list that has to be created and the second argument is the click listener callback function that defines the action to take when the user selects an item. Check out the code given below - 

/** check whether the selectlist button has been clicked */
        if (view == findViewById(R.id.selectlist)) {
         //List items
         final CharSequence[] items = {"Milk", "Butter", "Cheese"};
         //Prepare the list dialog box
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         //Set its title
         builder.setTitle("Pick an item");
         //Set the list items and assign with the click listener
         builder.setItems(items, new DialogInterface.OnClickListener() {
         // Click listener
         public void onClick(DialogInterface dialog, int item) {
                 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
             }
         });
         AlertDialog alert = builder.create();
         //display dialog box
         alert.show();
        }
On selecting a particular item the onClickListener() function will be called which in turn will display a toast notification containing the name of the selected item as shown in screenshots below.
Show List button in android
Dialog box with a list in android
Particular item being selected from the list in Android

Similarly for displaying a dialog box with checkboxes in it we use setSingleChoiceItems() method of the Builder sub class of AlertDialog. The first parameter of this method is again an array of items to be displayed. The second parameter is an integer value for the checkedItem, which indicates the zero-based list position of the default selected item and we use "-1" if don't want any item to be selected by default. The third parameter is the click listener function. Check outthe code given below - 

/** check whether the selectlistwithcheckbox button has been clicked */
        if (view == findViewById(R.id.selectlistwithcheckbox)) {
         //List items
         final CharSequence[] items = {"Milk", "Butter", "Cheese"};
         //Prepare the list dialog box
         AlertDialog.Builder builder = new AlertDialog.Builder(this);
         //Set its title
         builder.setTitle("Pick an item");
         //Set the list items along with checkbox and assign with the click listener
         builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
         // Click listener
         public void onClick(DialogInterface dialog, int item) {
                 Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                 //If the Cheese item is chosen close the dialog box
                 if(items[item]=="Cheese")
                  dialog.dismiss();
             }
         });
         AlertDialog alert = builder.create();
         //display dialog box
         alert.show();
        }
Check out the screenshots shown below.
Show List with Checkboxes button in Android
Dialog Box with checkboxes in Android
Choosing a particular item from list in Android

In the onClickListener() function we have introduced an if condition which checks whether the selected item is Cheese and if it is so we have called a function dialog.dimiss(). This function closes/dismisses the dialog box. Apart from this everything is similar to the code of the previous dialog box i.e. whenever an item is selected the toast notification displays the selected item's name.
An item being selected in a listin dialog box in Android
Toast Notification is displayed after selecting a particular option in Android

So now the complete code of DialogBox.java will be -

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;


public class DialogBox extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** We need to set up a click listener on the alert button */
        Button alert = (Button) findViewById(R.id.alertbutton);
        alert.setOnClickListener(this); 
        /** We need to set up a click listener on the yesno button */
        Button yesno = (Button) findViewById(R.id.yesnobutton);
        yesno.setOnClickListener(this);
        /** We need to set up a click listener on the selectlist button */
        Button selectlist = (Button) findViewById(R.id.selectlist);
        selectlist.setOnClickListener(this);
        /** We need to set up a click listener on the selectlistwithcheckbox button */
        Button selectlistwithcheckbox = (Button) findViewById(R.id.selectlistwithcheckbox);
        selectlistwithcheckbox.setOnClickListener(this);
    }

    public void onClick(View view) {
        /** check whether the alert button has been clicked */
        if (view == findViewById(R.id.alertbutton)) {
             // Create the alert box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            // Set the message to display
            alertbox.setMessage("This is an alert box.");
            // Add a neutral button to the alert box and assign a click listener
            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                // Click listener on the neutral button of alert box
                public void onClick(DialogInterface arg0, int arg1) {
                    // The neutral button was clicked
                    Toast.makeText(getApplicationContext(), "'OK' button clicked", Toast.LENGTH_LONG).show();
                }
            });
             // show the alert box
            alertbox.show();
        }
         /** check whether the yesno button has been clicked */
        if (view == findViewById(R.id.yesnobutton)) {
            // Create the dialog box
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
            // Set the message to display
            alertbox.setMessage("This is a dialog box with two buttons");
             // Set a positive/yes button and create a listener
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                // Click listener 
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "'Yes' button clicked", Toast.LENGTH_SHORT).show();
                }
            });
            // Set a negative/no button and create a listener
            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                // Click listener
                public void onClick(DialogInterface arg0, int arg1) {
                    Toast.makeText(getApplicationContext(), "'No' button clicked", Toast.LENGTH_SHORT).show();
                }
            });
            // display box
            alertbox.show();
        }
        /** check whether the selectlist button has been clicked */
        if (view == findViewById(R.id.selectlist)) {
            //List items
            final CharSequence[] items = {"Milk", "Butter", "Cheese"};
            //Prepare the list dialog box
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //Set its title
            builder.setTitle("Pick an item");
            //Set the list items and assign with the click listener
            builder.setItems(items, new DialogInterface.OnClickListener() {
                // Click listener
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                }
            });
            AlertDialog alert = builder.create();
            //display dialog box
            alert.show();
        }
        /** check whether the selectlistwithcheckbox button has been clicked */
        if (view == findViewById(R.id.selectlistwithcheckbox)) {
            //List items
            final CharSequence[] items = {"Milk", "Butter", "Cheese"};
            //Prepare the list dialog box
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //Set its title
            builder.setTitle("Pick an item");
            //Set the list items along with checkbox and assign with the click listener
            builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
                // Click listener
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                    //If the Cheese item is chosen close the dialog box
                    if(items[item]=="Cheese")
                        dialog.dismiss();
                }
            });
            AlertDialog alert = builder.create();
            //display dialog box
            alert.show();
        }
    }
}