Change The Color Of Scrollview Programmatically
What I'm currently doing Currently, I have changed the scrollbar in my XML file using the android:scrollbarThumbVertical property like so:
However, I did come across this one stackoverflow answer that uses reflection to do it. Please upvote the answer there if it works for you: https://stackoverflow.com/a/19819843/3286163
The answer was for a listview but is the same for the scrollview:
ScrollViewscr= (ScrollView)findViewById(R.id.scrollView1);
try
{
FieldmScrollCacheField= View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
ObjectmScrollCache= mScrollCacheField.get(scr); // scr is your Scroll ViewFieldscrollBarField= mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
ObjectscrollBar= scrollBarField.get(mScrollCache);
Methodmethod= scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);
// Set your drawable here.
method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_blue));
}
catch(Exception e)
{
e.printStackTrace();
}
Only thing I could find. I gave it a try myself and it worked.
Solution 2:
It is easy nowadays :)
scrollView.verticalScrollbarThumbDrawable = ColorDrawable(Color.CYAN)
scrollView.horizontalScrollbarThumbDrawable = ColorDrawable(Color.WHITE)
Solution 3:
In API 29+ use ScrollView.setVerticalScrollbarThumbDrawable()
otherwise use the accepted answer.
Post a Comment for "Change The Color Of Scrollview Programmatically"