Q.1 Explain the dialog and also explain the Demonstrate working dialog in android. - A dialog is a small window that prompts the user to make a decision or enter additional information. - A dialog does not fill the screen. - It is normally used for modal events that require users to take an action before they can proceed. - Design the dialog o The Dialog class is the base class for dialogs, but you should avoid instantiating Dialog directly. Instead, use one of the following subclasses: AlertDialog A dialog that can show a title, up to three buttons, a list of selectable items, or a custom layout. DatePickerDialog or TimePickerDialog A dialog with a pre-defined UI that allows the user to select a date or time. DialogFragment A class provides all the controls you need to create your dialog and manage its appearance, instead of calling methods on the Dialog object. - Write a program to Creating dialog package com.example.alertdialog; import com.example.alertdialog.*; import android.os.bundle; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.intent; import android.view.menu; import android.view.view; public class MainActivity extends Activity protected void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); public void open(view view) AlertDialog.Builder alertdialogbuilder = new AlertDialog.Builder(this); Prepared by : Dhaval Patel 1
alertdialogbuilder.setmessage(r.string.decision); alertdialogbuilder.setpositivebutton(r.string.positive_button, new DialogInterface.OnClickListener() public void onclick(dialoginterface arg0, int arg1) Intent positveactivity = new Intent(getApplicationContext(),com.example.alertdialog.Po sitiveactivity.class); startactivity(positveactivity); ); alertdialogbuilder.setnegativebutton(r.string.negative_button, new DialogInterface.OnClickListener() ); public void onclick(dialoginterface dialog, int which) Intent negativeactivity = new Intent(getApplicationContext(),com.example.alertdialog.Ne gativeactivity.class); startactivity(negativeactivity); AlertDialog alertdialog = alertdialogbuilder.create(); alertdialog.show(); public boolean oncreateoptionsmenu(menu menu) // Inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); Prepared by : Dhaval Patel 2
Q.2 List and explain the different Method type & description in android. Sr.No Method type & description seticon(drawable icon) 1 This method set the icon of the alert dialog box. setcancelable(boolean cancelable) 2 This method sets the property that the dialog can be cancelled or not setmessage(charsequence message) 3 This method sets the message to be displayed in the alert dialog setmultichoiceitems(charsequence[] items, boolean[] checkeditems, DialogInterface.OnMultiChoiceClickListener listener) 4 This method sets list of items to be displayed in the dialog as the content. The selected option will be notified by the listener setoncancellistener(dialoginterface.oncancellistener oncancellistener) 5 This method Sets the callback that will be called if the dialog is canceled. settitle(charsequence title) 6 This method set the title to be appear in the dialog Q.3 What is menu in android? Explain the custom menu and system menu in android. OR Write the different between custom menu and system menu. - Menus are a common user interface component in many types of applications. - It provide a familiar and consistent user experience. - You should use the Menu APIs to present user actions and activities using menu. - All activity used using menu. - System menu o Most of the mobile manufacturers include a hidden service menu in their mobile handsets. o These menus allow users (or developers) to access some technical information and service to access service details etc. o For that purpose there are design system menu - Custom menu o We implemented our own custom menu to match the look and feel of the app. o The good thing about the custom menu is that, it fits right inside the actual menu container provided by Android. o A devices that doesn t have a hardware button, it shows as a popup, and one those that have a hardware menu button, it shows on the side of the menu is called custom menu. Prepared by : Dhaval Patel 3
Q.4 Create and Use Handset menu Button. Or Create menu in android. - Android application menu is one of the important user interface entity which provides some action options for a particular view - Step for creation. 1. Create a new project File New Android Project and give activity name as AndroidMenusActivity. 2. Now create an XML file under res/layout folder and name it as menu.xml. 3. Open menu.xml file and type following code. In the following code we are creating a single menu with 6 menu items. Each menu item has an icon and title for display the label under menu icon. Also we have id for each menu item to identify uniquely.» menu.xml <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <!-- Single menu item Set id, icon and Title for each menu item --> <item android:id="@+id/menu_bookmark" android:icon="@drawable/icon_bookmark" android:title="bookmark" /> <item android:id="@+id/menu_save" android:icon="@drawable/icon_save" android:title="save" /> <item android:id="@+id/menu_search" android:icon="@drawable/icon_search" android:title="search" /> <item android:id="@+id/menu_share" android:icon="@drawable/icon_share" android:title="share" /> <item android:id="@+id/menu_delete" android:icon="@drawable/icon_delete" android:title="delete" /> <item android:id="@+id/menu_preferences" android:icon="@drawable/icon_preferences" android:title="preferences" /> </menu> Prepared by : Dhaval Patel 4
4. Now open your main Activity class file (AndroidMenusActivity.java) and type following code. In the following code each menu item is identified by its ID in switch case statement.» AndroidMenusActivity.java package com.androidhive.androidmenus; import android.app.activity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.widget.toast; public class AndroidMenusActivity extends Activity public void oncreate(bundle savedinstancestate) super.oncreate(savedinstancestate); setcontentview(r.layout.main); // Initiating Menu XML file (menu.xml) public boolean oncreateoptionsmenu(menu menu) MenuInflater menuinflater = getmenuinflater(); menuinflater.inflate(r.layout.menu, menu); /** * Event Handling for Individual menu item selected * Identify single menu item by it's id * */ public boolean onoptionsitemselected(menuitem item) switch (item.getitemid()) case R.id.menu_bookmark: // Single menu item is selected do something // Ex: launching new activity/screen or show alert message Toast.makeText(AndroidMenusActivity.this, "Bookmark is case R.id.menu_save: Toast.makeText(AndroidMenusActivity.this, "Save is case R.id.menu_search: Toast.makeText(AndroidMenusActivity.this, "Search is Prepared by : Dhaval Patel 5
case R.id.menu_share: Toast.makeText(AndroidMenusActivity.this, "Share is case R.id.menu_delete: Toast.makeText(AndroidMenusActivity.this, "Delete is case R.id.menu_preferences: Toast.makeText(AndroidMenusActivity.this, "Preferences is default: return super.onoptionsitemselected(item); 5. Finally run your project by right clicking on your project folder Run As 1 Android Application to test your application. On Android Emulator click on Menu Button to launch menu. Q.5 Explain and create android pop up menu. - Android Popup Menu displays the menu below the anchor text if space is available otherwise above the anchor text. - It disappears if you click outside the popup menu. Prepared by : Dhaval Patel 6
Activity_main.xml popup_menu.xml Prepared by : Dhaval Patel 7
Activity class Prepared by : Dhaval Patel 8
Q.6 Explain Toast in android. How it create. - Toast is a solution for android developer when required to notify user about an operation without expecting any user input. - This provides a small popup that displays for a small period and display out automatically after timeout. - Sometimes developers use this for debugging. - For example, some of the app shows Press back once to Exit message when pressed back button in home page. Another real-time example is Gmail app, It shows a Toast, when a mail message is saved to draft. How to Create a Toast Checkout below code snippet to show an simple toast in Android //display in short period of time Toast.makeText(getApplicationContext(), "Your toast message.", Toast.LENGTH_SHORT).show(); //display in long period of time Toast.makeText(getApplicationContext(), "Your toast message", Toast.LENGTH_LONG).show(); Q.7 Basic operation of SQLite Database - Creating the database o SQLite database file with two tables: One with and one without a PRIMARY KEY column - Adding new columns o After creating data base we can add new coloum using this operation - Inserting and updating rows o We can add or remove the rows - Creating unique indexes o For searching the data we can create the unique index - Querying the database - Selecting rows o We can use the query statement for done different process like insert, update, delete etc. - Security and injection attacks - Date and time operations o It does not only allow us to insert dates and times in various different formats only standard format which we implement. - Retrieving column names Prepared by : Dhaval Patel 9