xebia-functional/macroid

How to set styles in macroid?

AbimbolaE opened this issue · 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??

dant3 commented

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

pfn commented

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)

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?

pfn commented

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

On Wed, Feb 3, 2016 at 8:43 PM Abimbola Esuruoso notifications@github.com
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
#73 (comment).

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

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

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