Thursday, March 1, 2012

display popup window in android

This program is for display a popup window in android





Step 1) Your main java file is here


package al.ert;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.TextView;

public class AlertActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
       
        AlertDialog.Builder adb = new AlertDialog.Builder(this);
        adb.setTitle("Title of the window");
        adb.setMessage("Message of the window");
       
        final TextView tv=(TextView)findViewById(R.id.textView1);
       
        adb.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
        {
    //Action for 'Ok' Button
                tv.setText("You have clicked ok");
                        }
        });
       
       
        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
         {
                        public void onClick(DialogInterface dialog, int id)
                                        {
                                        // Action for 'Cancel' Button
                                                                                tv.setText("You have clicked Cancel");
                                        dialog.cancel();
                        }
        });
        adb.setIcon(R.drawable.icon);
adb.show();
    }
}


Step 2) Content of main.xml file

<?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"
    android:weightSum="1">
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<TextView android:id="@+id/textView1" android:text="TextView" android:layout_height="wrap_content" android:layout_weight="0.05" android:layout_width="156dp"></TextView>
</LinearLayout>

2 comments: