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

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.

Related

Generating live resource class like R.java in android

How can I generate a live resource class just like what android generates based on its layout xmls, R.java, based on my customized xml file?
Is there any specific plugin for android studio to actively watch for a particular xml file, namely mylayout.xml, and generate a java class, namely myR.java as I'm writing codes?
EDIT: What I want to do is to create NON-VIEW objects from an xml file which defines objects and their attributes, but not in run-time. My intention is to auto-generate a before-run-time class which contains those objects ids and there would be such methods like getMyObject(int id) which automatically instantiate the object from its relevant class.
Android will automatically add the things from any custom XML layout to the R class. You do not need to do anything special for this, beyond coding your custom layout in line with Android's style and requirements. See Creating Custom Views.

What is the relationship between XML and Java in Android?

In android, whats the difference between these 2? I started trying to make apps a few days ago and i can seem to wrap my head around it?
From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?
Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?
And what are ID's for? Is it just to reference certain buttons between the 2 files?
So basically from what i understand if what i have said above is correct, activity_main codes how the buttons look and gives them ID's, and MainActivity code what the buttons do and use the ID's to code the right button.
IS this correct?
From what i have heard from the tutorial i am following, MaiActivity.java uses Java and Activity_main uses xml language?
Also is activity_main used to code the look of the app and MainActivity is used to code what the things do?
Yes. Android uses xml to declare layouts and java to provide logic.
Note that while both activity_main and MainActivity follow common naming conventions, there is no need for them to be called this way.
And what are ID's for? Is it just to reference certain buttons between the 2 files?
IDs are used to identify views in all situations. The most common use case is in the respective java class.
When you create a android project 2 files get generated MainActivity(java) and activity_main(xml) , the xml file is used to create the views which you will be setting in the java file in the setContentView . The android build system created R.java file which contains your xml ids and other xml declaration . the java file can access the views in the xml by referring to R.id,R.string etc . basically its like a address of the xml view which you can refer from java . However I would recommend you to go through the android developer site - http://developer.android.com/guide/index.html
XML, it's an intermediate language between all programming languages and databases, used to pass values from language to another. All tags are user-defined as well as the properties inside such tags. The user can determine the name of the tag, and determine the properties in it, then the name of the tag and its properties with same names will be used in both languages, the first one sets the values to the properties while the other gets them. And so, it works as an intermediate language.
To be specific on how it works, for example, let's assume that we want to pass values from database to java class. There will be three files as follow:
- Java file (.class).
- XML file (.xml).
- Database file (.sql) for example.
In the XML file there is a tag:
<Student>
<name>the name of the student</name>
<age>number</age>
<collage>name</collage>
</Student>
Now each student's data will be in such tag, set from the database file (by an algorithm that writes inside a file when facing a specific text which is the property name), and the java file will get the values (by an algorithm that reads from a file when facing a specific text which is the property name). In this way the values are transformed from language to another.
In Android, the XML file contains all the elements of the activity such as buttons, text views, menus and so on. Each element has an XML tag with its name like Button tag, and each tag has properties. The java file will go to the XML file and look for element tag (Button tag) by the ID of that element (tag), and then the java file (class) takes the values of the properties and sets them to the variables (attributes) of the Button class, and then the Button class draws the Button in the activity. Furthermore, Android studio provides virtual mobile phone screen and displays on it the elements to tell the developer the primary appearance of the activity, in addition, to inform the developer what is the appropriate position, dimensions, or the color of the element, this will generate the XML code to make it easier while coding (it's called visual programming), but in fact the java file did not read the XML file yet, until the Gradle is building the APK (execution phase).
In Android basically we use two languages JAVA and XML.
XML
For layout, how your screen looks? What are the elements(Textview, Buttons, Listview, etc) on screen? What are the attributes of these elements (e.g What is the textcolour, background colour, visibility, font, width, height and much more?)?
The answer of all above question is inside layout subdirectory of res directory i.e a xml file.
Manifest.xml
You will find this xml in app directory of your project.
As in novel or any other book we have content/index page, which gives us the information about all chapters/topic included in that book. In a similar way APK have Manifest.xml which includes all information about Activities, User permissions, receiver, App name, App icon etc.
With the help of xml you can create animation (e.g how textview or any other element will be animated? fade in, fade out, zoom in zoom out etc). Also you can create shapes like circle(oval), rectangle etc and use them as a background or as a icon.
You can string.xml, color.xml etc
JAVA
Used for coding. This page control all the elements of xml with time. You can give default attributes values for different elements in xml, which will be used(for that particular element) in Activity(app) until you change that attribute in corresponding JAVA file for that particular element. To change attributes one must first define an id to the element and use that id in JAVA file to change its attributes.

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

What can be edited on the fly?

I understand that to apply a style you have to create the textview and apply the style programmatically, which I haven't figured out how to do yet, but what can you edit on the fly. Like text color? Background color? What things can or what cannot be edited on the fly from the java. Also can it be defined in the xml and then edited in the Java or must anything you want to change or set be defined solely from the Java?
The documentation for TextView lists the XML attributes that it supports, along with the method names for those that are supported at run time. Just about everything can be changed at run time, whether or not defined in XML (including every attribute you mentioned). The run-time changes override whatever is defined in XML.

How to structure Android views in xml logically

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.

Categories

Resources