Android: Is it possible to make a copy of a View? - android

The app I am working on requires a dynamic layout that displays a HorizontalScrollView of profiles. A profile is simply a RelativeLayout with a picture and some text. Since I get the data from a data file, I need to create a Relative layout for each of profiles. At first I created each RelativeLayout programmatically in a for loop and then added it to the parent view. This works, but I don't want to do it this way. I want to use different layout files based on the screen size of the device, etc.
Then I thought. Well, what if I had a Layout with just one profile on it? My code could get that one profile with findViewById() and then create new ones based off of it! In other words:
// get the layout
profileLayout = (LinearLayout) findViewById(R.id.profileLayout);
// get the first profile in the layout
originalProfile = (RelativeLayout) findViewById(R.id.profile1);
// make copy of profile
temporaryProfile = originalProfile;
// make changes to the this profile and add it back
profileLayout.addView(temporaryProfile);
Of course, this doesn't work because this is java and temporaryProfile is now a reference to originalProfile. So is there any way to make a copy of this RelativeLayout? I'm aware of LayoutInflater, but I still don't understand how it works. There is also Object.clone().

Not so much. Sounds like your particular case could benefit from a ListView of some kind and an adapter (see resources like this for info). If not, then a LayoutInflater is the best answer as it does precisely what you want. Acquiring one you can then use it to "inflate" any view you've defined in your XML layout files and do whatever you want with it.
Here's a great discussion of the inflater.

you can make a copy of your inflator with a new context. Layout Inflater - Android Developers

Why not simply use an include in the xml file for the layout that you want.

Related

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

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

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.

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.

LayoutInflater and ExpandableListView

I am still fairly new to android, but I am getting there!
I have created an ExpandableListView almost identical to the api sample code on the android developers website. The ExpandableList works wonderfully.
I tried creating custom layouts through xml and a SimpleExpandableAdaptor and had nightmares. Which brings me to the point:
I have been reading a lot about LayoutInflaters and how well they work with XML files. However, I would like to create two seperate ImageButtons and place them next to the groupExpanded indicator (in the group TextView) with out referencing xml. I don't think I can use xml to do this simply because the code supplied by API doesn't rely on xml, its created runtime.
Can LayoutInflater work in this situation (no xml)? Does anyone have suggestions where I might look for a clean solution?
Thanks in advance.
You can do both, either inflate a xml layout using a LayoutInflater or creating the View elements using the Android object model. I would suggest using xml files as you can easily change the layout to a later point without touching the code.
Have a look at this blog post it shows how to inflate layouts in an adapter for a ExpandableListView.
I believe LayoutInflater is used for the express purpose of loading an XML layout into a View object. Alternately, you can create your own view by constructing RelativeLayout/LinearLayout etc. and programmatically adding your ImageButtons to it
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.HORIZONTAL);
layout.addView(imageButton1);
layout.addView(imageButton2);

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