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
End Incoming Call Programmatically
How to Fix Android Studio Getting Stuck Executing Gradle Tasks
Android Wsdl/Soap Service Client
Move Snackbar Above The Bottom Bar
Adb Shell Su Works But Adb Root Does Not
Designing Android Apps for Tablets
Android Studio and Gradle Build Error
How to Access an Existing Sqlite Database in Android
How to Get Md5 from Debug.Keystore File
Signing My Android Application as System App
How to Run a Specific Android App Using Terminal
Android Build Gradle Is Too Slow (Dependency Resolution)
Android Understanding Heap Sizes
How to Get The Application's Icon from The Package Name
Use 32-Bit Jni Libraries on 64-Bit Android