-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Setting styles #15
Comments
Glad to hear you're making progress with an app using Anvil! As for the styles - I'm open to any suggestions. Meanwhile I'm using good old functions to style views. In your case (using java) shadows can be set in java using public AttrNode buttonTextStyle() {
return attrs(
shadowLayer(2, 1, 1, Color.BLACK)
// maybe some other style attributes go here as well
);
}
v(Button.class,
buttonTextStyle()); Does it make sense? In my experience every XML attributes corresponds to some java method of a certain view class. We're just too much used to the (misleading) names of the XML attributes, that it may be really hard to find a matching view method. |
Oops. I see what you mean. There is no generated shadowLayer() method because it takes 4 arguments and generated methods use only one (since it's a typical case to set just one attribute value). Here I see three ways:
|
One more option would be to add some style() generator to Anvil that will add some style ID to the view properties without applying it. Then fix Anvil renderer to look for that style ID and use |
Good suggestions. I agree that the Android API (including the divide between styles vs themes vs attributes... wat) is very painful to work with -- that's why I'm using Anvil despite its early state. :) If I can do everything I need to through the Anvil API without having to use styles that would be preferable from my perspective. I ended up going with option (1) because it seemed the easiest to implement. I can add it to V15Attrs or BaseAttrs in a PR if you think it would be generally useful. And if we run into this more frequently (3) might make sense -- I don't have a good idea of how common these multi-method setters are yet, since I've mainly used the xml layouts for styling and layout up to now. public static AttrNode shadowLayer(final float radius, final float dx, final float dy, final int color) {
final List<Object> params = new ArrayList<Object>() {{
add(radius); add(dx); add(dy); add(color);
}};
return new SimpleAttrNode(params) {
public void apply(View v) {
if(v instanceof TextView)
((TextView) v).setShadowLayer(radius, dx, dy, color);
}
};
} |
I scanned the android.jar in API levels 10 and 15 - it looks like there is only about 40 setters with more than one parameter. In API level 21 there's ~50 of them. However, most of them are the ones I never heard of: API level 15 (in API level 10 there's even less):
Most of them are just advanced versions of single parameter methods like setTag or setTypeface. |
Yeah adding them on demand is probably fine, there aren't many of those that I know what do that don't have single-parameter equivalents. |
I've added the suggested |
I've added the suggested |
I think the bigger issue is that I currently have an app with a defined theme and style structure. I don't want to be able to just add a single attribute such as match parent but instead add a style for example "MyTextViewStyle" |
Here's some thoughts about how one can organize view styles in Anvil: http://zserge.com/blog/anvil-howto-style-views.html |
Is there a way to easily translate the android predefined themes to anvil? For example Widget.AppCompat.Button.Borderless and other Material elements. It's a pain having to hunt on all the sub definitions of the theme to recreate them in anvil. |
* improve nullability * add MethodSignature class instead of string concatenation * clean up * add nullability to dsl15 and dsl19 * update travis config * migrate to androidx * fix appcompat core dependency * fix typo * fix grid layout and add ability to pass span lookup
Really happy with how Anvil is working, and I'm writing new functionality for my app using it.
I'm having trouble assigning styles to elements created by anvil. For example, I want to add a shadow to the text on a button. Using standard layouts I can do something like the following:
and then set the style from XML. However, it doesn't look like there is a way to set the style on an element programmatically, and thus no way to do it from anvil. Am I missing something?
The text was updated successfully, but these errors were encountered: