Change scrollbar color of listview

Hi, is there i want to change the color of the scrollbar of the listview or page scroll.
Thanks

Tagged:

Best Answer

  • RohitSingh.6601RohitSingh.6601 ✭✭ US ✭✭
    Accepted Answer

    You have to try this Custom Render Code

    using System;
    using Android.Content.Res;
    using Android.Graphics.Drawables;
    using Android.Graphics.Drawables.Shapes;
    using Android.Runtime;
    using Java.Lang;
    using Java.Lang.Reflect;

        ...
    
        try
        {
            Field mScrollCacheField = Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache");
            mScrollCacheField.Accessible = true;
            var mScrollCache = mScrollCacheField.Get(Control);
            var scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
            scrollBarField.Accessible = true;
            var scrollBar = scrollBarField.Get(mScrollCache);
            var method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Class.FromType(typeof(Drawable)));
            method.Accessible = true;
    
            var layers = new Drawable[1];
            var sd1 = new ShapeDrawable(new RectShape());
            sd1.Paint.Color = newColor.ToAndroid();
            sd1.SetIntrinsicWidth(6);
    
            layers[0] = sd1;
            method.Invoke(scrollBar, layers);
        }
        catch (System.Exception ex)
        {
            //TODO log exception
        }
    

Answers

  • AswathiPSAswathiPS ✭✭ USMember ✭✭

    @AswathiPS said:
    Hi, i want to change the color of the scrollbar of the listview or page scroll.
    Thanks

  • RohitSingh.6601RohitSingh.6601 ✭✭ USMember ✭✭
    Accepted Answer

    You have to try this Custom Render Code

    using System;
    using Android.Content.Res;
    using Android.Graphics.Drawables;
    using Android.Graphics.Drawables.Shapes;
    using Android.Runtime;
    using Java.Lang;
    using Java.Lang.Reflect;

        ...
    
        try
        {
            Field mScrollCacheField = Class.FromType(typeof(Android.Views.View)).GetDeclaredField("mScrollCache");
            mScrollCacheField.Accessible = true;
            var mScrollCache = mScrollCacheField.Get(Control);
            var scrollBarField = mScrollCache.Class.GetDeclaredField("scrollBar");
            scrollBarField.Accessible = true;
            var scrollBar = scrollBarField.Get(mScrollCache);
            var method = scrollBar.Class.GetDeclaredMethod("setVerticalThumbDrawable", Class.FromType(typeof(Drawable)));
            method.Accessible = true;
    
            var layers = new Drawable[1];
            var sd1 = new ShapeDrawable(new RectShape());
            sd1.Paint.Color = newColor.ToAndroid();
            sd1.SetIntrinsicWidth(6);
    
            layers[0] = sd1;
            method.Invoke(scrollBar, layers);
        }
        catch (System.Exception ex)
        {
            //TODO log exception
        }
    
  • AswathiPSAswathiPS ✭✭ USMember ✭✭

    Thanks @Rohit Signh

  • RohitSingh.6601RohitSingh.6601 ✭✭ USMember ✭✭

    @AswathiPS said:
    Thanks @Rohit Signh

    @AswathiPS said:
    Thanks @Rohit Signh

    **If this code helps you so please like my code Your Welcome @AswathiPS **

Sign In or Register to comment.