0

I am learning Android development and I'm trying to make a simple calculator. I am having some problems with layout of the calculator. In my opinion, it would be too clunky to post the whole xml code, so I'm just going to post the snippets that matter in my opinion.

I made a vertical LinearLayout as the top-most parent, which then has 2 children, a horizontal LinearLayout, (which consists of a textView which shows your input and a button that tells to calculate) and a GridLayout, which would be 0-9 buttons and operators.

The problem is, the grid layout would be a 4x4 grid with buttons and when I want to set the First row of buttons, each button needs to get layout_height, which can't be left empty and if I set it's value to match_parent, then that button alone would fill up the whole screen.

So how can I solve this problem with layout_height, is there some workaround or would it be better to make multiple LinearLayouts for the grid? If you have any additional questions, feel free to ask so I can explain.

Here is the children LinearLayout and GridLayout xml code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_weight="1"
    android:background="@color/colorLight"
    android:orientation="horizontal">

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="8"
    android:background="@color/colorLight"
    android:>
Leonz
  • 177
  • 3
  • 14
  • can you set grid_column ? it doesnt solve your problem? – Amir Jun 11 '16 at 12:51
  • 1
    can you use `GridView` instead of `GridLayout`? – SQLiteNoob Jun 11 '16 at 12:51
  • Use **0dp** for the View's height. Or set the View visibility to **GONE**. I'd prefer the second option. – Phantômaxx Jun 11 '16 at 12:53
  • @Amir, I will try that now and see if it Works. SQLiteNoob, I need to use Grid layout for better customizability. Bob Malooga, when I set height to 0dp, the button disappears, as it would with GONE visibility. I need the button to be visible. Take for example any simple calculator on the app store. – Leonz Jun 11 '16 at 13:04
  • Would it be a good thing to make 4 LinearLayouts into a grid and add buttons to each of them? It would work, but would it be a good solution? – Leonz Jun 11 '16 at 13:07

2 Answers2

1

Sorry, I misunderstand your question. If you use API 21 or newer, you can use columnCount to get it.

Please refer to that answer or that answer

Community
  • 1
  • 1
Robust
  • 2,415
  • 2
  • 22
  • 37
  • Thx for answering. The problem is not in the children layout, I set the layout_weight for them and they fit as they should. The problem is when I add a button to grid layout, that button needs to have a layout_height value, and if I set it to match_parent, it will fill the whole screen and if I set it to 0dp, it will disappear. – Leonz Jun 11 '16 at 13:00
0

The best way to sort this out for various screen sizes is to use the weight attribute. For GridLayout, there is none, according to the documentation.

Check out this answer for ideas, such as subbing nested LinearLayouts. GridLayout (not GridView) how to stretch all children evenly

Community
  • 1
  • 1
SQLiteNoob
  • 2,958
  • 3
  • 29
  • 44