0

In the following example I have onCreateDialog and showDialog deprecated.

package com.dialogtest;

import android.app.Dialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    CharSequence[] items = {"Google", "Apple", "Microsoft"};

    // Declare the boolean array of same size as items
    boolean[] itemsChecked = new boolean[items.length];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View v) {
        showDialog(1);
    }

    @Override
    protected Dialog onCreateDialog(int id) {

        switch (id) {

            case 1:
                return new AlertDialog.Builder(this)
                        //.setIcon(R.drawable.ic_launcher)
                        .setTitle("This is a dialog with some simple text...")
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {

                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        Toast.makeText(getBaseContext(),
                                                "OK clicked!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                        )

                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {
                                        Toast.makeText(getBaseContext(),
                                                "Cancel clicked!", Toast.LENGTH_SHORT).show();
                                    }
                                }
                        )

                        .setMultiChoiceItems(items, itemsChecked,
                                new DialogInterface.OnMultiChoiceClickListener() {
                                    public void onClick(DialogInterface dialog,
                                                        int which, boolean isChecked) {
                                        Toast.makeText(getBaseContext(),
                                                items[which] + (isChecked ? " checked!" : " unchecked!"),
                                                Toast.LENGTH_SHORT).show();
                                    }
                                }
                        ).create();


        }
        return null;
    }
}

I was suggested to use DialogFragment but I am not so sure.

So I want to know that is it not possible to directly use AlertDialog anymore?

user963241
  • 6,758
  • 19
  • 65
  • 93
  • [http://stackoverflow.com/questions/10285047/showdialog-deprecated-whats-the-alternative](http://stackoverflow.com/questions/10285047/showdialog-deprecated-whats-the-alternative) – M D Jan 17 '17 at 12:13
  • 2
    deprecated doesn't mean that you can't use them. They are just warning that you should now avoid using those methods as they will be removed in future versions. – Vivek Mishra Jan 17 '17 at 12:15
  • try using Appcompat AlertDialog and i don't think switching to appcompat version will affect you much :) – shadygoneinsane Jan 17 '17 at 12:20
  • But If I am not using Appcompat activity then DialogFrament is only alternative? – user963241 Jan 17 '17 at 12:32
  • You can use android.app.AlertDialog in your activity `AlertDialog.Builder alertDialog = new AlertDialog.Builder(YourActivity.this); alertDialog.setMessage("Hi"); alertDialog.show();` – shadygoneinsane Jan 17 '17 at 13:24
  • @shadygoneinsane: You mean I can put this code in OnClick instead of in onCreateDialog? – user963241 Jan 17 '17 at 13:29
  • @user963241 check the answer – shadygoneinsane Jan 17 '17 at 13:38

2 Answers2

0

You should use alert dialog with appcompact version Click Here

shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47
divyansh ingle
  • 340
  • 1
  • 9
0

As per reference docs AlertDialog is a subclass of Dialog that can display one, two or three buttons.

In your case use the below code :

public void onClick(View v) {
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(ExampleActivity.this);
            //.setIcon(R.drawable.ic_launcher)
    alertDialog.setTitle("This is a dialog with some simple text...")
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getBaseContext(),
                                    "OK clicked!", Toast.LENGTH_SHORT).show();
                        }
                    }
            )
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Toast.makeText(getBaseContext(),
                                    "Cancel clicked!", Toast.LENGTH_SHORT).show();
                        }
                    }
            )
            .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
                        public void onClick(DialogInterface dialog,
                                            int which, boolean isChecked) {
                            Toast.makeText(getBaseContext(),
                                    items[which] + (isChecked ? " checked!" : " unchecked!"),
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
            .show();
}
shadygoneinsane
  • 2,226
  • 1
  • 24
  • 47