macroid icon indicating copy to clipboard operation
macroid copied to clipboard

How to set styles in macroid?

Open AbimbolaE opened this issue 9 years ago • 6 comments

I have an EditTextView and I would like to change the style to match the following xml:

    <style name="login_field" parent="android:Widget.EditText">
        <item name="colorControlNormal">@color/DarkGreen</item>
        <item name="colorControlActivated">@color/SeaGreen</item>
        <item name="colorControlHighlight">@color/SeaGreen</item>
    </style>

However according to this post on StackOverflow setting a style isn't possible programmatically. How can I achieve this in Macroid??

AbimbolaE avatar Jan 31 '16 18:01 AbimbolaE

The style can be passed to a view's constructor. There are no other options to use styles programmatically.

dant3 avatar Feb 02 '16 18:02 dant3

The best approach is to re-write your style as Tweaks,

thus:

val login_style = Tweak[EditText] { e: EditText =>
  e.setXXX(...)
  e.setYYY(...)
  e.setZZZ(...)
}

w[EditText] <~ login_style

if on v21+, you can do:

new EditText(context, null, 0, R.style.login_field)

if before v21, you must do:

styles.xml:

<attr name="myEditStyle" format="reference"/>
<style name="MyAppTheme" parent=...> <!-- or use your existing AppTheme if set -->
  <item name="myEditStyle">@style/login_field</item>
</style>

in code:

new EditText(context, null, R.attr.myEditStyle)

However!!!

You are attempting to change theme attributes, which is not handled by any of the above (colorControlXXX are theme attributes, you wouldn't be able to apply this in xml via style= either, additionally, since you are attempting to override theme attributes, you should remove the parent from login_field as well), what you need to do is:

new EditText(new ContextThemeWrapper(context, R.style.login_field)), null, 0)

pfn avatar Feb 02 '16 19:02 pfn

So assuming I wanted to do the following:

new EditText(context, null, R.attr.myEditStyle)

Is there any clean way of achieving this using Macroid? Or do I have to manage the EditText using the Android API like in regular Java?

AbimbolaE avatar Feb 04 '16 04:02 AbimbolaE

Ui(new EditText(context, null, R.attr.myEditStyle) <~ tweak

On Wed, Feb 3, 2016 at 8:43 PM Abimbola Esuruoso [email protected] wrote:

So assuming I wanted to do the following:

new EditText(context, null, R.attr.myEditStyle)

Is there any clean way of achieving this using Macroid? Or do I have to manage the EditText using the Android API like in regular Java?

— Reply to this email directly or view it on GitHub https://github.com/47deg/macroid/issues/73#issuecomment-179635095.

pfn avatar Feb 04 '16 04:02 pfn

That was bloody fast mate.... Thanks, I'll try it!

AbimbolaE avatar Feb 04 '16 04:02 AbimbolaE

Ui(new EditText(context, null, R.attr.myEditStyle) <~ tweak

IIRC widget[EditText](null, R.attr.myEditStyle) should be the same.

stanch avatar Feb 05 '16 00:02 stanch