xebia-functional / macroid

A modular functional UI language for Android
527 stars 37 forks source link

How to set styles in macroid? #73

Open AbimbolaE opened 8 years ago

AbimbolaE commented 8 years ago

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 8 years ago

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

pfn commented 8 years ago

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)
AbimbolaE commented 8 years ago

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 8 years ago

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 https://github.com/47deg/macroid/issues/73#issuecomment-179635095.

AbimbolaE commented 8 years ago

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

stanch commented 8 years ago

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

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