Format buttons to fill parent based on dynamically added text - android

I'm hoping I can get some formatting help. I have a TableLayout. Inside the TableRow, I have 2 RelativeLayouts. Each RelativeLayout has 2 buttons in it that overlap (this is by design). The button text is dynamically added at runtime. This makes the buttons taller if the text is long enough. However, the text in one column (RelativeLayout) is different that the text in other column (RelativeLayout). So, the TableRow is expanded by the taller of the buttons. What I'd like is to have the smaller buttons fill in to the same size as the larger one.
I'm real new to Android development, but I've tried every combination of match_parent/fill_parent/wrap_content that I can think of, but none of them make the buttons stay the same size (that is, the size of the larger one). Some even expand the button to the full height of the screen (which I don't understand either...I'd think that the parent would be the TableRow and that would limit it)...
I appreciate any advice.

Use
button.setEllipsize(TruncateAt.END);
attribute while creating the buttons

Related

How to set row height of GridView fit to Screen in Android

I want to stretch the row of a GridView to match the height of the screen in Android.
How do I do this?
You are in control over your row heights, by virtue of what you put in
them. Since your cells appear to have more than one widget, they are
presumably wrapped in a LinearLayout or something. Set your
LinearLayouts to be some specific height, and the rows will all be
that height.
Personally, I think you should be resizing your images if you are
going to have text above and below each image on a per-cell basis.
With the Reference commonsware link

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.

Causing a 3 row GridView to fill the available vertical space?

I have a two column, three row GridView that I would like to have fill the available screen space provided by the surrounding layouts. By setting the layout_height and layout_width to "fill_parent" and the background color to red I can see that the GridView is in fact filling the space -- but the content is not.
When there is extra space, I'd like the GridView to have more verticalSpacing or horizontalSpacing to "fill up" the screen. I don't see an available setting to do that though.
There has to be an elegant way to do this, but I've yet to find it =\

Relative stacking with Linear Layout in Android?

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.

Layout buttons so each divides up the space equally

Im using a LinearLayout to put two buttons horizontally side-by-side, but I want to each button to size itself to use 50% of the horizontal space. I thought layout_weight of "1" for each button would do the trick, but maybe my layout_width needs to be changed?
The layout_weight attribute controls how much of the left over space each of your buttons is given. If your buttons take up different amounts of space to start with, then each of them will first be given the space they ask for, and then any remaining space will be divided up between them, meaning that you won't have an exact 50/50 split.
You can get around this behaviour by first setting layout_width="0px" (keeping your layout_weights as they are), and relying on 50% being enough space to display each button.
I would set the layout_width of each button to 0px, then use the layout_weight=1 trick you mentioned.

Categories

Resources