JDev
JDev

Reputation: 2531

How to add scrolling to the "Android-Dialog"?

Tell me how do I add scrolling to the "MyDialog extends Dialog".

There MyDialog which has components (EditText, etc.), if I want to enter something in the field, leaves keyboard, and some content is lost out of scope. For example, to type something in another field to hide the keyboard, then select a different input field. It would be desirable that there was no need to hide the keyboard to scroll the contents of fire, if not all fit.

Thank you.

Upvotes: 0

Views: 2158

Answers (3)

Mike
Mike

Reputation: 2593

I had to do something like this recently, hopefully this might help you out:

    // Create a ScrollView so the dialog can scroll
    ScrollView scrollView = new ScrollView(getActivity());
    scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));

    // Create layout for the controls in the dialog
    LinearLayout lay = new LinearLayout(getActivity());
    lay.setOrientation(LinearLayout.VERTICAL);
    lay.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    // Create a TextView to add to the dialog
    TextView productNameLabel = new TextView(getActivity());
    productNameLabel.setText("Some text");
    productNameLabel.setGravity(Gravity.CENTER);
    productNameLabel.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT, 1f));

    // Add the views to the layout
    lay.addView(productNameLabel);

    // Add the layout to the scrollview
    scrollView.addView(lay);

    // Create the dialog
    final AlertDialog.Builder b = new AlertDialog.Builder(getActivity())
            .setTitle("Dialog Title")
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do something when OK is clicked
                }

            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do something when Cancel is clicked
                }

            });

    // Tell the dialog to use the ScrollView
    b.setView(scrollView);

    // Show the dialog
    b.create().show();

It's probably not the best way of doing things, but I'm new to Android and what I've done works :)

Upvotes: 1

Manuel del Castillo
Manuel del Castillo

Reputation: 88

Just add an ScrollView between your dialog and its contents. If you add next field support to the keyboard, this is just enough. And you can scroll the view by hand.

Upvotes: 0

Sandeep
Sandeep

Reputation: 1814

you can build layout having scroll bar that you want and add it to dialog building custom dialog.

Upvotes: 0

Related Questions