Generating live resource class like R.java in android - 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.

Related

Loading XML layout from server pros and cons?

In order to avoid frequent updates is it a good practice/is it possible to load XML layout from the server? So any necessary changes can be done on the backend without having to update the app.
From the docs:
When your application is compiled, aapt generates the R class, which
contains resource IDs for all the resources in your res/ directory.
For each type of resource, there is an R subclass (for example,
R.drawable for all drawable resources), and for each resource of that
type, there is a static integer (for example, R.drawable.icon). This
integer is the resource ID that you can use to retrieve your resource.
So, layout files are compiled (in a binary-like format) as part of your project. If the XML was not part of your project at build time, you won't be able to use it during run-time.
is it a good practice/is it possible to load XML layout from the server?
No because of what I wrote above.
to do a native vs runtime/remote layout inflation depends on your use case. (so I will not touch on whether it is good practice or not).
as to how to accomplish a runtime layout inflation -> you can use https://github.com/flipkart-incubator/proteus as a replacement for your layoutInflator
from the page it states that
Instead of writing layouts in XML, in proteus layouts are described in JSON, which can be used to inflate native Android UI at runtime. The JSON layouts can be hosted anywhere (on the device, on servers, etc.).

Xamarin.Forms Android Project not showing Resource.Layout folder

I have a Xamarin.Forms solution in which inside my Android project I have a custom BroadcastReceiver. Inside this class I'm trying to create a custom notification layout using RemoteViews which was explained to me using this thread.
However I am having problems with the construction of this class using the RemoteViews(Android.Content.Context,Resource.Layout) constructor. Specifically the Resource.Layout portion. When I go into the 'Resources' folder there is only a Drawable folder (even when 'Show All Files' is enabled). Which is interesting considering I can access layouts after typing Resource.Layouts.
Is there a way to get a custom .xml id int (or .axml if that's the case) to be referenced for this strategy inside the Layout 'folder', or any other way Xamarin does instead that I may not have knowledge of?
Essentially all you have to do is add a folder in the Android project named "Layout" under the Resources folder. Then create the .axml file. Then the file can be referenced using Resources.Layout and the id retrieved using the method from this thread.

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.

Getting ids of xml layout

Im working at a little Annotation Processor for android.
I have the following scenario:
I have a xml layout resource id and I want to find all views in this xml layout. I simply want to parse the xml layout file to retrieve some information that I will use later on.
Does anybody know if such a tool exists or how to implement something like this?
I know it's not simple. There are serval things to consider like:
same layout files in different layout-resource folders like:
res/layout/mylayout.xml
res/layout-xlarge/mylayout.xml
I want to parse both mylayout.xml files.
I only have the resource id (integer) of a layout, how do I map that back to the xml file (String, name)
Any suggestion how to start?
I doubt I can use Android classes, because I want to write an Annotation Processor. AnnotationProcessing runs in it's own jvm before compiling Android resources.
From what I understand the workflow should be as follows:
Map id (integer) to layout file name (String). I guess I have to
parse the R.java class to achieve that.
Next I have to check
recursively all layout resource folders to find the corresponding
layout.xml files.
Parsing the xml files (no big deal)
if you need the name of the resources that correspond to a particular id, you can use
String getResourceEntryName(int id)
that is a method of Resources(). Here you can find the documentation

Android - Defining Custom Component Layouts in XML

i'm reading the android developer docs on creating custom components and one thing that's unclear, is whether you can define the layout of your component using xml and then reuse that across class libraries. like, say for instance, i want to create a class library called myComponents, and in there i want to have myTehAwesumsWidget or whatever, and i want the layout to be defined in xml, can i include that xml in the referenced class library?
If you replace "class library" with "Android library project", everything you describe should work just fine. Here is an Android library project that distributes a custom widget (also wrapped in a dialog and custom preference).

Categories

Resources