Expand one View in a LinearLayout without moving the other views - android-widget

I have a vertical LinearLayout with 2 views on top of each other.
The top view should be an expandable panel - if clicked, it should expand down, but without affecting the other view.
I want the bottom view to always be aligned by the "closed" version of the top view.
The top view should animate to the opened version, so I can't just hide the opened version behind.
How would you accomplish this?

A LinearLayout will not allow what you are asking for. There are no set of layout parameters that will allow children in a LinearLayout to overlap.
You could use a FrameLayout instead. The trick here is getting the layout just right. Here is one example of how you might do it. The ImageViews with ID's of topview and bottomview are the views you care about, the rest is structure to get the views to be where you want them.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="fill_parent"
android:layout_height="70dip" />
<ImageView
android:id="#+id/bottomview"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#FFFF0000"/>
</LinearLayout>
<ImageView
android:id="#+id/topview"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:background="#FF00FF00"/>
</FrameLayout>
Then you just need to add an onClick listener to the top view and change the layout params. For example, here is a quick example of expanding the view.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView i=(ImageView)findViewById(R.id.topview);
i.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Make the view bigger
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,300);
v.setLayoutParams(lp);
v.invalidate();
}
});
... more code...
The rest would be adding code to keep track of expand/collapse state and adding animation.
Hope that helps.

Related

Can't scroll using Scroll View

Whenever I press play, I can't move the scrollbar or the scroll view. The scrollbar also resets for some reason...
I was trying to make a questionnaire but it just doesn't work. And yes, I have set the viewport and the scrollbar, and the react transform.
you have to add vetrical or horizontal layout group to content gameobject under scrollview>viewport>content. also you have to add content size fitter and set it to preferred size for vertical or horizontal. then your scrollview will work.

Align UIView's subviews

I have a list of views dynamically populated and then added to a fixed UIView. Now I want a behavior such that all these views get horizontally or vertically stacked up, i.e, all the CGRects get aligned back to back.
I know I can simply go through all subviews and align them based of their previous sibling's frame, but is there a better way to do it?
for(UIView *view of [self view].subviews)
{
//code to align
}
Moreover, if I delete some view from the queue, the rest of views should realign (gravity effect) to fill up that space. Any ideas?
Unfortunately there is no container or control that has this functionality. On other platforms there is something like a container where you place controls and it will align those controls horizontally or vertically, and wrapping them on the next line / column, but there is nothing like this on iPhone.
However you can create easily a UIView subclass that creates this functionality ...
Did you try the Layer gravity property?

UIScrollView inside a CALayer

I'm developing some kind of card game in which my view objects (cards, etc) are all CALayers. These objects are added as sublayers of a mainBoard (a CALayer), which, in turn, is a sublayer of the [UIView layer] property of my mainView. Everything is Ok, I can do hit testing to verify which layer is being touched through the touchesBegan:withEvent: callback of the mainView class normally, and so on.
However, I need to have a scroll inside my mainBoard to show "bigger" CALayers, so I tried first adding the "bigger" CALayer inside a CAScrollLayer, but I noticed that the CAScrollLayer doesn't really scroll (doesn't handle user input, neither draws scrollbars). So, the workaround would be to add an UIScrollView directly to the mainView UIView. The UIScrollView scrolls perfectly, until I try to add the "bigger" layer to it, by using [scrollView.layer addSublayer:<bigger layer>].
Does anyone has any idea of how I can have "scrollable" objects inside a CALayer?
Thanks!
nacho4d pointed the answer for my question.
Replying to mark: the "bigger" layer (a layer whose bounds is bigger than the area available in the screen (and so, needs to be contained in a scrollable area)).
The solution was to first wrap the "bigger" layer in a UIView:
UIView *wrapperView = ...
[wrapperView.layer addSublayer:<bigger_layer>];
afterwards, I can add the view as a subview of the UIScrollview:
[<uiscrollview> addSubview:wrapperView];
// set up scrolling properties
<uiscrollview>.contentSize = <bigger_layer>.frame.size;
The problem was that I was trying to add the "bigger" layer as a sublayer of the UIScrollview directly, instead, wrapping it in a UIView and adding it as a subview worked.
Thanks

What's the simplest way to make all my controls in my root UIView scroll?

I have a ViewController that manages a view full of controls, including UITextViews, UIButtons, and a drawing view. It's far more than I can fit on the screen, but I'd like my design to have it all in one place and make the root view scrollable. I've become very spoiled with Interface Builder automating much of the coding work for me. Is there a way to add a UIScrollView to my root UIView and then attach all my other controls/views to that UIScrollView and let IB automate the functionality to make it all scroll when the user swipes their finger? If not, what's the simplest way to make all my controls in my root UIView scroll vertically?
What you want to do is make a UIScrollView the root view for your view controller, and then attach your composed view as a subview of the scroll view. The scroll view will take care of the rest.

How to make modal view scrollable?

I have a modal view with a textview and a button. When the keyboard is displayed it covers up some of my UI elements. I'd like my modal to be scrollable so that it can be viewed while the keyboard is displayed. How can I do this?
I've had the same question, and i've played with it a bit,
setting a UIScrollView is not enough, as in the inspector
you should 1st. increase it Hight, then in the attributes, check the following checkboxes:
Scrolling enabled, Bounce Scroll, always bounce vertically.
Edited: Forgot the most inportant thing:
in the size inspector, set the Buttom field (under content) to the size you wish, (960 is twice the regular size)
Use a UIScrollView instead of an ordinary UIView. You can disable scrolling when you don't want the user to be able to with:
[theView setScrollEnabled:NO];