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();
}
});
}
}