Skip to content Skip to sidebar Skip to footer

How To Show Scrollbars On A Textview Programmatically

I have searched and searched to find an answer to how to add a vertical (or horizontal) scrollbar to a TextView without having to use the XML just to add the line: android:scrollba

Solution 1:

Rusian Yanchyshyn, posted the key in his answer at Android: Enable Scrollbars on Canvas-Based View

With the help of an anonmous class & an initializer block we can now do the following:

TextViewtextViewWithScrollBars=newTextView(context)
            {
                {
                    setVerticalScrollBarEnabled(true);
                    setMovementMethod(ScrollingMovementMethod.getInstance());
                    setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);

                    // Force scrollbars to be displayed.TypedArraya=this.getContext().getTheme().obtainStyledAttributes(newint[0]);
                    initializeScrollbars(a);
                    a.recycle();                        
                }
            }

Solution 2:

// try this
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"></LinearLayout>@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = newTextView(this);
        textView.setText("demotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotextdemotext");
        textView.setVerticalScrollBarEnabled(true);
        textView.setLines(3);
        textView.setMovementMethod(newScrollingMovementMethod());

        addContentView(textView,newViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

Solution 3:

Show only when scroll

Xml (in textView element):

android:scrollbarAlwaysDrawVerticalTrack="true"android:scrollbars="vertical"

OnCreate:

textView.movementMethod = ScrollingMovementMethod()

Solution 4:

first - Make a style.xml

<stylename="VerticalScrollableTextView"><itemname="android:scrollbars">vertical</item><itemname="android:scrollbarFadeDuration">0</item></style>

second - use ContextWrapper

valtextView= TextView(
        ContextThemeWrapper(context, R.style.VerticalScrollableTextView)
    )
    textView.movementMethod = ScrollingMovementMethod.getInstance()

That's All!

Post a Comment for "How To Show Scrollbars On A Textview Programmatically"