Search Tutorials

Sunday, 29 September 2013

Android Dialog Box Example

Android Dialog Box Example...
Open xml file and below code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="24dp"
        android:layout_marginTop="17dp"
        android:text="CLOSE"
        android:onClick="close" />
</RelativeLayout>


Now open java file and paste below code

package selecom.alert;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
    }
    public void close(View v)
    {
      onCreateDialog(10);
    }

protected Dialog onCreateDialog(int id)
{
      switch(id)
      {
      case 10:
            Builder builder=new AlertDialog.Builder(this);
            builder.setMessage("Activity will close!!!");
            builder.setTitle("Warning...");
            builder.setIcon(R.drawable.warning_image);
//button 
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                 
                  @Override
                  public void onClick(DialogInterface dialog, int which)
                  {
                        Toast.makeText(getApplicationContext(), "Nothing happened", Toast.LENGTH_LONG).show();
                       
                  }
            });
//button
      builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                 
                  @Override
                  public void onClick(DialogInterface dialog, int which)
                  {
                        finish();
                       
                  }
            });
//button
      builder.setNeutralButton("Thinking", new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                  Toast.makeText(getApplicationContext(), "i have to think", Toast.LENGTH_LONG).show();
                 
            }
      });
           
      AlertDialog dialog=builder.create();
      dialog.show();
           
      }
      return super.onCreateDialog(id);
     
}
  
} 

We can use maximum three buttons in dialog box by default and builder is used to create dialog box. Sequence of the buttons to appear on box: positive, neutral than negative.

5 comments:

  1. How to link a range,say 25- 30 from a radio button, to next page. The next page should display as "normal value". For 35-40, it should display "good value".

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Coooooooooooooooool

    ReplyDelete
  4. I used Dialog box for the first time and it was awesome. Thanks

    ReplyDelete
  5. I don't know anything.

    ReplyDelete

Back to Top