Naseeb Sheoran
Naseeb Sheoran

Reputation: 473

Adding vertical scroll bar to an alert dialog

I am making an alert dialog in an application.Because of more text present in the alert dialog,i want to put vertical scroll bar in the alert dialog.I have tried many options,But nothing is working.Please tell me how to solve this problem.This is the code :

AlertDialog.Builder HelpOnButtonDialog ;
        HelpOnButtonDialog = new AlertDialog.Builder(this);
        TextView HelpOnButtonView = new TextView(this);
        HelpOnButtonView.setSingleLine(false);
        HelpOnButtonView.setTextColor(getResources().getColor(R.color.dark_green));
        HelpOnButtonView.setText("hello");  
        Button HelpOnButton = new Button(this);
        HelpOnButton.setHeight(20);
        HelpOnButton.setWidth(20);
        HelpOnButton.setText("Ok");
        HelpOnButton.setOnClickListener(HelpOnButtonClickListener);
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setLayoutParams( new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(1); 
        linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
        linearLayout.addView(HelpOnButtonView);
        linearLayout.addView(HelpOnButton);

        ScrollView sv = new ScrollView(this);
        sv.pageScroll(0);
        sv.setBackgroundColor(0);
        sv.setScrollbarFadingEnabled(true);
        sv.setVerticalFadingEdgeEnabled(false);
        sv.addView(linearLayout);
        alertHelp = HelpOnButtonDialog.create();
        alertHelp.setView(sv);
        alertHelp.show();

Upvotes: 0

Views: 3423

Answers (1)

jayangaVliyanage
jayangaVliyanage

Reputation: 83

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 :)

Upvotes: 3

Related Questions