How to structure Android views in xml logically - android

I want to put view elements that belong together in a container (e.g. a label and input field). This is useful for example to show/hide these elements all at once. The container, however, is only to group them logically, i.e. I don't want to change the layout with the additional containers.
If I look at HTML, there you can use a div element to structure elements together. When applying a style or removing this element, then its children are affected by that as well. I am looking for something like this in Android.
Android has an abstract ViewGroup, yet I cannot use this directly. Android Studio tells me "Element ViewGroup is not allowed here". I don't want to use a LinearLayout because I don't want to change the layout. Is there a ViewGroup that does nothing, besides adding structure to the XML?
Alternative idea:
Maybe I could use the android: tag attribute for this. Such that I construct a method to "hide all elements that contain tag X". Or more generally "perform action Y of elements with tag X". With this approach I would try to emulate what classes do in CSS/HTML: Give elements attributes, query elements using these attributes, apply styles/actions on these elements.
Does anyone have experience with such an approach in Android?
Bonus question:
When looking at Android I get the feeling that very flexible and useful concepts, which are mature and well known from web development, have been lost. For example, in Android XML you can set one style on a view. However, using HTML/CSS you typically set a multitude of classes to elements and can create a style for each one of them. For instance elements with class "important" should be bold, with class "title" should have a larger font, thus an element with both "important_text" and "title" would be bold as well as large. How would you do this in Android?

Take a look how <include> and <merge> layout tags are working. Probably that's could be close to that you are searching.

Related

Apply a different style to certain rows of RecyclerView

I have the following use case: using a RecyclerView with a custom adapter for listing a simple set of data (each row has something like an icon, a title, and a description). Think of your favorite email or newsfeed app and this is most likely the same visually.
I've reached the point where I need to stylize some key elements from the list quite differently to indicate a special situation - that they are "unread" or that they are "recommended" (e.g. with a different background color, the title font becomes bold, the icon is made larger, the description is hidden, etc).
All of these variations, being purely representation/visual, I have followed the good practices and defined them as 2 different <style> blocks in the values/styles.xml file. However, I am having a hard time applying one of these 2 styles to the row in the onBindViewHolder() method of the adapter, based on some programmatical condition.
(Of course, I can go down the bad way and modify the styling programmatically by manually calling setBackgroundColor() etc on each individual View, but this seems like an ugly workaround for a seemingly trivial problem; additionally, this way we are skipping all of the benefits the .xml styling provides like resolution or locale specific styles).

Android : Difference between Component and Widget?

what is the difference between Component and Widget? in the Android UI Design Level. Difference between Java Class Creative UI and using .xml layout file design?
"widget" for subclasses of View that have a visual representation to the user by default -- things like TextView, Button, ListView, etc.
I tend to use the term "view" for something that could be any subclass of View, including pure containers like LinearLayout.
But, again, there is no particular harm in referring to them by either term.

Iphone equivalent to Android <include> and <merge> tag?

I'm going to port an Android application over to the iPhone platform. I need header-footer like functionality so I'm looking for Android-style layouts merging for iPhone.
Does iPhone support this? If so, how?
[EDIT]
May be above header-footer description creating confusion.I am trying to describe again.
I would like to merge two different .xib file's view in single xib.for an example I have "footer.xib",I just want to include (reuse) same "footer.xib" view in different pages instead of copying same code for each page. I'm very new to Iphone world, so any guidance would be appreciated!
I'd second the notion of going from scratch. I'd question why you'd need header functionality (UINavigationController provides a default header), but with the footer, do this:
Create a subclass of a UIView or UIToolbar or the like, and do custom drawing or custom layout in code. Then, place them in your views by dragging a UIView into a NIB/.XIB view controller file and changing the "Custom Class" field to that of your subclass, or programmatically add them.

How to extract supported XML Elements and Properties from a View?

Is there a way to extract the XML properties that a view supports? For example, an EditText has an XML property called Text, but on the Java side you use getText and setText.
I want to create a system that automatically discovers all the supported XML properties so I can verify the XML layout. At first just working with the default widgets is enough, but eventually I want to also enumerate custom widgets.
Both standard and custom XML attributes are defined in values/attrs.xml. In the case of the built in ones they are in android-sdk/platforms/android-X/data/res/values/attrs.xml.
It looks to me like you would have to find a way to parse these files to figure out which attributes can be associated with which views.

XML parser for dynamic layout (dynamically loaded skins)

I want to write an app where (at least for now) the content is always the same but the layout is loaded dynamically at run time based on a user preference. Essentially I want the app to apply a "skin" which may look completely different to other skins.
I found some tutorials using SAXparser:
http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser/
http://twigstechtips.blogspot.com/2010/12/android-how-to-parse-xml-string.html
and can imagine writing something from scratch that recognizes all the standard xml layout tags and then dynamically loads each part of the layout. But that's a lot of work to do from scratch! Surely this functionality is available in android, or surely someone has written some open source code which can be run at the start of your activity's onCreate method, which takes in an xml file and sets your layout?
I found a similar but unsatisfactorily answered question here:
How to create a layout file programmatically
which makes me think that since setContentView must take an integer resourceID as its argument, the fact that these are pre-baked at compile time might be a problem. (setContentView may also take a View object as its argument, but I don't want a ton of if statements and to pass it each View object one by one, I want some code that inputs an xml file or xml string and sets the content view.)
Maybe I'm way off track. Is there another way to do this? I would think that the ability to have an app with dynamically loaded skins is important.
Thanks!
I had similar requirements and tried the same approach - it does not work.
Documentation clearly states this: http://developer.android.com/reference/android/view/LayoutInflater.html
Update:
Since OP needs to load XML layouts created at runtime:
Possibly this could be done, by creating XML layout files, copying them to dummy project, create .apk and then load apk on to device.
DexClassLoader can be then used to load classes inside apk.
well, android makes the hard work for you, but no all the the work....
first that all you have to forget about parsing xml layouts... instead you can make skeletons layout, that manages his inner childs position, size, etc... and later inflate that 'skeleton' xml with LayoutInflater and obtain a View instance...
When you have that View instance then you can do what you want with it, applying the users preferences like backgrouds, foregrounds colors, position, sizes, etc...
maybe i dont understand your question but you can get any view inflated from a xml resource at compile-time and later apply other style or set another propertys
It seems it is impossible to load the layout & change the skin dynamically according to the doc :
Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R.something file.)
http://developer.android.com/reference/android/view/LayoutInflater.html

Categories

Resources