Adding a Vertical Scrollbar to an Alertdialog in Android

Adding vertical scroll bar to an alert dialog

Create a custom dialog you can customize it as you wish. In the dialog box layout xml file

surround your RelativeLayout or LinearLayout with scrollable like below code

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical"
android:layout_weight="1">

<!--Your other text views, buttons... etc surrounded by RelativeLayout
or LinearLayout goes here-->

</ScrollView>

When the content exceeds dlalog box size it will display the scroll bar.
Hope this will help you :)

Android - How do I make this alert dialog scrollable?

This solution is take from this post.

In order for a view to scrollable, it must be nested inside of a ScrollView container:

<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView />
<Button />
</LinearLayout>
</ScrollView>

Note that a ScrollView container can only have one child layout view. It is not possible, for example, to place a TextView and Button in a ScrollView without the LinearLayout.

Android scrollable AlertDialog programmatically

i tried it, but it forces my app to stop.

ScrollView can have just a child. It is an Android constraint. So, doing

    final LinearLayout layout = new LinearLayout(this);
ScrollView scrollView = new ScrollView(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);

scrollView.addView(t[i]);
}

will make your app crash, because you are adding more than one child to the ScrollView. But if you do

final LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
CheckBox[] t=new CheckBox[15];
for (i=0;i<currentBattery.temperatureNumber;i++)
{
t[i]=new CheckBox(this);
t[i].setText("title");
t[i].setChecked(false);
t[i].setOnCheckedChangeListener(this);

layout.addView(t[i]);
}
final ScrollView scrollView = new ScrollView(this);
scrollView.addView(layout);

it should work smoothly.

Scroll View not working in Alert Dialog

You can create custom layout for your dialog and set any property easily.

For your requirement, Crate a layout for your required Dialog. Put android:scrollbars = "vertical" in your textView inside your layout. And textview.setMovementMethod(new ScrollingMovementMethod());.

You can set custom layout on your by following method.

 public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);

TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);

Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();

}


Related Topics



Leave a reply



Submit