Styling a AndroidPlot XYPlot with XML styles - android

I'm looking for a way to apply a Style to an AndroidPlot element, and the documentation is precious scarce. Basically, I want to externalize the graph design elements into my styles.xml file, but I'm not sure how to do this. I've tried adding an element to Attrs.xml but to no avail.
My activity_main.xml layout file:
<com.androidplot.xy.XYPlot
android:id="#+id/main_xyplot_main"
style="#style/GraphStyle"
androidPlot.domainLabel="#string/graph_x_axis"
androidPlot.titleWidget.labelPaint.textSize="#dimen/title_font_size"
androidPlot.domainLabelWidget.labelPaint.textSize="#dimen/domain_label_font_size"
androidPlot.rangeLabelWidget.labelPaint.textSize="#dimen/range_label_font_size"
androidPlot.rangeLabel="#string/graph_y_axis"
androidPlot.title="#string/graph_title"
androidPlot.renderMode="use_background_thread"
androidPlot.graphWidget.rangeLabelPaint.textSize="#dimen/range_tick_label_font_size"
androidPlot.graphWidget.rangeOriginLabelPaint.textSize="#dimen/range_tick_label_font_size"
androidPlot.graphWidget.domainLabelPaint.textSize="#dimen/domain_tick_label_font_size"
androidPlot.graphWidget.domainOriginLabelPaint.textSize="#dimen/domain_tick_label_font_size"
androidPlot.graphWidget.gridLinePaint.color="#000000"
androidPlot.graphWidget.marginBottom="25dp"
androidPlot.graphWidget.marginLeft="20dp"
androidPlot.graphWidget.marginRight="10dp"
androidPlot.graphWidget.marginTop="20dp"
androidPlot.legendWidget.heightMetric.value="25dp"
androidPlot.legendWidget.iconSizeMetrics.heightMetric.value="15dp"
androidPlot.legendWidget.iconSizeMetrics.widthMetric.value="15dp"
androidPlot.legendWidget.positionMetrics.anchor="right_bottom"
androidPlot.legendWidget.textPaint.textSize="10dp"
android:layout_above="#id/main_percentage"
android:gravity="top"
android:visibility="invisible" />
Styles.xml
<style name="GraphStyle">
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingLeft">0dp</item>
<item name="android:paddingRight">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:layout_width">"wrap_content"</item>
<item name="android:layout_height">"wrap_content"</item>
</style>
Is there even a way to do this? I want to put all of those androidPlot.* into the GraphStyle.
Thanks!

Unfortunately this is not currently possible. Androidplot is packaged as a Jar and (as far as I am aware) it is not possible to include the styleable definitions that are used by in a jar. This means that you won't be able to override the Androidplot specific fields of the Plot (those that begin with "androidPlot") but can still override the fields that come from the View base class (those that begin with "android").
Going forward, we are considering using .aar instead of .jar as the packaging format. Among other things, this should let us include styleable definitions in the artifact, getting rid of the above limitation. Here's a link to the thread on that topic.
UPDATE: As of 0.6.2 (the current development version) Androidplot is available as a .aar with limited styleable support, however as 0.6.2 matures more and more attributes are being added. See this thread for details.

Related

Android built-in styles

I'm a learner of Android programming and I'm currently reading this book, well it's the first, HeadFirst Android Development.
In chapter 14 Navigation Drawers, there was this attribute of TextView textAppearance that was given a value of #style/textAppearance.AppCompat.Body1.
The book said it was a built-in styles that makes text look slightly bolder.
My question is, how many built-in styles does Android has?
I want to know all of them.
If you go through the following link you will get all the styling options for TextView.
https://developer.android.com/reference/android/widget/TextView
you can check the styling options for other views from the left panel of the same link by clicking on other classes.
A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under res/values/ directory of your project and will have as the root node which is mandatory for the style file.
You can define multiple styles per file using tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomFontStyle">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/>
</style>
</resources>
Using Styles
Once your style is defined, you can use it in your XML Layout file using style attribute as follows
<TextView
android:id="#+id/text_id"
style="#style/CustomFontStyle"
android:text="#string/hello_world" />
Always, when you are writing in XML to find the built-in attribute use:
attribute_name="#android:attribute_value
The #android: will list all the built-in the attribute for a given attribute you want, for Example:
style="#android:style/TextAppearance.AppCompat.Medium"
Default Styles & Themes
Android provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "#android:style/Theme.NoTitleBar".
You can see the following source code for Android styles and themes- here and here

Meaning of 'android:' prefix within attribute value (e.g. "android:imageButtonStyle")

I want to style all my ImageButtons in a theme. After searching for quite some time I found the solution to my problem. But I don't know why it works like it does.
My main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher_foreground" />
</LinearLayout>
This is my original theme that didn't work. It styles my TextView but ignores the ImageButton. The result is shown in the screenshot below.
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:imageButtonStyle">#style/redBackground</item>
<item name="android:textViewStyle">#style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
And here's the theme that works:
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="imageButtonStyle">#style/redBackground</item>
<item name="android:textViewStyle">#style/redBackground</item>
</style>
<style name="redBackground">
<item name="android:background">#FF0000</item>
</style>
</resources>
The only difference is the missing 'android:' prefix in front of the 'imageButtonStyle' attribute.
So my questions are:
What is the difference between imageButtonStyle and android:imageButtonStyle?
Why does android:textViewStyle work but not android:imageButtonStyle? They are both defined the plattforms 'attrs.xml'.
Why is there no textViewStyle (without android prefix)? Removing the prefix yields an error.
Where are the attributes defined that have no prefix? Apparently not in the plattforms 'attrs.xml'.
Where can I find proper documentation for the whole style stuff? Of course I halve already read the respective Google docs (https://developer.android.com/guide/topics/ui/look-and-feel/themes.html). But still i have basic questions like this one.
Interestingly, it seems like the 'android:imageButtonStyle' version has worked some years ago: How to apply an style to all ImageButtons in Android?. I haven't tested that myself, though.
And here's the post that proposed removing the android prefix. Including unanswered comments that ask why it works: buttonStyle not working for 22.1.1
android tag that you use is used for attribute coming from Android SDK.
app tag is used if you are using the support library.app is just a namespace for any custom parameters for a custom View.
This can be anything but if you see the root element there's probably a line xmlns:app="http://schemas.android.com/apk/res-auto" that assigns the namespace
You may also see other namespaces if you are using custom views (of your own or form a library).
In case that anyone else stumbles across the question: I've found the answer in this Droidcon talk: https://youtu.be/Jr8hJdVGHAk?t=21m12s
The topic is handled in a minute starting at 21:12.
As I understand it, specifying no namespace results in the global namespace being searched which seems to include the support libraries attributes. And indeed both, the SDK's R.attr as well as the support library's R.attr define the imageButtonStyle attribute (with slightly different descriptions). However, the support library does not define a textViewStyle attribute. So that explains why you can't omit it's android: prefix.
To answer my last question concerning the documentation: Despite the Google guide and the R.attr classes' documentation, the video mentioned above and this Google I/O talk are quite informative: https://www.youtube.com/watch?v=TIHXGwRTMWI
So the only question that is left open is why the SDK's imageButtonStyle does not work.

Where is #style/MyActionBarTitleText

I’m new to android and are looking at tutorials to learn android development. I'm trying to figure out how all the xml files and tags are fit together.
In a google tutorial for actionbars one custom style, in themes.xml, look like this:
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="#android:style/Widget.Holo.ActionBar">
<item name="android:titleTextStyle">#style/MyActionBarTitleText</item>
</style>
I don't find or understand where "#style/MyActionBarTitleText" is defined and i don't get any compiling error in Eclipse. When a tag is referenced like this i thought it has to be defined in a xml file under my project somewhere but i cant find it?
Style XMLs are defined in res/drawable[-mdpi/ldpi/xhdpi]
Style defines look of your Activity. You can define color, themes, shapes in Styles. Read more about styles and other resources here
http://developer.android.com/guide/topics/resources/style-resource.html
http://developer.android.com/guide/topics/resources/available-resources.html
Yes you can find it in: res->values->styles.xml
and also you can change your application theme/style in styles.xml file

Animations in Android Libraries don't animate?

I have an app which uses a single common library used throughout my applications in order to keep a common theme.
In this library, I have the following style defined:
<style name="App.Widget.ProgressBar.Large" parent="#android:style/Widget.ProgressBar.Large">
<item name="android:indeterminateDrawable">#anim/anim_progressbar_large</item>
<item name="android:minWidth">80dip</item>
<item name="android:maxWidth">80dip</item>
<item name="android:minHeight">80dip</item>
<item name="android:maxHeight">80dip</item>
</style>
Which should allow a custom progress spinner to be displayed (irrelevant of device).
When this resource is local to the app (app/res/values/), not in the library, my Spinner animates correctly. However, when this style resource is in the library (library/res/values), the Spinner displays the beginning image but doesn't animate.
Does anyone know why the location of the resource would make a difference? I don't want to have to duplicate this resource for each application.
This is an old question, and I'm not sure I had exactly the same problem as the individual that posted the question, but for some reason using
style="#android:style/Widget.ProgressBar.Large"
did not animate the ProgressBar for me, whereas using
style="?android:attr/progressBarStyleLarge"
did. Hope it helps someone.

android:style reference after SDK and ADT plugin update in mid 2011

I see a lot a similar questions R.java can't compile , No resource found that matches #android:style/ "just after SDK & ADT update" leading to existing projects can't compile
The R.java file cannot be compiled. I find out the errors come from referencing
<style name="Theme.Wallpaper" parent="android:style/Theme.Wallpaper" >
<item name="android:colorForeground">#fff</item>
</style>
What is happening is that some styles, like Theme.Wallpaper are not public. You should not extend from them anymore.
Some suggest to revert to platform_tools_r05
http://groups.google.com/group/android-developers/browse_thread/thread/550fce9670530d9b/9b2b2aa389dce367?show_docid=9b2b2aa389dce367&pli=1
If you want to do the correct way
read Xavier July 28
http://groups.google.com/group/android-developers/browse_thread/thread/550fce9670530d9b/9b2b2aa389dce367?show_docid=9b2b2aa389dce367&pli=1
If you wish to reuse a style that is private, you should copy the
content of that style into your own instead of extending it.
Ok so Where do I find the content of the (private) style ?
Seriously, after an update I don't have time to correct platform mistakes when the project code worked right.
<!-- Default theme for windows that want to have the user's selected
wallpaper appear behind them. -->
<style name="Theme.Wallpaper">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="android:windowShowWallpaper">true</item>
</style>
Looks like you want to use the above code from the Android source and create your own theme based it.

Categories

Resources