Created
June 21, 2017 03:13
-
-
Save vinhdn/6d8bb21e4ebb7e75ab4c278c63edb63c to your computer and use it in GitHub Desktop.
Change color of scrollbar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void changeColorScrollBar(View Scroll, int color, Context cxt){ | |
try | |
{ | |
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache"); | |
mScrollCacheField.setAccessible(true); | |
Object mScrollCache = mScrollCacheField.get(Scroll); | |
Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar"); | |
scrollBarField.setAccessible(true); | |
Object scrollBar = scrollBarField.get(mScrollCache); | |
Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class); | |
method.setAccessible(true); | |
Drawable[] layers = new Drawable[1]; | |
GradientDrawable dr = new GradientDrawable(GradientDrawable.Orientation.BOTTOM_TOP, null); | |
dr.setColor(color); | |
dr.setAlpha(255); | |
dr.setCornerRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 17, cxt.getResources() | |
.getDisplayMetrics())); | |
dr.setSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, cxt.getResources() | |
.getDisplayMetrics()), (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, cxt.getResources() | |
.getDisplayMetrics())); | |
ShapeDrawable sd1 = new ShapeDrawable(new RectShape()); | |
sd1.getPaint().setColor(color); | |
sd1.setIntrinsicWidth((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, cxt.getResources() | |
.getDisplayMetrics())); | |
layers[0] = dr; | |
method.invoke(scrollBar, layers); | |
} | |
catch(Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment