I'm working on the controls for a game, and require part of the control panel (gray in the figure below) to change dynamically, either showing a single canvas (left) or 5 buttons (right). The border between the lower-row views should always be positioned at exactly the same x-position as the border between the buttons on the upper row, as shown. At the same time, all twelve upper buttons should be scaled and distributed evenly.
I've considered several approaches, but as of yet none do all of what I want:
Using two LinearLayouts, one for each row of controls: reliably aligning the borders seems to be impossible, and replacing part of the layout is difficult at best.
Using a TableLayout: again, replacing a portion of the layout is difficult.
Using a RelativeLayout: resizing and aligning buttons independently of the screen size doesn't seem possible
Any suggestions for an alternative method, or on how to make one of the above approaches work? It would also be nice if there were some way to animate the change of views, i.e. sliding in the buttons from the left over the canvas. Thanks!
Interesting, I've done this several weeks ago. What I did is to make use of this property of View object: "Visibility". So that means at a fixed position, I can set any View to display on to, not depending on any type of Layout, it can be Visibility.GONE, Visibility.VISIBLE or Visibility.INVISIBLE.
In my app, I used RelativeLayout to set relative position to the right side TextView.
Give it a try :)
In order to close this question: I have solved the problem by writing a custom layout class that places and sizes the child views without heeding the measured size of the children. Effectively this gives me the behavior of a linear layout with layout weights, but is more deterministic with border placement.
A ViewAnimator is used to switch between the Canvas and the Buttons.
Related
I have a screen which basically consists of 2 parts
upper is a hierarchical structure of multiple LinearLayouts with regularly changing TextViews,
lower is a heavy for drawing custom view.
Currently the TextViews in the upper part have wrap_context set to their width, as a result every their change cause Android to relayout the whole page, including the hard for drawing custom view in the lower part.
My question - Is there another way to solve this problem except changing layout_width parameter for changing TextViews to constant value?
Create LinearLayout to be parent with vertical orienetation. Now create two children LinearLayout with vertical orienetation but with layout_height to 0dp for each and layout_weight to 1 for each respectively.
I have been struggling to figure out which layout to use and how to achieve what I am looking for. I've tried gridlayouts, linear and relative layouts as well as scroll views in many different combinations. What I want is something like Google now.
I have two buttons to launch the two modes in my app. I have an imagebutton in the top right which they press for more information and when they do so a text view slides out. I have made the layout work, and to overlay the imagebutton on my mode buttons I require a relative layout. However my problem comes from trying to optimise for different screen sizes. I would like it to centre the buttons to rest a quarter of the way down and the layout to pop out underneath towards the half way mark. For the bottom button I want it three quarters of the way down with a text view that drops down towards the bottom. If there isn't sufficient space to fit everything I want a scroll view for when the text views appear, but otherwise it should fit comfortably and not require scrolling.
How do I do this? Two nested relative layouts within a linearlayout, both with equal weight? Then some how I need to address that if the length of the height (taking in account orientation) does not allow it to fit, adapt it so the buttons are closer and scrollable.
You can use sepperate res/layout folders for different screen sizes just like with drawables, examples: layout-small, layout-large etc.
More explained here http://developer.android.com/training/multiscreen/screensizes.html#TaskUseWrapMatchPar
How to make multiple overlapped layout in android( for example I try to make two layout and make one layout to disable and other layout to enable for some operation, this layout overlapped with other layout).
You have a few options:
FrameLayout
This allows you to stack views directly on top of each other. This is nice and simple for some cases, but is limited. It will not help you align those views, simply stack them.
RelativeLayout
This is probably the best option, it allows stacking views on top of each other, but also allows for aligning views with each other. You can align edge, place views next to each other, or center in the parent. This is one of the most powerful views in Android.
AbsoluteLayout
This is deprecated and its use is discouraged. That having been said, this gives you pixel perfect view alignment if you know what the exact pixels should be. The reason it is deprecated is because Android devices have variable screen sizes and pixel densities, so more than likely when using this view you will be limiting your device support greatly.
I'm trying to layer graphics one top of each other, like an icon over a background, with the second layer (icon) at a certain pixel offset from the top left corner of first layer (background). Since each layer will eventually have its own animation, I'm placing each in its own View.
For my implementation I have two ImageViews, one for each layer, inside a RelativeLayout, which in turn is inside a ScrollView. ImageViews are positioned using layout_margin relative to the top left corner (0,0). The first ImageView is larger than the screen (background), while the second ImageView is smaller than it (icon). ScrollView automatically resizes the first ImageView (background) since it is larger than the screen, it does not resize the second since it is smaller (icon).
I need both of them to scale together, and I also need the positioning of the second layer over the first layer to adjust itself accordingly. This actually works well in a layer-list, but due to the animations I am forced to use Views. How can I scale and position multiple Views together, or do I need to build my own class for something that seems like it should be fairly basic?
Thanks in advance.
I had a similar problem in my android application. Android has introduced new set of API's to help us on this, setScaleX() and setScaleY().
Just call layout.getParent().setScaleX(); and layout.getParent().setScaleY();
I have 6 images I want to display as 2 rows with 3 images in each. I'm using nested LinearLayouts to achieve this, and it works well except for one thing:
The height of the largest image dictates the size of the linear layout, meaning there is empty space a lot of the time. In other words, my problem is as follows:
I keep getting the layout shown on the left, and I want the layout shown on the right.
I am aware that you can just use GridView, but that will still prevent the exact layout shown on the right, so I'm at a loss really. Many thanks.
Instead of 2 rows of three columns, you need 3 columns of 2 rows. LinearLayouts would be fine, just to be sure set the Gravity of the individual cells to Gravity.TOP.
You could equally achieve the whole grid using RelativeLayout instead of Linear. Each of your bottom row would just need android:layout_below and android:layout_alignLeft set to be the ImageView above it.