0

I am trying to have a ScrollView in the message part of the AlertDialog. But, i am unable to get the desired result. Following is the relevant piece of code. If you find something incorrect in the way i am building the custom view, please let me know. TIA.

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_SEARCH:
        dialogLayoutOuter = new ScrollView(this);
        LayoutParams scroll_lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        dialogLayoutOuter.setLayoutParams(scroll_lp);
        dialogLayoutOuter.setFillViewport(true);
        dialogLayoutOuter.setVerticalScrollBarEnabled(true);

        dialogLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp_lv = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        dialogLayout.setLayoutParams(lp_lv);            
        dialogLayout.setOrientation(LinearLayout.VERTICAL);

        int bgColor = 0xFF00FFFF;
        int bgColorBlack = 0xFF000000;
        LinearLayout.LayoutParams lp_divider = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,2);
        LinearLayout.LayoutParams lp_text = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        LinearLayout.LayoutParams lp_lv1 = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
        ListIterator<ListView> itr = myListViewList.listIterator();
        while(itr.hasNext()) {
            ListView lv = itr.next();
            TextView myTextView = new TextView(this);
            myTextView.setText(lv.getTag().toString());
            myTextView.setBackgroundColor(bgColor);
            myTextView.setTextColor(bgColorBlack);
            myTextView.setGravity(Gravity.CENTER);
            dialogLayout.addView(myTextView, lp_text);
            LinearLayout llDivider = new LinearLayout(this);
            llDivider.setBackgroundColor(bgColorBlack);
            dialogLayout.addView(llDivider, lp_divider);
            dialogLayout.addView(lv, lp_lv1);
        }

        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);

        dialogLayoutOuter.addView(dialogLayout);
        dialogBuilder.setView(dialogLayoutOuter);           
        activityDialog = dialogBuilder.create();
        return activityDialog;
    }
    return null;
}
VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • is the content large enough that the scrollview should scroll? I mean do you have content thats not visible? Why do you don't use an xml layout? – WarrenFaith May 08 '11 at 11:26
  • Hmmmm the fact that you're iterating through a **list of ListViews** makes me think there's something pretty wrong with your code. – dmon May 08 '11 at 12:48
  • @WarrenFaith Yes. The content is large enough not to fit in one screen length. I will not know how many lists i have to show until run-time - that's why i can't use xml layout. – VJ Vélan Solutions May 08 '11 at 15:29
  • @dmon I will know how many lists to show only at runtime. Hence, the iteration. There is nothing with that, is it? – VJ Vélan Solutions May 08 '11 at 15:31
  • check my answer here http://stackoverflow.com/a/33098898/1881527 – Melbourne Lopes Oct 13 '15 at 09:25

1 Answers1

1

A ListView doesn't work inside a ScrollView, because both have a scrolling mechanism built in.

I recommend to replace the ListViews you create with simple LinearLayouts. Easiest way is to create an xml layout, that contains everything you would show inside a row and an xml layout with the ScrollView.

Inflate the ScrollView with LayoutInflater, find the LinearLayout where you want to add your rows and add them there. Finally set the ScrollView as a View of your Dialog.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
  • How is the recommended approach different from taking all my rows from existing ListViews and stuffing it one giant ListView and setting that as my dialog's view? Thanks for your analysis and suggestion. – VJ Vélan Solutions May 08 '11 at 18:25
  • Does I understand it correctly, that you want to display the rows from your ListView (where ever that is displayed) and display that in a dialog? If I am right, forget that approach. You should still have the data which filled the ListView, that use that to create a new dialog and iterated as described above over the data and create your dialog with that. – WarrenFaith May 08 '11 at 21:19
  • Thanks. I think that's what i will settle for. – VJ Vélan Solutions May 08 '11 at 23:18