Creating layout dynamically from template - android

Is it possible to load a layout XML at runtime and load into activity?
In my app, I have various types of data like Person, Company, City, etc; The requirement is to dynamically load the layout, find views by tags (property names like Person.name, Person.address) and then fill in data. For example, if user has selected an object of type Company, we want to load a company.xml layout, inflate it and then associate various properties (company name, company slogan, city, address, revenue) to tagged views. One possibility I see here is - each view in the layout will be associated with property-name as tag and then appropriate data will be loaded in appropriate views.
What should be the best design you would recommend?

Is it possible to load a layout XML at runtime and load into activity?
Not using standard Android layout XML files. Those have to be precompiled into your APK. The API does not support inflating layouts from anything but layout resources.
You could create your own interpreter of Android layout XML files, or roll something else similar.

Related

How Can i replicate Photoshop like functionality to save and restore UI Layouts?

I built an android application in which user can add views like edit text, text views & image views to Relative layout and export the layout as image . But most of my users requesting feature to save their work as template to work on it later point just as like .PSD file in Photoshop. I am unable to find a Way to achieve this. How can I save Views and its properties inside a layout to inflate them later at any point of time.
I think you can try this way. Although I haven't written the relevant code, I think it is feasible.
You can create a class, this class has the following properties:
View type
View level
View's superView
View's subViews
Size
Layouts
etc.(you can add it according to your needs.)
Then convert each View into this class, save all the converted classes to the local, when the next time you need to use, you can read all the classes saved locally last time, and according to the class attributes, such as view type, view level, size, layouts and other attributes create all views in turn.

Creating new Views by Drag and Drop & Controlling them (custom layouts)

The main purpose of the app is to provide education courses for students.
I have two actors:
Teachers who create the content of the course from inside the android app
where the lesson could be any combination of android views.
Students who view these courses and interact with lessons, may be click button to hear sound (Only view this lessons)
Stage 1 : Implementing drag and drop editor
So far I have been able to create new Views (ImageViews TextViews ... etc) & drop them in new layout.
But once I drop them I have no control on them any longer. Now I am restricted to one style of layout, which I do not want. I want to reach the level of android studio layout editor, where I can add Views, change their position, size, etc.
Stage 2 : Save layout
Would you suggest a way to save layout data to be shown to Students. JSON? XML?
Stage 3: Parsing Layout data
I have implemented XML parser and tested it using a layout that I had created in android studio. However, depending on your suggestion I may change the way I parse the lesson.
Drag & DropLayout Editor - Instructor Layout - Student Layout - Android Studio
I would suggest the following:
Stage 1
Create custom views for all widgets which can be dropped (LinearLayout, ImageView, etc). Let them all implement the following interface:
interface Layout {
Layout getLayout();
LayoutEditor getLayoutEditor();
}
Where Layout is the serializable representation of that view's layout (XML or JSON) and LayoutEditor is another interface which can used to modify the attributes on that View (layout_width, layout_height background, etc)
Stage 2
So after you have dragged and dropped widgets on to say the app canvas you get a view hierarchy of Views which implement Layout interface. Just walk this tree recursively and call getLayout() on each view and stitch together a Layout hierarchy. Which you can then serialize and save.
Stage 3
You deserialize the Layout you saved and walk through the layout tree and recreate the View hierarchy with it.
Check out proteus for some inpiration. This uses JSON layouts to inflate native Android views.

accessing layout.xml through java and assigning values to views in android

I am new to android programming am trying to learn it.
So basically there are two ways of creating whatever is going to be visible on the screen :-
We create views and view group objects inside layout.xml files. And then as the need be we access those views that are already existing
in the layout through our java programs by accessing their ids ( as
r.java….). So basically when we start a particular activity, we
set the content to be displayed corresponding to that activity using
setContentView method, to which we pass the layout.xml file, inside
which we have defined the different views and view groups to be
displayed on the screen.
The second way is we create these views dynamically though our java programs and then set them as the content on the screen using
setContentView again.
Now the above is my basic understanding. Please let me know if the above needs correction.
Now what I want to understand here is :-
Is there a way that using the first method itself, we can do the
vice-versa, as in say instead of fetching the the views through their
ids from the layout.xml files, can we already have a predefined
layout.xml file with different views having ids, and now through our
java programs can we just access those views though their ids and set
their values, something like say (in javascript) :-
document.getElementById(“someTextBoxId”).value= “some calculated
value from java code here”
Thanks.

Android - Using different resource files for a base activity?

I'm quite new to Android so go easy on me!
I'm building an app that catalogs a bunch of restaurants and displays each of their menus, depending on the selection of the user.
Is it possible to store the info of each restaurant in a separate resource file, and have a template-like activity that will display the particular info that the user selects from a spinner?
Yes, it is possible. You can replace views (which can be loaded from different layout.xml files). You can load a layout XML file using LayoutInflater. You can add views into a layout (which is a ViewGroup) using one of the addView() methods, and there are also methods to remove views.
It is unusual, however, to hardcode data into any layout. It is usually better to store the data in a database or file, and use a layout which is more like a template, where you replace the data in the layout at runtime.

Is there a way to add layouts at runtime to an Android app?

I want to create an app to which I can add layouts over time. E.g. The app ships with a known set of layouts, but can download new theme packs without having to distribute a new apk. I don't expect these to be stored in the layouts directory - they can be stored anywhere the app can access as long as I have a way of loading them.
It looks like layouts need to be compiled into binary form if I'm going to use them with the LayoutInflator. So is there a way to compile them before distribution? I would really rather not have to write an interpreter for these as I'm sure it would be quite a bit slower.
This layout file in another layout via LayoutInflater.
Suppose, You want to add this layout in your main layout. So, Take a linear layout in main layout and this list layout in main layout via mainlayout.add(view) , Here view is LayoutInflater viw.
Example :
private View view;
view = getLayoutInflater().inflate(R.layout.image_sub_layout,null);
topicLinearLayout.addView(view); // main Layout Linear layout
Thanks.

Categories

Resources