how to Display a Dialog Window Using an Activity ? - Android Tutorial


hey guys, to day i am going to give you a fresh tutorial on android. as many people wants a dialog window in their Android apps.

To Display a dialog window, you must be familiar with basic android coding functionality!

android tutorial
1 . Using Eclipse, create a new Android project and name it Dialog. 
2 . Add the following statements in bold to the main.xml file:
version=”1.0” encoding=”utf-8”?> 
”http://schemas.android.com/apk/res/android

android:orientation=”vertical”

android:layout_width=”fill_parent”

android:layout_height=”fill_parent” > < span="">

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”@string/hello” />

span="">

android:id=”@+id/btn_dialog”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”Click to display a dialog” />

3 .        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);

}

});

    }

@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();

}

})

3 . 

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.
how to Display a Dialog Window Using an Activity ?

{ 0 comments... read them below or add one }

Post a Comment