I can't set weight in GridLayout - android

I've tried enough, searched the internet and I do not know what I can do to fix this.
I have in my code this:
<GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:rowCount="2"
android:background="#f11"
>
<Button
android:id="#+id/button1"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_row="0"
android:layout_column="0"
android:text="Button"/>
<Button
android:id="#+id/button2"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_row="0"
android:layout_column="1"
android:text="Button"/>
<Button
android:id="#+id/button3"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_row="1"
android:layout_column="0"
android:text="Button"/>
<Button
android:id="#+id/button4"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_row="1"
android:layout_column="1"
android:text="Button"/>
</GridLayout>
In my IDE (ANDROID STUDIO 3.0) appear this: (Which is what I want.)
But in devices appear like this:
How fix this?
Thanks!

When android api <21 ,it didn't work well.
So we can add compile.
compile 'com.android.support:gridlayout-v7:25.3.1'
And change your xml code to this.
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f11"
app:columnCount="2"
app:rowCount="2">
<Button
android:id="#+id/button1"
android:text="Button"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/button2"
android:text="Button"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/button3"
android:text="Button"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="1"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/button4"
android:text="Button"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="1"
app:layout_rowWeight="1"/>
</android.support.v7.widget.GridLayout>
Note
In the com.android.support:gridlayout-v7:25.3.1
It had new attribute.
app:layout_columnWeight
app:layout_rowWeight
app:layout_rowSpan
app:layout_columnSpan
You can use in your code.

Instead, you can use table format and assign the buttons in there easily which will cause no problem in various screens when compiled.

Related

GridLayout look strange on a real Device and perfect on emulator

I've designed a basic calculator in android.
As i designed it, i tested it on Emulator Pixel XL Device (API 28, Screen Resolution 1440x2560 560dpi) and worked fine.
"Emulator Layout"
But when I installed the app in Huawei Honor 4C (Android 6.0) It look hilarious.
"Huawei Honor 4c"
minimumSdkVersion: 21
targetSdkVersion: 28
My Design Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffeeeeee"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffffff">
<TextView
android:id="#+id/display"
android:layout_width="match_parent"
android:layout_height="100dp"
android:ems="10"
android:textSize="30sp"
android:gravity="end|bottom"
/>
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:columnCount="4"
android:rowCount="5">
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="C" />
<Button
android:id="#+id/btnDivid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="/" />
<Button
android:id="#+id/btnMultiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="X" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="Delete" />
<Button
android:id="#+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="7"
android:text="7" />
<Button
android:id="#+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="8"
android:text="8" />
<Button
android:id="#+id/btn9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="9"
android:text="9" />
<Button
android:id="#+id/btnMinus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="-" />
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="4"
android:text="4" />
<Button
android:id="#+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="5"
android:text="5" />
<Button
android:id="#+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="6"
android:text="6" />
<Button
android:id="#+id/btnPlus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:text="+" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="1"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="2"
android:text="2" />
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="3"
android:text="3" />
<Button
android:id="#+id/btnEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_rowSpan="2"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:background="#color/colorPrimaryDark"
android:onClick="numberClicked"
android:text="=" />
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_columnWeight="1"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="0"
android:text="0" />
<Button
android:id="#+id/btnDot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="2"
android:layout_gravity="fill"
android:onClick="numberClicked"
android:tag="."
android:text="." />
</GridLayout>
</LinearLayout>
What are the possible reasons?
I think that this apparent problem is related to the attribute android:supportsRtl="true" and the different API levels of your device/emualtor.
Add in manifest in application tag
<application
android:name=".Application"
android:supportsRtl="true">
If set to true and targetSdkVersion is set to 17 or higher else false
Uninstall app in device and clear project then install to real device again.
If it works give me a thumbs up ..Keep coding :)

Grid layout not getting aligned properly

I am trying to make an app which contains a grid layout. I have to insert 4 buttons in the layout but I am facing problems regarding the alignment of these buttons.
Here is a screenshot and the xml file:
XML file:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sumTextView"
android:columnCount="2"
android:rowCount="2">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="0"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="0"
android:onClick="chooseAnswer"
android:tag="0"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="0"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="0"
android:onClick="chooseAnswer"
android:tag="1"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="0"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowWeight="0"
android:onClick="chooseAnswer"
android:tag="2"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnWeight="0"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowWeight="0"
android:onClick="chooseAnswer"
android:tag="3"
android:text="3"
android:textSize="80sp" />
</GridLayout>
Can someone tell me what the error is?
Add android:layout_columnWeight="1" and android:layout_rowWeight="1" to all cells.
Like this:
<GridLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/sumTextView"
android:columnCount="2"
android:rowCount="2"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="chooseAnswer"
android:tag="0"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="chooseAnswer"
android:tag="1"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="chooseAnswer"
android:tag="2"
android:text="3"
android:textSize="80sp" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_gravity="fill"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:onClick="chooseAnswer"
android:tag="3"
android:text="3"
android:textSize="80sp" />
</GridLayout>

How do I stretch a GridLayout to its parent?

I am trying to stretch the keyboard of my calculator (which is a GridLayout with buttons inside of it). It is a 5*4(columns*rows).
I give GridLayout android:layout_height="match_parent" and android:layout_width="match_parent" to make its dimensions fill the available space. then
I assign the column and row indices to the cells with android:layout_column an android:layout_row accordingly.
I get the following result:
1080x1920
The cells take an uniform width and height, but do not stretch enough to fill the whole grid. So I searched online and saw that I could assign an equal weight to each cell and the layout figures out a uniform width and height value that fills the whole grid.
(Side question: how does android determine the size of these cells in this case? why did it not make them larger/smaller than what it is here?)
So I add:
android:layout_weight="1" and android:layout_height="1" to each of the buttons
I get this result:
1080x1920
Awesome. Looks like it is working as intended.. until I change the resolution to something else:
480x800
Or if I keep the same 1080x1920 resolution but force the min height of the top section to something like 280dp (279dp works fine)
How can I fix this? I am intending API Level 21 and I want to force the GridLayout to fill the available space no matter. I prefer the buttons to be squeezed (even have their content clipped/ invisible) rather than to shoot off the screen. To finish here is the XML:
main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root">
<!--Top section (screen + few keys)-->
<LinearLayout
android:style="#style/CalculatorScreenStyle"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/top">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="280dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
android:hint="Enter a number here"/>
</LinearLayout>
<!-- Keyboard layout -->
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/keyboard"
android:columnCount="4"
android:rowCount="5">
<Button
android:style="#style/CalculatorButtonStyle"
android:text="C"
android:id="#+id/clearButton"
android:layout_column="0"
android:layout_row="0" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="Button"
android:id="#+id/button15"
android:layout_column="1"
android:layout_row="0" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="%"
android:layout_column="2"
android:layout_row="0"
android:id="#+id/percentButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:id="#+id/divideButton"
android:layout_column="3"
android:layout_row="0"
android:text="÷" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="7"
android:layout_column="0"
android:layout_row="1"
android:id="#+id/sevenNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="8"
android:layout_column="1"
android:layout_row="1"
android:id="#+id/eightNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="9"
android:layout_column="2"
android:layout_row="1"
android:id="#+id/nineNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="+"
android:layout_column="3"
android:layout_row="1"
android:id="#+id/plusButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="4"
android:layout_column="0"
android:layout_row="2"
android:id="#+id/fourNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="5"
android:layout_column="1"
android:layout_row="2"
android:id="#+id/fiveNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="6"
android:layout_column="2"
android:layout_row="2"
android:id="#+id/sixNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="x"
android:layout_column="3"
android:layout_row="2"
android:id="#+id/multiplyButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="1"
android:layout_column="0"
android:layout_row="3"
android:id="#+id/oneNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="2"
android:layout_column="1"
android:layout_row="3"
android:id="#+id/twoNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="3"
android:layout_column="2"
android:layout_row="3"
android:id="#+id/threeNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="-"
android:layout_column="3"
android:layout_row="3"
android:id="#+id/minusButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="+/-"
android:layout_column="0"
android:layout_row="4"
android:id="#+id/signButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="0"
android:layout_column="1"
android:layout_row="4"
android:id="#+id/zeroNumButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="."
android:layout_column="2"
android:layout_row="4"
android:id="#+id/periodButton" />
<Button
android:style="#style/CalculatorButtonStyle"
android:text="="
android:layout_column="3"
android:layout_row="4"
android:id="#+id/equalsButton" />
</GridLayout>
</LinearLayout>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CalculatorButtonStyle" >
<item name="android:textSize">22sp</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:background">#9E5EC2</item>
</style>
<style name="CalculatorScreenStyle">
<item name="android:background">#ED59E8</item>
</style>
</resources>
EDIT: As a temporary solution, I am using a vertical LinearLayout of horizontals LinearLayouts and I assing an equal weights to all the Buttons. It works fine but then I wonder what is the use of GridLayout if I have to emulate how it works myself..
When android API level < 21, it doesn't work well.
So we can add dependency in app gradle file:
implementation 'androidx.gridlayout:gridlayout:1.0.0'
If you've still not migrated to androidx (which you ought to), then use:
implementation 'com.android.support:gridlayout-v7:28.0.0'
Then change your code.
Use androidx.gridlayout.widget.GridLayout (or android.support.v7.widget.GridLayout, if not androidx) in your XML code.
<androidx.gridlayout.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="4"
app:rowCount="5">
<Button
android:id="#+id/clearButton"
style="#style/CalculatorButtonStyle"
android:text="C"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/button15"
style="#style/CalculatorButtonStyle"
android:text="Button"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/percentButton"
style="#style/CalculatorButtonStyle"
android:text="%"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/divideButton"
style="#style/CalculatorButtonStyle"
android:text="÷"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="0"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/sevenNumButton"
style="#style/CalculatorButtonStyle"
android:text="7"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/eightNumButton"
style="#style/CalculatorButtonStyle"
android:text="8"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/nineNumButton"
style="#style/CalculatorButtonStyle"
android:text="9"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/plusButton"
style="#style/CalculatorButtonStyle"
android:text="+"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="1"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/fourNumButton"
style="#style/CalculatorButtonStyle"
android:text="4"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/fiveNumButton"
style="#style/CalculatorButtonStyle"
android:text="5"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/sixNumButton"
style="#style/CalculatorButtonStyle"
android:text="6"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/multiplyButton"
style="#style/CalculatorButtonStyle"
android:text="x"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="2"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/oneNumButton"
style="#style/CalculatorButtonStyle"
android:text="1"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/twoNumButton"
style="#style/CalculatorButtonStyle"
android:text="2"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/threeNumButton"
style="#style/CalculatorButtonStyle"
android:text="3"
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/minusButton"
style="#style/CalculatorButtonStyle"
android:text="-"
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="3"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/signButton"
style="#style/CalculatorButtonStyle"
android:text="+/-"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/zeroNumButton"
style="#style/CalculatorButtonStyle"
android:text="0"
app:layout_column="1"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/periodButton"
style="#style/CalculatorButtonStyle"
android:text="."
app:layout_column="2"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1"/>
<Button
android:id="#+id/equalsButton"
style="#style/CalculatorButtonStyle"
android:text="="
app:layout_column="3"
app:layout_columnWeight="1"
app:layout_row="4"
app:layout_rowWeight="1"/>
</androidx.gridlayout.v7.widget.GridLayout>
Note
The new attributes are:
app:layout_columnWeight
app:layout_rowWeight
app:layout_rowSpan
app:layout_columnSpan

GridLayout is showing only one column on some DPI

So I have a GridLayout - it's using weight to distribute itself properly. The problem is that it doesn't display correctly on some devices - for example on Xperia E3 (480 x 854, 218 DPI) or 3.7" FWVGA slider in the designer.
Here's how it should always look like:
Here's how it looks like on some devices (only first column of GridLayout is visible and it gets stretched even beyond the right side of the screen):
Here's the XML, basically GridLayout contains buttons and the main thing here is that each button has
android:layout_columnWeight="1"
android:layout_rowWeight="1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3">
...
</RelativeLayout>
<!--Bottom part (buttons)-->
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:orientation="vertical">
<!--Scroll bar with all the different functions and constants-->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:layout_marginBottom="5dp">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawHorizontalTrack="false">
...
</GridLayout>
</HorizontalScrollView>
<!--THIS IS THE PROBLEM Main buttons for the calculator THIS IS THE PROBLEM-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.9">
<GridLayout
android:id="#+id/mainBtnGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="5"
android:columnCount="5"
android:orientation="vertical">
<Button
android:id="#+id/btnClear"
android:background="#drawable/button_main"
style="#style/button_main_calculator"
android:text="C"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnSeven"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="7"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnFour"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="4"
android:layout_columnWeight="1"
android:layout_rowWeight="1"/>
<Button
android:id="#+id/btnOne"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnSign"
android:background="#drawable/button_main"
style="#style/button_main_calculator"
android:text="±"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnBack"
android:background="#drawable/button_main"
style="#style/button_main_calculator"
android:text="B"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnEight"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="8"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnFive"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="5"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnTwo"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnZero"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnTypingPositionSwitch"
android:background="#drawable/button_main"
style="#style/button_main_calculator"
android:text="M"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnNine"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="9"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnSix"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="6"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnThree"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnDecimal"
android:background="#drawable/button_main"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="."
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Space
android:layout_width="5dp" />
<Space
android:layout_width="5dp" />
<Space
android:layout_width="5dp" />
<Space
android:layout_width="5dp" />
<Space
android:layout_width="5dp" />
<Button
android:id="#+id/btnDivide"
android:background="#drawable/button_operator"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="/"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnMultiply"
android:background="#drawable/button_operator"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="x"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnSubtract"
android:background="#drawable/button_operator"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="-"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnAdd"
android:background="#drawable/button_operator"
android:onClick="Button_OnClick"
style="#style/button_main_calculator"
android:text="+"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="#+id/btnEquals"
android:background="#drawable/button_operator"
style="#style/button_main_calculator"
android:text="="
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
</GridLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I have no idea what to do with this, I mean, it's not happening constantly! Thank you for reading this, I will greatly appreciate any suggestions.
I've finally found a solution to this problem.
Turns out that you just need to specify
:layout_width="0dp"
for each button. Then it looks as it's supposed to on all the different screen resolutions and sizes.
I suspect this happens because on smaller screens the grid layout was overflowing even before I've specified
android:layout_columnWeight="1"
android:layout_rowWeight="1"
*Note: for GridLayout v7+ you should use app instead of android as follows:
app:layout_columnWeight="1"
app:layout_rowWeight="1"
so it "didn't know how far it should stretch".
However, now when the width is set to 0dp, it's no longer overflowing anywhere and that's why adding weights now works flawlessly.
I hope this helps some of you - this was a really frustrating problem to have!

How to use xml attributes from higher api on lower api?

I want to use xml attributes from higher api on lower android system. How to do that without changing much? like android:layout_columnWeight and android:layout_rowWeigh.
Here is my xml code:
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="Hello"
android:id="#+id/hello"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="How are you"
android:id="#+id/howareyou"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="good evening"
android:id="#+id/goodevening"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="please"
android:id="#+id/please"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="my name is"
android:id="#+id/mynameis"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="do you
speak english"
android:id="#+id/doyouspeakenglish"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="welcome"
android:id="#+id/welcome"
android:onClick="buttonTapped"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_gravity="fill"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:text="i live in"
android:id="#+id/ilivein"
android:onClick="buttonTapped"/>
please don't say to use another method to do that, gridLayout is very useful but I can't use on lower android systems.
In this case, you can use android.support.v7.widget.GridLayout from the Android Support Library in place of the standard GridLayout.
Just switch to the GridLayout from the support library.
That way you get to use your layout_columnWeight on older APIs, but replace the android namespace prefix with app.
Reference
The only other alternative is to use an alternative layout file inside your layout-v21 directory or something.

Categories

Resources