Layout manipulation , from code - android

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);

Related

Android draw outside the view

I would work with a piece of layout that isn't shown, the user must scroll to see what I have did. How can I do it? Can I join 2 different layout in one? I prefer to collocate item as I do with default layout. If it can help, I use Android studio. I prefer use elements without code, adding them from palette. An example :
You can use two different layout in main relative layout & whenever u want child layout just set visibility.
You will want to use a ScrollView for this. From the documentation:
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.

Using on Activity in Android to draw multiple UI's

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.

Is it OK to add multiple views to root view of an Activity?

It may sound foolish, but I actually can't find anything on it.
Is it ok to add multiple views to the root view of activity in Android?
So for example I could go like this:
setContentView(R.layout.main);
setContentView(gLView);
setContentView(otherView);
Or simply retrieve it as a view
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(view1);
layout.addView(view2);
layout.addView(view3);
layout.addView(view4);
It all seems to work on devices I test, but is it guaranteed to work on all of them?
Or should I artificially create single FrameLayout to add to root view and add everything to this FrameLayout?
More experiments:
1) if I don't use setContentView and print:
Log.d("Test", this.getWindow().getDecorView().findViewById(android.R.id.content).getClass().getName());
I get: android.widget.FrameLayout
2) If I set content view to for example GLSurfaceView and print the same Log.d
It also prints android.widget.FrameLayout
3) And if I dont' use setContentView at all, and simply do
FrameLayout layout = (FrameLayout)this.getWindow().getDecorView().findViewById(android.R.id.content);
layout.addView(myView);
It attaches the view
So I assume that android.R.id.content returns not what you set by setContentView but the parent of what you set, or the actual root view in activity (which turns out is FrameLayout?)
And I am able to add multiple children to it this way, but question is:
Am I allowed to do that?
Yes, it's perfectly fine to add multiple content Views at the root level. The content Views are simply added to a FrameLayout container, and it is best practice to simply use it for your layout if a FrameLayout is all you require for it, instead of adding an additional container layer.
If you are adding a new content View, then you should use the addContentView() method instead of setContentView(), which would cause the existing content to be replaced instead.
Also, it is possible to add multiple Views to the content container in XML layouts as well by using the <merge> tag, which would just replace the base FrameLayout.
Calling setContentView several times will simply replace whatever view you set before. So what you want to do is create a root view that contains the multiple views you want to add and set the root view to your Activity with setContentView. Your second example with the FrameLayout is a good approach.
Your approach is valid at some extent. but its nice practice to call the setcontentView() once in activity.
this is because it will be very easy to maintain the Activity life cycle and reduce the app crash due to layout leak.
I personally call the setcontentView() once. I define all the layout in single XML file. Afterwords I call setVisibility(View.VISIBLE) and setVisibility(View.INVISIBLE) or setVisibility(View.GONE) to show or hide any particular layout containing my certain views.
Edit
Also its very easy to adjust the layout in XML because you can use simple drag and drop and If you are using relativeLayout then it will be very easy to place any view any where. but in Java code its some how difficult to place the views on your desired position
Hope this helps :)

Android: How to organaze layouts

I need to organize complicated Activity with many Views inside.
Also layout should depending on screen orientation.
Inner elements - it actually some templates.
Help me please to find out what is the best way to organize this?
Should I create layouts dynamically or I can create some xml templates and use them?
And if I want to realize Template3 as clickable element is it enough to define onListItemClick for ListView (ListItem has an xml declaration, so the ids will be the same for all elements).
And all the template elements should have the same width and height. It's OK with the ListView, But what to do with Template1? I want each items has the same height, and match parent view.
How to organize onConfigurationChanged() ? Where and how I can realize adding or removing 3 additional Template1 elements? Dynamically? Or create 2 xml?
What is the best for performance?
Maybe someone can give me a link to "example of complicated layout" or something like this? :)
Try searching on concept of fragmentation,it will solve the problem.

Graphical Layout shows first view of ViewFlipper, how to see others?

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.

Categories

Resources