Monday, October 31, 2011

AlertBox

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/hello"
    />
    <Button android:id="@+id/btn1"
    android:layout_width="150dip"
    android:layout_height="wrap_content"
    android:text="Click Me">
    </Button>
</LinearLayout>


AlertActivity.java



package com.adsample;

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 AlertActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button btnview=(Button) (findViewById(R.id.btn1));
        btnview.setOnClickListener(this);
       
        }

public void onClick(View v) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("Hello User");
alertbox.setPositiveButton("+ve", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
           
                Toast.makeText(getApplicationContext(), "+vebutton clicked", Toast.LENGTH_LONG).show();
                onPause();
                }
        });
alertbox.setNeutralButton("neutral", new DialogInterface.OnClickListener() {

         
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "Neutral button clicked", Toast.LENGTH_LONG).show();
                onPause();
            }
        });
alertbox.setNegativeButton("-ve", new DialogInterface.OnClickListener() {

         
            public void onClick(DialogInterface arg0, int arg1) {
                Toast.makeText(getApplicationContext(), "-ve button clicked", Toast.LENGTH_LONG).show();
                onPause();
            }
        });

alertbox.show();
}
}







Android 2.2 with Min SDK Version :8 was used.



No comments:

Post a Comment