Friday, September 27, 2013

Android-Dialog Window

Android-Dialog Window

Displaying a Dialog Window

There are times where you need to display a dialog window to get a confirmation from the user. In this case, you can override the onCreateDialog() protected method defined in the base Activity class to display a dialog window. The following Try It Out shows you how. Displaying a Dialog Window Using an Activity

1. Using Eclipse, create a new Android project and name it Dialog.
2. Add the following statements in bold to the main.xml file:
android:orientation=”vertical”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/hello” />

Android-Dialog Window

3. Add the following statements in bold to the MainActivity.java file:
package net.learn2develop.Dialog;
import android.app.Activity;
import android.os.Bundle;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
boolean[] itemsChecked = new boolean [items.length];
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});
}

Android-Dialog Window

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle(“This is a dialog with some simple text...”)
.setPositiveButton(“OK”, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
“OK clicked!”, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(“Cancel”, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
“Cancel clicked!”, Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? “ checked!”:
“ unchecked!”),
Toast.LENGTH_SHORT).show();
}
}
)
.create();
}
return null;
}
}

Android-Dialog Window

4. Press F11 to debug the application on the Android Emulator. Click the button to display the dialog. Checking the various checkboxes will cause the Toast class to display the text of the item  checked/ unchecked. To dismiss the dialog, click the OK or Cancel button

Android-Dialog Window



To display a dialog, you first override the onCreateDialog() method in the Activity class:
@Override
protected Dialog onCreateDialog(int id) {
//...
}
This method is called when you call the showDialog() method:
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(0);
}
});

Android-Dialog Window

The onCreateDialog() method is a callback for creating dialogs that are managed by the activity. When you call the showDialog() method, this callback will be invoked. The showDialog() method accepts an integer argument identifying a particular dialog to display.
To create a dialog, you use the AlertDialog class’s Builder constructor. You set the various properties,
such as icon, title, and buttons, as well as checkboxes:
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle(“This is a dialog with some simple text...”)
.setPositiveButton(“OK”, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
“OK clicked!”, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton(“Cancel”, new
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton)
{
Toast.makeText(getBaseContext(),
“Cancel clicked!”, Toast.LENGTH_SHORT).show();
}
})
.setMultiChoiceItems(items, itemsChecked, new
DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
Toast.makeText(getBaseContext(),
items[which] + (isChecked ? “ checked!”:
“ unchecked!”),
Toast.LENGTH_SHORT).show();
}
}
)
.create();
}
return null;
}
The preceding code sets two buttons: OK and Cancel, using the setPositiveButton() and setNegativeButton() methods, respectively. You also set a list of checkboxes for users to choose via the setMultiChoiceItems() method. For the setMultiChoiceItems() method, you passed in two arrays: one for the list of items to display and another to contain the value of each item to indicate if they are checked. When each item is checked, you use the Toast class to display a message

Upload UIImage as base64 String

Upload UIImage as Base64 String (Upload UIImage as string) //Select Pic From Camera or Gallery       @objc func btnPro...