askvictor
askvictor

Reputation: 3819

Android AlertDialog not showing when show() method called

I've got the following code where I'm attempting to show a simple list of options in a translucent activity. However, the dialog is not showing up at all, yet no exceptions are being raised. Any ideas?

public class SendToActivity extends AppCompatActivity {
    List<String[]> players;
    String[] chosen_player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(SendToActivity.this);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
        for(String[] player: players){
            adapter.add(player[0]);
        }
        builder.setTitle("Which Player?");
        builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                chosen_player = players.get(item);
            }
        });
        AlertDialog dialog = builder.create();
        dialog.show();

Upvotes: 1

Views: 4911

Answers (4)

askvictor
askvictor

Reputation: 3819

Turned out I was calling finish() on the activity too soon, which basically killed the dialog as soon as it started. Still getting my head around asynchronous programming :/

Upvotes: 0

Kaveesh Kanwal
Kaveesh Kanwal

Reputation: 1763

Try this code on click of your item:

AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setMessage(context.getResources().getString(R.string.your_message));
            dialog.setPositiveButton(context.getResources().getString(R.string.positive_button_string), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    //your code
                }
            });
            dialog.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                   //your code when cancel is clicked
                }
            });
            dialog.show();

Upvotes: 0

Parth Bhayani
Parth Bhayani

Reputation: 1924

Try this,

AlertDialog.Builder alertBuilder; // Call this variable globally.

alertBuilder = new AlertDialog.Builder(SignUp.this);

            alertBuilder.setTitle(GlobalFields.DIALOG_TITLE);

            // Set an EditText view to get user input
            final EditText input = new EditText(SignUp.this);
            input.setHint("Enter Code");
            alertBuilder.setView(input);

            alertBuilder.setCancelable(false)
                    .setMessage("Please enter verification code from email")
                    .setPositiveButton("Ok", null);

            alertBuilder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {

                            dialog.cancel();
                        }
                    });

            // create alert dialog
            final AlertDialog alertDialog = alertBuilder.create();
            alertDialog.setOnShowListener(new OnShowListener() {

                @Override
                public void onShow(DialogInterface arg0) {
                    // TODO Auto-generated method stub
                    Button btn = alertDialog
                            .getButton(AlertDialog.BUTTON_POSITIVE);

                    btn.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View arg0) {
                            // TODO Auto-generated method stub

                            try {
                                InputMethodManager inputManager = (InputMethodManager) getApplicationContext()
                                        .getSystemService(
                                                Context.INPUT_METHOD_SERVICE);
                                inputManager.hideSoftInputFromWindow(
                                        input.getWindowToken(), 0);

                                // do your stuff
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            });

            // show it
            alertDialog.show();

Upvotes: 1

KishuDroid
KishuDroid

Reputation: 5451

You can use below code for showing List in Dialog.

    final Dialog dialog = new Dialog(mContext);
    dialog.setContentView(R.layout.dialog_list);
    dialog.setTitle("Select Continent");
    final ListView listView = (ListView) dialog.findViewById(R.id.list);

    dialog.show();
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
            android.R.layout.simple_list_item_1, android.R.id.text1, "Pass your arrayList");
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            int itemPosition = position;


            strContinent = (String) YourArrayList.get(position);

            dialog.cancel();

        }

    });

dialog_list.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <ListView
             android:id="@+id/list"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" >

         </ListView>

    </LinearLayout>

Happy Coding ..

Upvotes: 0

Related Questions