Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

How to set styles in macroid? #73

Open
AbimbolaE opened this issue Jan 31, 2016 · 6 comments
Open

How to set styles in macroid? #73

AbimbolaE opened this issue Jan 31, 2016 · 6 comments

Comments

@AbimbolaE
Copy link

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
Copy link

dant3 commented Feb 2, 2016

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

@pfn
Copy link

pfn commented Feb 2, 2016

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
Copy link
Author

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
Copy link

pfn commented Feb 4, 2016

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
#73 (comment).

@AbimbolaE
Copy link
Author

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

@AbimbolaE AbimbolaE reopened this Feb 4, 2016
@stanch
Copy link
Collaborator

stanch commented Feb 5, 2016

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

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants