A dialog is usually a small window that appears in front of the current Activity.
The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are
normally used for notifications that should interupt the user and to perform short tasks that
directly relate to the application in progress (such as a progress bar or a login prompt).
AlertDialog box with Single Button
Here is an example which showing how we can create AlertDialog box in android.
Creation
AlertDialog alert=new AlertDialog.Builder(AlertDialogExp.this).create();
where AlertDialogExp.this is the main class.
Set Title
You can set the title of the AlertDialog like this:
alert.setTitle("Next Activityyy...");
Set Message
you can set the message body of the AlertDialog obx like this:
alert.setMessage("do you want to go net Activity?");
Set Button(S)
Next step is setting the button/buttons:
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(AlertDialgExpActivity.this,second.class);
startActivity(i);
}
});
Show your dialog
Don't forget to show your dialog
alert.show();
Example Program
AlertDialgExpActivity.class
package com.ann;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AlertDialgExpActivity extends Activity {
//Button b;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickExp(View v){
// b=(Button)findViewById(R.id.button);
AlertDialog alert=new AlertDialog.Builder(this).create();
alert.setTitle("Next Activityyy...");
alert.setMessage("Do you want to go next Activity?");
alert.setButton("Ok",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent i=new Intent(AlertDialgExpActivity.this,second.class);
startActivity(i);
}
});
alert.show();
}
}
second.class
package com.ann;
import android.app.Activity;
import android.os.Bundle;
public class second extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sec);
}
}
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:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the button to view AlertDialog"
android:textColor="#FF0000"
android:textStyle="bold"
android:layout_marginTop="120dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:layout_marginTop="50dp"
android:onClick="onClickExp"
android:text="Click"
android:textColor="#FF0000" />
</LinearLayout>
sec.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is the Second Page"
android:layout_marginTop="200dp"
android:layout_marginLeft="40dp"
android:textColor="#FF0000"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
No comments:
Post a Comment