For example, when creating list items, I would like to reuse Android's simple_list_item1.xml or simple_list_item2.xml, but I would also like to tweak them.
So, is there any method to inherit the attributes of these views that are declared in xml?
My solution for now is to copy all the attributes in simple_list_item1.xml to a self-defined style, then have the TextView inherit the style. But I hope to get the data directly from android so that if things changes in the future, my views also inherits the changes.
Its not possible to use inheritance in a OOP model. The best way to use this is to copy and paste the source into your own project.
Related
I need to get all views that have text for setting custom fonts.
I can develop a recursive method in myBaseActiviy class for getting all views with checking instanceof when programme is in OnCreate(). But I worry about the performance? I interest your idea? What should I do?
There is no best way. An approach, the one I use, is to subclass TextView, adding a new attribute to specify the font I want to use, delegating the logic to the subclass self.
I think the easiest way is to create your own TextView. It's not as hard as it sounds ;-).
This is the origional answer:
https://stackoverflow.com/a/19679639/2767703
And this is the link you'll need:
http://javatechig.com/android/using-external-fonts-in-android-view
Or if you want to set the font in the xml:
https://stackoverflow.com/a/7197867/2767703
Warning
If you are developing for Android versions lower than Android 4.0 then
you should change the code. In these versions there is a bug that
doesn't free up memory for typefaces. What you should do is create a
HashMap that allows reusage of Typefaces. You can find more in the
comments of the last link (search for the comment with the highest
upvote).
You could also use this:
https://stackoverflow.com/a/16883281/2767703
This changes the font of every text in your application. Note: You still have to look at my warning if you use this.
You can do something like this:
Add new attribute for store font in style.
Extend your view to handle this attr and set font by view when do you need.
You can find example of using and creation of new attribute for font at this link https://stackoverflow.com/a/12282315/2156937
Hope it helps.
I like styling my android views from external styles.xml files. I was wondering what approach can I take to apply externalized styles to views that are created programatically and not in XML layout files, for example, views to be inserted on a ViewPager.
Another use case for this would be styling from XML files custom components that don't have a corresponding XML layout.
Any help would be appreciated :)
Ideally you'd be able to use the View's constructor that accepts the int defStyle attribute. While I haven't used this constructor there may be an issue with styles not being applied. It sounds like standard views may not handle the style in an expected way. Unfortunately, it sounds like your best bet might be subclass the view and override the constructor to ensure that your style is correctly applied. I'd also take a look at the source code of view to see how styles are handled in View.java's constructor. Hope this helps.
Currently I'm mixing explicit attributes (layout_width, height, alignment) on my various XML layouts with coded styles in styles.xml, plus outsourcing colors from colors.xml.
From your experience, what's the recommended way to organize an android app's layouts?
Usings styles is way more organized and makes the xml code easier to read, I think that the visual editor for layouts sucks, so the best way to edit is still to manual edit text the file. Easier reading is a big plus.
I use style anywhere im repeating layouts, something that is very useful is that any explicits attributes will override those of the style, so you can use styles and if what you need is almost the same, you just explicitly redifine the attribute.
Any time you need to fix a layout issue you can update the style and not need to update it a million times.
On the other hand if you are just using that layout "style" once, there is no reason to write an actual style for it, just do it all explicitly.
I am following an example project "Sky" by Jeff Sharkey using styles in my layout. It is working out quite well, however, I cannot determine how to specify the font type-face using the styles. Without this, I will have to apply the font explicitly to every TextView, EditView, etc., whereas I want to control where and when it is applied throughout my application.
It seems that it is not possible from xml/styles... You can only specify it from java code.
check here if you are interesting.
You can do this by creating your own textview class and use that in your layouts.
Inside your new custom class, set the font you want.
Et voila!
I have made an component for Android, which uses two drawables.
Using static values for the drawables in the code, the component works but now I want to declare the values in the properties (XML) does anyone know how to do this?
*Edit;
Is there a way to do this without using the attrs.xml?
You will need to set up a res/values/attrs.xml file to declare the attributes, then go through some code to retrieve those values inside of your View's constructor. I have a sample here that demonstrates the technique.