1

I am using Layout_Weight to set the width of my buttons to 40% of my screen width. Works great! But now I can't really do much with the height.

Ideally I'd like to set a percentage for the height as well, but to my knowledge I can only set the layout_width percentage to one dimension.

To adjust the height of my buttons so they don't look like little slivers on tablets, I tried using padding, tried using \n. The buttons with two lines of text are unequal to the single line text when switching to tablet, but are equal on phones.

What's the best way to adjust the height to be dynamic in this way like the width?

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="fill_parent">

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="This takes up two lines"
                android:id="@+id/btnDoubleLineExample"
                android:layout_weight="40"
                android:paddingTop="80dp"
                android:paddingBottom="80dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textSize="20sp"
                />

            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="One Line\n"
                android:id="@+id/btnOneLine"
                android:layout_weight="40"
                android:paddingTop="80dp"
                android:paddingBottom="80dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textSize="20sp"/>
        </LinearLayout>

Thank you for your time.

BR89
  • 716
  • 1
  • 6
  • 23

1 Answers1

1

I think you use badly layout_weight. Take a look at this question for a better understanding.

You have to consider that layout_weight is only applicable on LinearLayout and describe only one orientation (LinearLayout orientation)

In order to set a percentage on height you have to set a layout_weight to the LinearLayout. But it's really not recommended due to calculation of views witdh and height.

You can take a look to PercentFrameLayout or PercentRelativeLayout it can be help you.

Community
  • 1
  • 1
Lionel Briand
  • 1,732
  • 2
  • 13
  • 21
  • Thank you for the tip. I've never implemented a subclass of a layout before. How to I declare the subclass rather than just relativelayout? – BR89 Jul 01 '16 at 21:21
  • Figured out how to add it to the gradle :) Great resource here - https://inthecheesefactory.com/blog/know-percent-support-library/en – BR89 Jul 01 '16 at 21:35
  • @BR89 Glad to help you :) – Lionel Briand Jul 01 '16 at 21:41
  • Getting a strange error now when I try to render the layout with a child view- Exception raised during rendering: android/support/percent/R$styleable – BR89 Jul 01 '16 at 21:41
  • Search into stackoverflow before [look at this answer](http://stackoverflow.com/questions/22526925/java-lang-noclassdeffounderror-android-support-v7-appcompat-rstyleable) – Lionel Briand Jul 01 '16 at 21:45
  • Downloaded everything that wasn't wear or TV or hardware acceleration for my current API (24). Upon restart Android studio recognizes it as a Custom View. Can't do anything in design mode, but text mode should work like a charm now. You sir are fantastic - thanks :) – BR89 Jul 01 '16 at 21:53