16

I want to dynamically set scrollbars in an EditText programmatically, but I don't find this api as setscrollbars. Could anyone tell me if this can be done programmatically?

android:scrollbars="vertical"

Is this possible? How can I achieve this programmatically?

4 Answers 4

6

I was looking for a method to do this programatically, too.

Seems from http://developer.android.com/reference/android/view/View.html#attr_android:scrollbars it's not possible, cause no related method is mentioned there.

The only method that sounds good by description is:

View.setVerticalScrollBarEnabled(boolean)

but in my case it didn't work. But that may depend on the layout and the way it is used.

I am thinking now about having two layouts: one with the attribute set to "none" and one set to "vertical". Then in code I could decide which to use.

Sign up to request clarification or add additional context in comments.

1 Comment

hi, it didn't worked for me also. please post if you have any other suggestions.
2
    final TypedArray typedArray = context.obtainStyledAttributes(null,  new int[]{});
            try {
                Method method = View.class.getDeclaredMethod("initializeScrollbars" , TypedArray.class);
                method.invoke(this, typedArray);
            }catch (Exception e) {
            }
            typedArray.recycle();
 setVerticalScrollBarEnabled(true);

Comments

0

You have to override the android:scrollbarTrackVertical and android:scrollbarThumbVertical drawables with your own. You can do it in code or with styling.

This is a link that shows how to do custom buttons. The principle for scrollbars is similar.

1 Comment

i donot want to set myself stying,i want to know android:scrollbars="vertical how to using code complete. as android:text ,ican use code (View)setText()
0

Programmatically :

RecyclerView mRecyclerView =rootView.findViewById(R.id.your-recycler-view);
if(mRecyclerView!=null){
  mRecyclerView.setVerticalScrollBarEnabled( true );
}

In the XML :

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:scrollbars="vertical"
android:layout_height="wrap_content" />

Other scrollbars:

android:scrollbars="vertical"
android:scrollbars="horizontal"
android:scrollbars="vertical|horizontal"

1 Comment

This should be the accepted answer. In my case android:scrollbars="vertical" had to be in the XML for RecyclerView.setVerticalScrollBarEnabled to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.