How can I vary the height of some keys within a row? - android

I'm creating a custom keyboard layout. The SDK allows changing the width of keys in a row (as in ThickButtons), but ideally I'd like to be able to vary both the height and width of keys within a row (and still have the keys occupy all the available space.)
Another way of looking at this is that I want to allow some keys to be in more than one contiguous row. Any ideas would help. Thank you.

Will all key "widths" and "heights" be a multiple of the smallest key width/height? If so, you can nest horizontal and vertical LinearLayouts as necessary to describe the keyboard, using layout_weight to make your double-wide or double-high keys.
For example, if I look at the keypad to my workstation, it is essentially a 5x4 grid of keys, with a double-high "+" and "enter" key in the right-most column and a double-wide 0 in the bottom-left position (fairly standard configuration, refer to your own in this description). I can make vertical linear layouts for each of the columns "on top of" the 0 {numlock,7,4,1} and {/,8,5,2}. These can next be put into a horizontal linear layout to group all 8 keys. This assembly can be combined with the "0" button in another vertical layout, with weight "4" to the 8 key assembly and "1" to the 0 key. Let's call this full assembly of 9 keys assembly A. Now you can make vertical linear layouts for {*,9,6,3} and {-,+,enter}. In the second of these, give weightings of 2 to each of the + and enter key. Finally use a horizontal linear layout to group assembly A with each of the other 2 vertical layouts.

Related

How to avoid screen with changing TextViews relayout?

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.

LinearLayout weight choices

Why would someone use weights 2:4 instead of weights 1:2? I am looking through a Udacity course layout. It's a LinearLayout with two children views. and the children are given weights 2 and 4 respectively as opposed to 1 and 2. Why is that?
Because they wanted to? The actual values don't matter, the ratios are all that does. Usually when you see that its code that evolved over time, they originally had something in there with weight 1 and removed it (while weights don't need to be whole number, most people try to keep them that way).

Creating a keypad screen in Android

I need to create an activity where the screen is a keypad. At the top the PIN is displayed and below this are large buttons with the numbers 0 to 9 arranged in 4 rows and 3 columns (a delete key and OK button are included). The keypad needs to resize its buttons with different screen sizes. What is the preferred way of doing this? Should it be done (can it be done) using only Relative layouts or with Linear layouts or with both?
Linear layouts and weighting should do the trick.
See this question for more information

How to create a regular, resizable grid without nested weights?

I've one of the simplest layouts imaginable: A num pad.
I want to create a fragment containing a 3 x 4 grid of buttons. The layout should automatically resize the num pad to fill the available space.
I've learned, that GridLayout is not up to the task, and TableLayout/TableRow or nesting LinearLayouts means nesting weights, which is also discouraged for performance reasons. A RelativeLayout won't work either, because that requires at least one button with given dimensions.
So, is there a clean way to create a regular grid that will resize to fill its parent?
Any help is appreciated, thx!
You will need a custom compound control.
Check the following link:
http://developer.android.com/guide/topics/ui/custom-components.html#compound
Make the control fill the available space. Make it to have 12 buttons. Calculate the size and position of them based on their position and the available space.
Depending on your needs you might also need to override onMeasure() and onLayout() defined earlier in the above document, in the "Fully Customized Components" section.

Evenly spacing views within adjacent layouts

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.

Categories

Resources