Friday, 9 March 2012

Display Alert box in Android




**************SampleAppActivity.java*************


package com.sampleapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class SampleAppActivity extends Activity {
/** Called when the activity is first created. */
private Button btn_go;
AlertDialog alertDialog;
EditText nw_name;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn_go = (Button) findViewById(R.id.btn_go);
btn_go.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

alertDialog = new AlertDialog.Builder(SampleAppActivity.this).create();
alertDialog.setTitle("Values...");
alertDialog.setMessage("hi");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                      return;
                }
});
alertDialog.show();
}
});
}
}


***************main.xml***************

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

    <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
   
    <TextView
android:text="@string/name"
android:layout_marginLeft="10dip"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content" />

    <EditText
android:id="@+id/nw_name"
android:text=""
android:layout_width = "fill_parent"
android:layout_height = "wrap_content" />

    <Button
android:id="@+id/btn_go"
android:text="@string/go"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
    ></Button>

</LinearLayout>

**************strings.xml***************
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">hi, AlertActivity!</string>
<string name="app_name">SampleApp</string>
<string name="name">Name</string>
<string name="designation">Designation</string>
<string name="company">Company</string>
<string name="go">GO</string>

</resources>

No comments:

Post a Comment