-1

I have a List in an AlertDialog;

 public void thesaurusBtnAction(){
    // setup the alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Synonyms List");

    // add a list
    String[] someList = {"a", "b", "c", "d", "e","a","b"};
    builder.setItems(someList, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which) {
                case 0: 
                case 1:
                case 2:
                case 3:
                case 4:
            }
        }
    });

    // create and show the alert dialog
    AlertDialog dialog = builder.create();
    dialog.show();
}

I want to make the list in here a scrollable one and also make the AlertDialog size limit to a certain size as the AlertDialog try to increase it's size as the list grows.

LordDraagon
  • 521
  • 12
  • 31
  • My bad it's a duplicate of the mentioned question. I thought it will not be scrollable if I just adjusted the size with setLayout(). I'll mark it as duplicate – LordDraagon Apr 29 '18 at 06:39

2 Answers2

0

Enclose the layout within a ScrollView. In the enclosed layout, you can set android:scrollbars to vertical. Use builder.setView to display the ScrollView in the dialog.

jspcal
  • 50,847
  • 7
  • 72
  • 76
0
 public void thesaurusBtnAction(){
    // setup the alert builder
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Synonyms List");
    builder.setView(LayoutInflater.from(this).inflate(R.layout.dialoglayout,null));


    // create and show the alert dialog
    AlertDialog dialog = builder.create();
    dialog.show();
}

dialoglayout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:scrollbars="vertical">
    <!--Any of your code-->
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>

Yash Verma
  • 59
  • 1
  • 6