GridLayout look strange on a real Device and perfect on emulator - android

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 :)

Related

How to make buttons in grid-view horizontally scrollable?

Very new to android studio. Trying to make a calculator app in which I can scroll buttons horizontally to get buttons for scientific calculations but it is not working. Here is my xml code.
Then I have added this to activity main but my buttons get disappeared from the screen. Tried changing it a few times changing width and height but nothing seems to be working
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="2dp"
android:columnCount="4"
android:rowCount="5">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_clr"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="C"
android:textColor="#F75D68"
/>
<Button
android:id="#+id/btn_bracket"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="( )" />
<Button
android:id="#+id/btn_prcnt"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="%" />
<Button
android:id="#+id/divide"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:text="รท" />
<Button
android:id="#+id/multiply"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:text="X" />
<Button
android:id="#+id/minus"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:text="-"/>
<Button
android:id="#+id/plus"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:text="+" />
<Button
android:id="#+id/equal"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="3"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="3dp"
android:layout_marginBottom="2dp"
android:background="#2B90FA"
android:text="="
android:textColor="#ffffff" />
<Button
android:id="#+id/one"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="1"
android:textColor="#333333"
/>
<Button
android:id="#+id/two"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="2"
android:textColor="#010F14" />
<Button
android:id="#+id/three"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="3"
android:textColor="#333333" />
<Button
android:id="#+id/four"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="4"
android:textColor="#333333" />
<Button
android:id="#+id/five"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="5"
android:textColor="#333333" />
<Button
android:id="#+id/six"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="6"
android:textColor="#333333" />
<Button
android:id="#+id/seven"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="7"
android:textColor="#333333" />
<Button
android:id="#+id/eight"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="8"
android:textColor="#333333" />
<Button
android:id="#+id/nine"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:text="9"
android:textColor="#333333" />
<Button
android:id="#+id/d_zero"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:text="00"
android:textColor="#333333" />
<Button
android:id="#+id/zero"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:text="0"
android:textColor="#333333" />
<Button
android:id="#+id/pt"
style="#style/Buttons"
android:layout_width="0dp"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:text="."
android:textColor="#333333" />
</HorizontalScrollView>
</GridLayout>
You can use Two-way GridView
look for this

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>

Positioned Grid View pictures dont appear in cell

I made a mini game with images. I want to use the grid layout margin as board to keep dynamic the size. Now i have a problem with the pictures when I appear them they appear in full size and not in the grid cell.
Here is my activity main xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.example.codes.logicgarden.MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:animateLayoutChanges="false"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<ImageView
android:id="#+id/im00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:adjustViewBounds="true"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="0" />
<ImageView
android:id="#+id/im01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:adjustViewBounds="true"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="1" />
<ImageView
android:id="#+id/im02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:adjustViewBounds="true"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="2" />
<ImageView
android:id="#+id/im03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="3" />
<ImageView
android:id="#+id/im04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="0"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="4" />
<ImageView
android:id="#+id/im10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="5" />
<ImageView
android:id="#+id/im11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="6" />
<ImageView
android:id="#+id/im12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="7" />
<ImageView
android:id="#+id/im13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="8" />
<ImageView
android:id="#+id/im14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="1"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="9" />
<ImageView
android:id="#+id/im20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="10" />
<ImageView
android:id="#+id/im21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="11" />
<ImageView
android:id="#+id/im22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="12" />
<ImageView
android:id="#+id/im23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="2"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="13" />
<ImageView
android:id="#+id/im24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="14" />
<ImageView
android:id="#+id/im30"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="15" />
<ImageView
android:id="#+id/im31"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="16" />
<ImageView
android:id="#+id/im32"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="17" />
<ImageView
android:id="#+id/im33"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="18" />
<ImageView
android:id="#+id/im34"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="3"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="19" />
<ImageView
android:id="#+id/im40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="20" />
<ImageView
android:id="#+id/im41"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="21" />
<ImageView
android:id="#+id/im42"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="22" />
<ImageView
android:id="#+id/im43"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="23" />
<ImageView
android:id="#+id/im44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="4"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="24" />
<ImageView
android:id="#+id/im50"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="25" />
<ImageView
android:id="#+id/im51"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="26" />
<ImageView
android:id="#+id/im52"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="27" />
<ImageView
android:id="#+id/im53"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="28" />
<ImageView
android:id="#+id/im54"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="5"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="29" />
<ImageView
android:id="#+id/im60"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="30" />
<ImageView
android:id="#+id/im61"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="31" />
<ImageView
android:id="#+id/im62"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="32" />
<ImageView
android:id="#+id/im63"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_row="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="33" />
<ImageView
android:id="#+id/im64"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="4"
android:layout_row="6"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:background="#android:color/background_light"
android:onClick="dropIn"
android:tag="34" />
</GridLayout>
<RelativeLayout
android:id="#+id/playAgainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/gridLayout"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="100dp"
android:background="#android:color/holo_green_dark"
android:visibility="invisible">
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="204dp"
android:onClick="playAgain"
android:text="Play Again" />
</RelativeLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
How can i make when i click on the images they will appear in the grid view cell where they are assigned like in the editor and not original size?
The program working when the images have fix size but i want to make it dynamic to all platform.
How should look
How appear in emulator
You should really use a gridView with a custom adapter. than you set the adapter with a click listener and thats it.
Because if you want to change something in the image you will only need to do it once and not 32 times.
here is a really nice tutorial that can get you started with https://www.raywenderlich.com/127544/android-gridview-getting-started

remove extra space between grid components

I am trying to make a calculator app and i used grid for the buttons but when i do that by default there are some margins around the buttons even though i didn't specify one.
yellow part is the grid layout.
Now i tried this method given here. This doesn't seem to work, anyone?
P.s: i am new to android.
Code
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff0000"
android:columnCount="3"
android:rowCount="5"
android:layout_weight="1"
android:useDefaultMargins="false"
android:alignmentMode="alignMargins"
android:rowOrderPreserved="false"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:text="Button"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="100dp" />
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:text="Button"
tools:layout_editor_absoluteX="116dp"
tools:layout_editor_absoluteY="289dp" />
<Button
android:id="#+id/button6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:text="Button"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="255dp" />
<Button
android:id="#+id/button8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:text="Button"
tools:layout_editor_absoluteX="64dp"
tools:layout_editor_absoluteY="259dp" />
<Button
android:id="#+id/button9"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button10"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:text="Button"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="217dp" />
<Button
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button13"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="2"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button18"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="3"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button20"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button21"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="1"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:text="Button" />
<Button
android:id="#+id/button22"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_column="2"
android:layout_columnWeight="1"
android:layout_row="4"
android:layout_rowWeight="1"
android:text="Button" />
</GridLayout>

XML Error "Grid Layout is Useless" (Android Studio)

I'm currently taking an Android Development course in Udemy, and I'm currently learning how to build a basic phrases app, which consists of a Grid Layout. I followed exactly what the instructor did with the XML part, and I got an error that the instructor didn't get:
This GridLayout layout or its RelativeLayout parent is useless. A layout with children that has no siblings, is not a scrollview or a root layout, and does not have a background, can be removed and have its children moved directly into the parent for a flatter and more efficient layout hierarchy.
This is the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.jumin.basicphrases.MainActivity">
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Hello"
android:onClick="buttonTapped"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="How are you?"
android:onClick="buttonTapped"
android:id="#+id/howareyou" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Good Evening"
android:onClick="buttonTapped"
android:id="#+id/goodevening" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Please"
android:onClick="buttonTapped"
android:id="#+id/please" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="My name is..."
android:onClick="buttonTapped"
android:id="#+id/mynameis" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Do you
speak English?"
android:onClick="buttonTapped"
android:id="#+id/doyouspeakenglish" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Welcome"
android:onClick="buttonTapped"
android:id="#+id/welcome" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="I live in..."
android:onClick="buttonTapped"
android:id="#+id/ilivein" />
</GridLayout>
</RelativeLayout>
The error is very clear: Just remove the RelativeLayout and try again:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Hello"
android:onClick="buttonTapped"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="How are you?"
android:onClick="buttonTapped"
android:id="#+id/howareyou" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Good Evening"
android:onClick="buttonTapped"
android:id="#+id/goodevening" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Please"
android:onClick="buttonTapped"
android:id="#+id/please" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="My name is..."
android:onClick="buttonTapped"
android:id="#+id/mynameis" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Do you
speak English?"
android:onClick="buttonTapped"
android:id="#+id/doyouspeakenglish" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Welcome"
android:onClick="buttonTapped"
android:id="#+id/welcome" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="I live in..."
android:onClick="buttonTapped"
android:id="#+id/ilivein" />
</GridLayout>
This warning is because your RelativeLayout contains only GridLayout and both (recyclerView and GridLayout) are set to match_parent
You can remove RelativeLayout and set GridLayout as root layout and it will look exactly like now but better for performance.
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.jumin.basicphrases.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Hello"
android:onClick="buttonTapped"
android:id="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="0"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="How are you?"
android:onClick="buttonTapped"
android:id="#+id/howareyou" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Good Evening"
android:onClick="buttonTapped"
android:id="#+id/goodevening" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="1"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Please"
android:onClick="buttonTapped"
android:id="#+id/please" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="My name is..."
android:onClick="buttonTapped"
android:id="#+id/mynameis" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="2"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Do you
speak English?"
android:onClick="buttonTapped"
android:id="#+id/doyouspeakenglish" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="Welcome"
android:onClick="buttonTapped"
android:id="#+id/welcome" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="3"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_gravity="fill"
android:text="I live in..."
android:onClick="buttonTapped"
android:id="#+id/ilivein" />
</GridLayout>

Categories

Resources