i have to create a layouts dynamically depending on the some action for example depending on action i have created 5 layouts which is horizontal scroll and i want the control of each layout how to go about it please give me some suggestion
Your requirement is not entirely clear. If you want to load layouts from a xml resource fiel you can use LayoutInflater. This will also return a handle to the loaded layout that you can use for further manipulation.
You should use the layoutInflater to load the layout.xml files, later in List add the handlers in for loop.
unless you set the inflated layouts to some Activity to display it has no effect. Hence consider ViewStub and inflate the things inside the ViewStub.
If you are inflating and adding it as a part of some component ( like listView items), then you can get the references using a global ArrayList, to store all those handles.
I am unable to understand your question fully, can you please elaborate your problem?
Related
I would like to know if it is wise/possible to have one activity that displays multiple different UI elements dynamically in a single layout?
So I want to have a single activity that loads a blank layout and then from code I add various UI elements such as buttons, text views etc. Then when a button is pressed, for that layout to clear and then from code draw the next set of UI elements on that same layout and so on and so forth?
Or would it be better to have multiple xml layout files and just inflate them each time I want to use a different layout, so then not create them from code?
Hope that makes sense.
Thanks,
Wihan
You should look into Fragments.
Activities are not intended to do what you would like them to do.
Instead you use one Activity and add a Fragment(s). Those Fragments can then be dynamically switched via code.
Take a tour => http://developer.android.com/guide/components/fragments.html
Yes this is very much possible. But Android xml layouts give a very easy way to use and manage different views. You could add views to ViewGroup and clear the ViewGroup.
I would also suggest using Fragments . This could be dynamically added and replaced.
I am going to start one app where my activity page will contain "n" grouped views. Grouped view means "collections of views (i.e. One group can have TextView+Button+ImageView)". So the page will have "n" number of such grouped views.
I need suggestions like what would be the best practice to implement this. I could think of below ones:
1) Should a ScrollView be used (Then I will have to create groups in runtime and place one under another)?
2) Or a ListView be used (Then how can I accommodate the height of each row as grouped views height may differ from each other?)
Or is there any other way I can go along with?
Appreciate the suggestions and any sample examples if have. Advance Thanks.
Both options would work, it really depends on your use case.
Place a vertical LinearLayout inside of a ScrollView and add your grouped-views to the LinearLayout. I would recommend this if you have a relatively small number of such views (not necessarily a fixed number, but small enough that you wouldn't have to scroll many "pages" to see them all). Make sure the ScrollView has android:layout_height="match_parent" and the LinearLayout has android:layout_height="wrap_content".
If the number of grouped-views is not small, you could use a ListView and make an Adapter for it. This lets you take advantage of ListView's automatic view recycling when items get scrolled off screen.
For either case, make an XML file for just the grouped-views. In code, you would get a LayoutInflater object (usually by calling Activity.getLayoutInflater()) and call inflate(R.layout.your_grouped_views, null). If using the LinearLayout, you would add it in code with one of the LinearLayout.addView(..) methods; if using the ListView, your adapter would return the whole thing from getView(...).
create one xml layout containing the constant elements of your group view.
in you main xml layout which will be the contentView of your application, put a ScrollView and a single LinearLayout.
then in the program inflate as many views of your group view as you want.
For your answer i want to give you referance of this website, on this website you can learn create dynamic view in android...
I am using Eclipse and a ViewFlipper. In Graphical Layout, I want to see the second, third, and forth layouts of my views - right now, I can only see the first view. Any suggestions?
If I'm understanding you correctly, you want to see each view in the 'Graphical layout' tool? The way I do this, is instead of having all the layout work done in one xml (where your viewflipper is) I make each view a new layout xml. And then have each view (xml file) included into the view flipper by using this....
<include
layout="#layout/layout_media"
android:id="#+id/flipper_media" />
Hope this helps.
just put each layout in relative layout or linear what ever you are working with then with each layout you will work with the first one in the order and etc.. then at the end put each layout in the order you want later
I had to subclass the ViewSwitcher class to display an indeterminate ProgressBar until data is ready to display in the second view. I used isInEditMode() to determine whether I was actually running the app or just previewing in AS.
You should be able to add a custom attribute to choose which child to display. This might look a bit overkill, but if you happen to already have to subclass your ViewSwitcher or ViewFlipper, i think it is not a big deal.
I will try to put an example later.
i want to get views from layout defined in my layout folder but i get null pointer exception, i know that before i use findViewById i have to have the layout somewhere used in any activity so it gets initialized, is there anyway to get views from layout that has not been used in any activity cause i got one activity that uses layout which i dynamically change and in that layout i remove all of it's child views and apply one of the three layouts that i have aside as potential layouts to fill in the default activity's layout..
I need this to make dialogs cause i have one dialog that has one layout which is transparent and has attached onTouchListener and when i click the layout the dialog dismisses, now my idea is to get other layouts which have size defined and then fill in that layout in the default transparent layout of my custom dialog, and do this with any layout i could image so i can make dialogs from all the layouts i want..
Thanks, if you need anymore info about my idea please write me this is important to me!
You can use getResources() from the context to retrieve resources individually:
getResources().getLayout(id);
More info here: Accessing Resources
What are the differences between <\include> tag and <\ViewStub> tag and which one is preferrable while designing the layout.
The < include /> will just include the xml contents in your base xml file as if the whole thing was just a single big file. It's a nice way to share layout parts between different layouts.
The < ViewStub /> is a bit different because it is not directly included, and will be loaded only when you actually use it/need it, ie, when you set its visibility to VISIBLE (actually visible) or INVISIBLE (still not visible, but its size isn't 0 anymore). This a nice optimization because you could have a complex layout with tons of small views or headers anywhere, and still have your Activity load up really fast. Once you use one of those views, it'll be loaded.
include
It is used to reuse layout resource
ViewStub
It is used to lazily inflate layout resource
Sharing and reusing layouts is very easy with Android thanks to the tag, sometimes even too easy and you might end up with user interfaces that contain a large number of views, some of which are rarely used. Thankfully, Android offers a very special widget called ViewStub, which brings you all the benefits of the without polluting your user interface with rarely used views.
A ViewStub is a dumb and lightweight view. It has no dimension, it does not draw anything and does not participate in the layout in any way. This means a ViewStub is very cheap to inflate and very cheap to keep in a view hierarchy. A ViewStub can be best described as a lazy include. The layout referenced by a ViewStub is inflated and added to the user interface only when you decide so.
Another important difference is related to layout inflating. with it is not possible to change the layout already static inflated in XML, it is necessary to replace the view and set programmatically al the layout parameters.
With it is possible to define (for e.g.) height, width, etc... and inflate different layout at runtime time