How can I get the underlying XML from a dynamically modified view? - android

Thanks in advance...
I allow users to drag/drop views(text, image, etc) onto a RelativeLayout to create 'presentations'. I would ideally like to extract the resulting xml so the presentation can be saved to a fragment or .axml file, and recreated later either on the same device or a different one. I know I can iterate the children and manually create the xml I need, but it seems to me that since it must exist in the relative layout, that there is a way to extract it. Any ideas are appreciated!

You can't extract the XML for dynamically generated views. But what you can do is use XMLSerializer to make an XML of that view that you're generating. Check out my question.
To be frank, you can not inflate that XML dynamically (in future if you were going to do that). So, make sure you know what you're doing.

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.

best method to frequently change view inside layout

I want to add and remove some views frequently like
Recycler view,textview,seekbar,customview,imagebutton etc.!!!!
I know following methode to perform it
Add all view to layout and just play with Visibility.GONE, Visible and other..
using layout params add and remove view...
Use View.inflater to add and remove predefined XML views(I'm using)
So question is
1.Is there any other method to do it?
2. Which one you prefer and why?
When you make Visibility.GONE all the views and associated resources such as image, sound files will be kept in the memory and if you have a lot if resources that may slow down your application. I think better way would be to use Fragments.
Check out this link. Hope this helped

Is it possible to make a sample android view (Card View) in XML and store it in a a variable then use it again and again?

I want to use a cardView having some internal structure at multiple places. Is there a way to make a cardView in an extra layout XML file and then store it in a variable programmatically and later use that variable to show that cardView where ever I want by adding it dynamically?
I want to make that cardView sample in XML as it much easier to edit there.
Currently, when I try above method I get an error "view already has a parent" for obvious reasons. I can definitely copy paste code but i would like to know if there is any smart way.
Any help will be appreciated. Let me know if you need any other detail or find it hard to understand my question.
you can do it.......
have a separate xml for your card view...........let say card.xml
in your main/activity xml make/select a parent layout (Linear / Relative) where you want to place it......or may be different view..
private void addMyCard(){
ParentLayoutType parent=(ParentLayoutType) findViewById(R.id.idGivenToParent);
View yourCardViewName= LayoutInflater.from(this).inflate(R.layout.card,null);
parent.addView(yourCardViewName);
}
Hope it helps

Parse android xml layout and create views from it programmaticaly

I have the following problem:
Download complex xml-layout with many views from the server while running the application, dynamically attach it to app and display the necessary elements with ability to work with them.
I learned that at the moment this is not possible, at least there are no simple ways to do this
(https://stackoverflow.com/a/1942224/1956299).
I am wondering is it possible to parse downloaded xml-layout for my self and create the necessary ui elements programmatically? Considering layout can have many different elements with child views etc.? If yes, how it would be better and easier to do this?
There is only solution to do this
Read Xml By XmlPullParser line by line get the View Tag from incoming layout file,View attributes and create View Dynamically by java code

Android Dynamic Content Design

I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);

Categories

Resources