USER9561
USER9561

Reputation: 1084

How to add multiple textviews in dialog box

I want to add multiple TextView's in a dialog box. I have tries this but it shows only one TextView.

    AlertDialog.Builder alert = new AlertDialog.Builder(context);
                alert.setTitle("Alert Dialog With EditText"); //Set Alert dialog title here
                alert.setMessage("Enter Your Name Here"); //Message here
            for (int i =0; i<20; i++) {
                // Set an EditText view to get user input
                final TextView input = new TextView(context);
                input.setText("" + i);
                input.setPadding(5, 5, 5, 5);
                input.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String srt = "clickd";
                        Toast.makeText(context, srt, Toast.LENGTH_LONG).show();

                    }
                });
                alert.setView(input);
            }
                alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {

                        String srt = "Dialogg";
                        Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
                    } // End of onClick(DialogInterface dialog, int whichButton)
                }); //End of alert.setPositiveButton
                alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Canceled.
                        dialog.cancel();
                    }
                }); //End of alert.setNegativeButton
                AlertDialog alertDialog = alert.create();
                alertDialog.show();

I want to show numbers from 1 to 20 here in dialog box. Please help me how to do this.

Upvotes: 1

Views: 2375

Answers (2)

TharakaNirmana
TharakaNirmana

Reputation: 10353

You can use a custom alert dialog. With this approach you will have more control over the alert dialog:

http://www.mkyong.com/android/android-custom-dialog-example/

Upvotes: 0

Andy Joyce
Andy Joyce

Reputation: 2832

What i would do is create a linear layout and then add the text views to the linear layout and then add the linear layout to the dialog.

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

for (int i =0; i<20; i++) {
            // Set an EditText view to get user input 
            final TextView input = new TextView(context);
            input.setText("" + i);
            input.setPadding(5, 5, 5, 5);
            input.setOnClickListener(new View.OnClickListener() {
                @Override 
                public void onClick(View v) {
                    String srt = "clickd";
                    Toast.makeText(context, srt, Toast.LENGTH_LONG).show();

                } 
            }); 
layout.addView(input); 

then

alert.setView(layout); 

Upvotes: 6

Related Questions