0

I have written the following code in Android Studio to generate a list of String items. However, the problem is that when I run the code, the Android Studio does not indicate the list. I do not exactly know what is wrong with my code.

package com.example.practiceactivities;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

AlertDialog.Builder builder;

Button button;

TextView txt;

 String [] Colors = {"Red","Green","Blue"};

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

     builder = new AlertDialog.Builder(MainActivity.this);
     button = findViewById(R.id.button);
     txt = findViewById(R.id.textt);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            builder.setTitle("Color Box")
                    .setMessage("Choose Your color")
                    .setItems(Colors, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {

                            String color = Colors[i];
                            txt.setText(color);
                        }

                    })

                    .create().show();
        }
    });
}
}

enter image description here

gioravered
  • 1,758
  • 3
  • 19
  • 30
MMd.NrC
  • 91
  • 7
  • following this link (https://stackoverflow.com/questions/22559706/alertdialog-why-cant-i-show-message-and-list-together) it tells that "Because the list appears in the dialog's content area, the dialog cannot show both a message and a list and you should set a title for the dialog with setTitle()." – Paolino L Angeletti Mar 15 '23 at 08:47
  • 1
    So, try to set title as "Choose color" and remove message. – Paolino L Angeletti Mar 15 '23 at 08:47

0 Answers0