I have a parent Linear Layout inside which there are two Linear Layout and a button.Each Linear Layout inside the parent Linear layout has two items in it. Items include a edit text and a spinner.what I want is to swap this two LinearLayout when i click the button.So my question is How do i do that programatically?. I am new to Android development so please help me to solve this.
On your outermost LinearLayout, try calling removeViewAt(0). This should remove the first LinearLayout. Then call addView() passing it the first LinearLayout.
One simple way would be to just make two layouts. Designed such that when you are ready to use the other one, switch layouts and then set that as the current view.
Or you could do so with various animation classes if trying to get fancy with it. If there is an exact visual effect you are trying to receive, perhaps provide a bit more on what you want.
As Karim mentioned, you can use setVisibilty() to View.GONE and the to View.VISIBLE.
But for a smooth swapping you are going to need to learn about Translate Animation.
Here a working example of how to swap two views (e.g. two LinearLayouts):
ViewGroup root = findViewById(R.id.my_root);
// assumption: root has 2 child views only
// swap left and right (or top and bottom)
View leftView = root.getChildAt(0);
root.removeViewAt(0);
root.addView(leftView);
// now the two child views of root are swapped
Related
I have two layouts, manageLayout and mainLayout. They have constraints to the parent on all sides. I need in a one time have manageLayout on the top of mainLayout, and on the other time mainLayout on the top of manageLayout. Of course, there is sense in using visibility=gone on one of them, but i need one layout on the background of another. Problem: layout on the background handle events from top layout. How to make lower layout(and his elements) untouchable when another layout is risen?
Layout tree image:
LayoutTreeImg
Code sample, where i want to disable communications with lower layout: https://pastebin.com/PeL7u3YD (not only isSaveEnabled=false had no effect, also isEnabled=false had no effects too)
If you just need an explanation.
Once you've initialized both your views for mainLayout and manageLayout, you will need to set an empty onClickListener on both of them. Basically, layouts should get the click but do nothing. This way you can block the layout and widgets underneath the view on Front from getting clicked.
Now for for switching view to front maintain a boolean to know which view is on the front and on your button click set the other view bringToFront() (Or try some other ways mentioned here if you want) and don't forget to switch the boolean value.
Let me know if this works for you or you have any issues regarding this.
According to my perception, you can make lower layout setEnable(false). I hope it will work.
I have a situation where I need to swap 2-5 linear layouts.
The linear layouts are fixed in xml and currently they are either visible or gone.
In some cases, I now require the visible layouts to rearrange.
Can anyone suggest some quick work around?
I did it on my own.
I put all the layouts inside another layout.
On runtime, I fetched all the childs --> layouts of this new parent layout.
Had two lists, visible and gone.
With the required logic, I divide these layouts into these lists and arrange them as required.
Remove all the childs from parent layout.
Add back the layouts from the list as required.
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.
I want to define a layout that it's orientation is horizontal, but if width of the child views exceeds the width, it adds the new child view in a new line.
can it be done?
For this you will need a FlowLayout whose custom implementation is given on this link
Courtesy : Nishant Nair
http://nishantvnair.wordpress.com/2010/09/28/flowlayout-in-android/
You can use RelativeLayout as the parent view, and put many LinearLayouts in it, giving each of them a separate id, and adjusting their position relative to each other (in your case "below").
You can use grid view as layout to achieve this
NOp, :) not in the way you want it(not with linear layout). I am not sure but I think in java there was a FlowLayout that starts from left and keep moving to right when there is no space left it starts new row. But in android there is no such a layout.
But you can do some workarounds for this problem. There is a lot of differen solution but it cannot be done with linear layout. If you set orientation tho horizontal in will never go to new line.
as #Rasel suggested the best solution for you is to use gridview and do nothing about the orientation
Let me explain the scenario that I want to achieve:-
Consider the below as the Layout I have inside a Parent_Linearlayout:
[Linear Layout] (Fill_Parent, Wrap_Content)
[ScrollView]
Activity's setContentView is set to the Parent_Linearlayout
In the application, when a condition is met, I want the Scrollview to be removed from the screen and instead put another View in its place.
I've been able to do this, & when I remove the ScrollView, I'm applying translate Animation to it so that it seems as if the View has gone to the top -before removing it.
But when the animation occurs, the ScrollView translates OVER the Linear layout present above it.
How do I restrict it, so that the scrollview does not go over the linear layout, but disappears at the base of the Linearlayout. I want the linearlayout to always stay visible..
I've been trying to do this from quite some time, but I've not been able to get desired results..
Could someone kindly help me out here??
I don't quite understand your description of your layout, but the Android view system is drawn based on the ordering of the views in the hierarchy. Views added later to a parent are drawn after those added earlier. So if you always want the LinearLayout to be drawn on top of the ScrollView if/when they overlap, then declare or add the ScrollView object to its parent before the LinearLayout object.
In thinking more about this, I suppose the ordering here is important because you want the ScrollView to be placed below the LinearLayout in the parent of both of these views. Putting the ScrollView first (and thus having it painted first) would then put it above the other LinearLayout, which isn't what you want.
There are various ways to achieve what you want. For example, you could use a RelativeLayout as the parent of the views, then the ordering is not important.
Alternatively, you could place the ScrollView inside another LinearLayout (and that LinearLayout would be the second child of the overall parent layout). Then when you animate the ScrollView, it would be clipped by its immediate parent, which I believe would give you the effect you're looking for (make sure that setClipChildren() is set to true on this new intermediate LinearLayout, which it is by default, otherwise it won't clip the ScrollView as it animates out of it). Note that this approach would necessitate different animation values, since you are now animating the view outside of its parent (the new LinearLayout).