Using on Activity in Android to draw multiple UI's - android

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.

Related

Android UI: Most efficient way to have multiple elements in a single activity

In an Android application I'm building, I have a single activity which has multiple buttons and textviews. Based on user actions, I hide the elements and/or show them. This is intentional, cause I don't want the user to change activity.
For further clarity, the activity is a type controller. So when a user enables an option different buttons show up. This is why I want all of them in the same activity.
I am concerned about performance. Currently everything is in a Constraint Layout. Is there an effective way of having mutliple UI elements in the same activity, and having the ability to hide and show them at will? Should I look into Fragments?
Thank you in advance.
As it turns out there isn't a clear answer.
There are a few solutions that may prove useful, I list them below for anyone who might stumble onto this:
Fragment is useful when you have multiple independent parts of your layout. You are limited in the communication between elements of different Fragments.
ViewStub is used when you have some elements which are rarely used in your layout. This makes the initial loading of the layout lighter by not inflating the elements in the ViewStub. You can then inflate the elements on demand.
ViewFlipper allows you to cycle through a list of elements (or display them selectively using setDisplayedChild). It's mostly used for single elements, so I'm not sure about its performance if you were to use it for showing and hiding nested layouts.
ConstraintLayout is probably the best solution for cases similar to mine, where you have a layout of buttons and text which are hidden and displayed in a complex manner.

creating a class that holds 3 separate views

Basically I want one class to hold 3 separate Views on one screen.
I would like the top half of the screen to display a panoramic View (which I have defined in Panorama.java), and the bottom half will be split in half vertically and have 2 separate Views on them. On the left I would have my map class(CampusMap.java) and on the right I would have my other Activity (CampusTour.java).
Is this possible? If so, how would I implement it?
Fragments may be what you are looking for.
You can have multiple fragments on the same screen at the same time.
Fragments my friend...the answer you seek is Fragments...
They are basically modular code blocks that consist of an XML file married to Java code. They can be populated inside of a single container view, have work done to them via the container or via their own code, and can be removed and added at run time
you can only run one activity at a time. You probably looking to implement three views in a single layout.

Layout manipulation , from code

Let say we are in landscape and I have one root container
in that container other two(smaller). I want at run time to swap places of cotainers. The container A to go to the place of B and B to go to the place of the layout A.
My layout is defined in xml. So what is the best way to make two element to swap places ?
Is is good to change the properties of for example RelativeLayout.LayoutParams ?
First of all for scenario like this , what is the best layout to be used for the root container ?
after the call of setContentView(R.layout.mylayout);
and after root=findViewById(R.id.root); , a=findViewById(R.id.a);, b=findViewById(R.id.b);
should I remove the child views from the root layout ?
like root.removeallview(); ? or there is something else that I should do ?
I need someone with experience to explain me how should this kind of things done, of course evry suggestion is welcomed and if you have some useful links please share with me.
I want to be able to swap between this two different ways of presenting the layouts.
If you put these views into Fragments, then you can move the fragments around using a FragmentTransaction.
You could do it by setting alignParentLeft and alignParentRight with the LayoutParams.
If I were you I would probably take a different approach though. I would create my "main" layout that is just the top level container. Then create seperate xml files that represent all of the different layout configurations that you want to include in the app. At runtime use the LayoutInflator to inflate the xml file that you want to use into an object and add it to your parent layout with mainLayout.addView(child);

creating layouts dynamically and getting control on each layout

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?

Effective way to handle redundant UI elements in Android?

I have numerous activites in my Android app., and most should contain the same, relatively complex set of UI widgets on the screen's top area (lets say that its a kind of toolbar that you can find on most screens).
Right now, every screen's layout contains the markup for this toolbar, along with its logic inside every Activity's source, so it's damn redundant. Could you recommend a more efficient / less redunant way to do this?
I would take advantage of the <include> tag in the layout's xml. This will let you reuse that toolbar very easily and effectively. As for the code I would subclass Activity and place the logic in there, then each of you activities can subclass your custom Activity class.
There are 3 very useful articles on the dev site about this topic. Here is the first one link
I would create a custom View object (subclass View) and then include it in all of your layout xml. You can actually pass parameters, etc. just like built in views. Then define XML for that view that will always be used when that view is drawn on the screen. Also, this allows you to change the view and have that change populated across all of your Activities without having to manually modify all of the code.

Categories

Resources