Android: scaling button widths proportionally to screen width - android

I have a group of Buttons which have varying widths, and I'm inserting them programmatically into a RelativeLayout (horizontal orientation). I'm trying to scale the width of the buttons so that they take up the entire width of the visible screen. The important thing is, the buttons' widths should remain proportionally the same to each other; just stretched or shrunk to take up the entire screen width.
I'm setting the initial width of each button to a discrete, unrelated int value. In most cases, the group of buttons only take up part of the screen width. I'd like to stretch the buttons' widths, to take up the entire screen width, while retaining the button widths' proportions, relative to each other.
Can someone give me some assistance in calculating the new, adjusted widths of the buttons? I have no problem getting the screen width in pixels, and I can also easily get the total widths (in pixels) of all the buttons. I just need to scale each buttons' width, and I have to do it programmatically; not in the xml. TIA.

Figured it out (derp). Just set each button's width to (original button width) * ((screen width pixels) / (total buttons' widths)). The trick was getting an actual decimal value out of the quotient above.

Related

Dynamic height of ImageView in scrollView

I am trying to add 3-4 imageviews in a scroll view. Imageviews have dynamic heights. Height of first view have 70% of the screen and second and third have 30% height. I am using constraint layout and guidelines but the height is getting set according to the height of the scroll view but not according to the screen height. Is there a way to do this with constraint layout.
Have you tried doing it programmatically?
First, get the Screenheight for your device: How to determine the screen width in terms of dp or dip at runtime in Android?
Secondly, get your Imagview and set its height according to xx% of the determined screen height.
BR

Change the existing width of the button to at maximum 1/3 of the screen

How to change the existing width of a button programmatically to 1/3 of the width of the screen at maximum. By saying ‘at maximum’ I mean that if there will be enough space for a button to be 1/3 of the screen it should be resized. Otherwise the button should fit in the existing space gap (it can be for example 1/4 or even 1/5 of the screen’s width).
EDIT:
I'm looking for a soultion for RelativeLayout.
Use a TableLayout with three columns, see this example for more details
http://www.mkyong.com/android/android-tablelayout-example/

ImageView static height, dynamic width

Thanks to this site, I successfully implemented a Horizontal ListView of ImageViews.
Now what I need is for the ImageViews in the list to have a fixed height (they're part of a more complex layout, so I'm using the LinearLayout weight trick to give it a height equal to 1/3 of the screen height), and a width that adjusts in relation to the height without destroying aspect ratio. The images have a longer height than width - an aspect ratio similar to a phone on portrait.
Now I've tried setting the scaleType to the different available settings, also set adjustViewBounds to true, and set the layout_width to wrap_content and layout_height to fill_parent. The nearest I got to doing it was this:
http://img849.imageshack.us/img849/1289/device20120831133004.png
(i placed the white borders as image background)
What else can be done?
Problem here is that your ImageView cannot be adjusted to desired height because it's aspect ratio is different from screen. Your view matching screen height (or 1/3 of it) and width cannot be scaled proportionally without crop.
Try to set scaleType to centerCrop and load images resized to desired height.

Adapt text size depending on the size of the layout

I have 4 TextViews placed horizontally filling all the width of the screen, and each TextView with the same horizontal width . Depending on the device's screen the total width is diferent. I want to change the textsize to fill each TextView horizontally but in a manner that the text fits. Is there any way to do this?? Thx
I think that this link could help you immensly as it is very similar. You may have to SET your widths static based upon you screen width then continue to adjust your font size until the vertical direction fits for each textview.
Calculate text size according to width of text area

How to make buttons, with dynamic text, the same size and width to height ratio?

I have a few buttons I want to display in a row in a decision tree. I'm trying to get the buttons in each row of the tree to be the same size. But the buttons have different text. Some have a couple words and some have a sentence. Is there a way I can get the buttons to all be the same size and all maintain the same width to height (4:3) ratio?
I can get the heights the same using fill parent on the button heights while having the parent row layout_height set to wrap_content. How can I get the width of the buttons to be the same size while maintaining a 4:3 width to height ratio. (The width to height ratio constraint is so I don't end up with really tall and thin buttons, which would look silly)
Anyone have any suggestions?
Edit:
I just saw this link which is kinda what I am looking for: Scaling layout with invariant aspect ratio in Android
My problem with this is that it will inflate all the buttons and introduce a lot of empty space around the text inside.
If I could find a way to incrementally increase the width or height (whichever is smaller) of the button and then resize it so it re-"wraps_content", then this could work. Anyone know how to re-wrap the content?
If you are able to get the same height for all you can use android:layout_weight="1" for all buttons and put android:layout_width="0dip" so all buttons will have same width

Categories

Resources