How to override an android style's private attribute -


i trying find way override non-public attribute of android style, more atttribute named itemcolor of widget.fragmentbreadcrumbs style. style affects text color of breadcumb in preferenceactivity when preference fragment being displayed on right pane large screens. used class fragmentbreadcrumbs.

my application uses custom theme extends theme.holo.light , theme breaks on api 23 trying find workaround.

the aforementioned style sets default value itemcolor of @null not overridden in holo theme while example set valid value material theme. result title of breadcrumb not visible (see screenshot api 19 , screenshot api 23)

i guess trying find way change private value of theme similar way reflection can used modify private field's value of class. alternatively contextthemewrapper seems promising simple don't how can use or if applicable in situtation.

what need after fragmentbreadcrumbs class executes constructor below mtextcolor attribute not @null (which guessing 0) set android theme configuration have valid color value.

do think possible?

public fragmentbreadcrumbs(context context, attributeset attrs, int defstyleattr, int defstyleres) {     super(context, attrs, defstyleattr, defstyleres);      final typedarray = context.obtainstyledattributes(attrs,             com.android.internal.r.styleable.fragmentbreadcrumbs, defstyleattr, defstyleres);      mgravity = a.getint(com.android.internal.r.styleable.fragmentbreadcrumbs_gravity,             default_gravity);      mlayoutresid = a.getresourceid(             com.android.internal.r.styleable.fragmentbreadcrumbs_itemlayout,             com.android.internal.r.layout.fragment_bread_crumb_item);      /* value needed overridden */     mtextcolor = a.getcolor(             com.android.internal.r.styleable.fragmentbreadcrumbs_itemcolor,             0);      a.recycle(); } 

unfortunately toolchain report error if try use android:itemcolor because not correspond public attribute name, cannot make style attribute.

the thing can think of change text color via reflection after views have been constructed(/inflated). want possible, before first time updatecrumbs() run inside of fragmentbreadcrumbs. perhaps can override oncreate() of preferenceactivity or oncreateview() of preferencefragment (whichever applicable here) , this:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);      fragmentbreadcrumbs fbc = (fragmentbreadcrumbs) findviewbyid(...);     int color = ...;     fragmentbreadcrumbsutils.settextcolor(fbc, color); }   public class fragmentbreadcrumbsutils {      private static final field fragment_bread_crumbs_text_color = findfield();     private static field findfield() {         try {             field f = fragmentbreadcrumbs.class.getdeclaredfield("mtextcolor");             f.setaccessible(true);             return f;         } catch (throwable t) {             // don't fail reason, log             log.e("fragmentbreadcrumbsutils",                 "couldn't find mtextcolor field in fragmentbreadcrumbs",                 t);         }         return null;     }      public static void settextcolor(fragmentbreadcrumbs fbc, int color) {         if (fragment_bread_crumbs_text_color == null) {             return; // can't anything, don't have field         }          try {             fragment_bread_crumbs_text_color.set(fbc, color);         } catch (throwable t) {             // don't fail reason, log             log.e("fragmentbreadcrumbsutils",                 "couldn't set mtextcolor field in fragmentbreadcrumbs",                 t);         }     } } 

Comments