based on the answer on this question Enforcing Input Method of Edit Text i have read the it is not possible to use a specific key board for an edit text but is it possible to cancel the use keyboard or to disable pop up keyboard when the user clicks on an edit text. i have this edit text and i want to enter numeric values and i have a custom made numeric keypad pad below the edit i put it in the numbers 0 - 9 in a button so that when i click on the edit text box the cursor will appear on the text and then i have to click on the button to input the value of that specific button on the edit text. I've been looking for solution for some time now but i can seem to find the solution.
xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical" >
<LinearLayout
android:layout_margin="15dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/login"
android:orientation="vertical" >
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/previousscore_lbl_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/labeltextbox"
android:textColor="#000000"
android:gravity="center"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="none"
android:text="meter" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".20"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/previousscore_txt_score"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextbox"
android:textColor="#000000"
android:gravity="center"
android:inputType="number"
android:text="#string/one" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".30"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/previousscore_lbl_putt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/labeltextbox"
android:textColor="#000000"
android:gravity="center"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="none"
android:text="inches" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight=".20"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/previousscore_txt_putt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextbox"
android:textColor="#000000"
android:gravity="center"
android:inputType="number"
android:text="#string/one" />
</LinearLayout>
</LinearLayout>
<GridLayout
android:id="#+id/numerickeypad"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:columnCount="10"
android:rowCount="9" >
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/one" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/two" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/three" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/four" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/five" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/six" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/seven" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/eight" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/nine" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/zero" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="#string/dialogc" />
<Space
android:layout_width="32dp"
android:layout_column="0"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="1"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="2"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="3"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="4"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="5"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="6"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="7"
android:layout_row="8" />
<Space
android:layout_width="32dp"
android:layout_column="8"
android:layout_row="8" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="0" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="1" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="2" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="3" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="4" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="5" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="6" />
<Space
android:layout_height="32dp"
android:layout_column="9"
android:layout_row="7" />
</GridLayout>
<LinearLayout
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/login">
<Button
android:id="#+id/dialogbox_buttoncancel"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:background="#drawable/selector"
android:text="#string/cancel" />
<Button
android:id="#+id/dialogbox_buttonsave"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:background="#drawable/selector"
android:text="#string/save" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
If you'd like to use an EditText, try android:editable="false".
A better approach might be to use a TextView component. With that, you could use setText() each time a user presses your button(s). This approach assumes you don't want your users editing that field directly.
I understand you want to disbale the clicking event on EditText and popping up of system keyboard.
If so, Try this!
yourTextView.setInputType(0);
Related
I am trying to make an android Calculator app but however i am stuck on one thing, there is some extra space coming up while using grid layout.
Now i know i have used match parent value for the layout height and width which might be the reason for the extra spacing in the image.
Since , i am new to android and i am just learning more and more of android, could anyone let me know , how can i evenly distribute those buttons on the grid layout?
Code
<?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:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.tilak.myfirstapplication.MainActivity">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:height="225dp"
android:ems="10"
android:layout_gravity="fill_horizontal"
android:inputType="textMultiLine"
tools:layout_editor_absoluteX="104dp"
tools:layout_editor_absoluteY="207dp" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="5"
android:columnCount="4"
android:background="#ff0000"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="0"
android:layout_column="0"
tools:layout_editor_absoluteX="136dp"
tools:layout_editor_absoluteY="100dp" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="0"
android:layout_column="1"
tools:layout_editor_absoluteX="116dp"
tools:layout_editor_absoluteY="289dp" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="0"
android:layout_column="2"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="255dp" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="0"
android:layout_column="3"
tools:layout_editor_absoluteX="41dp"
tools:layout_editor_absoluteY="180dp" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="1"
android:layout_column="0"
tools:layout_editor_absoluteX="64dp"
tools:layout_editor_absoluteY="259dp" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:text="Button" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_row="1"
android:layout_column="2"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="217dp" />
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="3"
android:text="Button"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="168dp" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="0"
android:text="Button" />
<Button
android:id="#+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="1"
android:text="Button" />
<Button
android:id="#+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="2"
android:text="Button" />
<Button
android:id="#+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="2"
android:layout_column="3"
android:text="Button" />
<Button
android:id="#+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="0"
android:text="Button" />
<Button
android:id="#+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="1"
android:text="Button" />
<Button
android:id="#+id/button18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="2"
android:text="Button" />
<Button
android:id="#+id/button19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="3"
android:layout_column="3"
android:text="Button" />
<Button
android:id="#+id/button20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="0"
android:text="Button" />
<Button
android:id="#+id/button21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="1"
android:text="Button" />
<Button
android:id="#+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="2"
android:text="Button" />
<Button
android:id="#+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_row="4"
android:layout_column="3"
android:text="Button" />
</GridLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
I suggest using the weight attribute to make the child views share the space equally. There's another post here on StackOverflow with a great answer that covers the subject:
https://stackoverflow.com/a/30245753/2281718
In short, just set the width to 0dp and add the android:layout_columnWeight="1" for all children
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
...
/>
Use place holder like this.
This is not exact XML.
<button 1/>
<textview weight=1>
<button 2/>
<textview weight=1>
<button 3/>
<textview weight=1>
<button 4/>
I am having problems with a GridLayout. In the layout below, phone_image and voip_image overlap and I don't want them to. phone_image is in column 0 and voip_image is in column 2.
SSCCE on Github
https://github.com/emnrd-ito/LayoutDemo
<HorizontalScrollView
android:id="#+id/container_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<GridLayout xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/individual_gridlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:alignmentMode="alignBounds"
android:columnCount="4"
android:columnOrderPreserved="false"
android:rowCount="7">
<ImageView
android:id="#+id/division_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_margin="10dp"
android:layout_row="0"
android:layout_rowSpan="3"
android:background="#null"
android:layout_gravity="center_horizontal"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/employee_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="0"
android:text="Nobody Lastname"
tools:text="name" />
<TextView
android:id="#+id/division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="1"
android:text="My Division"
tools:text="division" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="2"
android:text="My Title"
tools:text="position" />
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="3"
android:text="123 Sesame St."
tools:text="address" />
<TextView
android:id="#+id/city_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="4"
android:text="mycity, mystate"
tools:text="city,state" />
<TextView
android:id="#+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_margin="10dp"
android:layout_row="5"
android:text="(555) 555-5555"
tools:text="phone" />
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="3"
android:layout_margin="10dp"
android:layout_row="6"
android:text="someone#somewhere.com"
tools:text="email" />
<ImageButton
android:id="#+id/directions_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="4"
android:background="#null"
android:src="#drawable/ic_directions_enabled" />
<ImageButton
android:id="#+id/phone_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:background="#null"
android:src="#drawable/ic_phone_enabled" />
<ImageButton
android:id="#+id/voip_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="5"
android:background="#null"
android:src="#drawable/ic_voip_enabled" />
<ImageButton
android:id="#+id/email_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="6"
android:background="#null"
android:src="#drawable/ic_email_enabled" />
</GridLayout>
</HorizontalScrollView>
Edit:
I tried using Space like so:
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_column="1"
android:layout_row="5"/>
It seems to insert about 1/2 column, whereas using the blank image inserts a whole column. There may be parameters to play within the Space component though.
You can try to give android:maxWidth property to both voipImage and phoneImage ImageButtons, like this:
<ImageButton
android:id="#+id/phone_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:maxWidth="96dp"
android:background="#null"
android:src="#drawable/ic_phone_enabled" />
<ImageButton
android:id="#+id/voip_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="5"
android:maxWidth="96dp"
android:background="#null"
android:src="#drawable/ic_voip_enabled" />
Update
The above changes are not necessary. I think that to solve your problem you can simply change the layout_column property of voip_image like this:
android:layout_column="1"
Hope this can help
One solution is to create a blank image and put it in the drawable folder.
Then use it to take up space in the layout like so:
<ImageButton
android:id="#+id/phone_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:background="#null"
android:src="#drawable/ic_phone_enabled" />
<ImageButton
android:id="#+id/blank_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_row="5"
android:background="#null"
android:src="#drawable/ic_blank" />
<ImageButton
android:id="#+id/voip_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="5"
android:background="#null"
android:src="#drawable/ic_voip_enabled" />
I have the following xml file to define my layout for an activity in android:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="10"
android:background="#cccccc"
>
<TextView
android:text="#string/new_title"
android:textSize="32dip"
android:layout_columnSpan="10"
android:layout_gravity="center_horizontal"
android:id="#+id/textTitleEdit"
android:layout_row="0"
android:layout_column="0" />
<TextView
android:text="#string/new_descr"
android:textSize="16dip"
android:layout_columnSpan="8"
android:layout_gravity="left"
android:id="#+id/textSubTitleEdit"
android:layout_row="1"
android:layout_column="0" />
<TextView
android:text="#string/new_name"
android:layout_gravity="right"
android:layout_row="2"
android:layout_column="0"
/>
<EditText
android:ems="7"
android:singleLine="true"
android:inputType="textCapWords"
android:id="#+id/new_name"
android:layout_row="2"
android:layout_column="1" />
<ImageButton
android:src="#mipmap/ic_backspace_black_24dp"
android:id="#+id/clear_line"
android:layout_row="2"
android:minHeight="15dp"
android:minWidth="15dp"
android:maxHeight="15dp"
android:maxWidth="15dp"
/>
<ImageButton
android:src="#mipmap/ic_search_black_24dp"
android:id="#+id/new_search"
android:layout_row="2"
android:minHeight="15dp"
android:minWidth="15dp"
android:maxHeight="15dp"
android:maxWidth="15dp"
/>
_____________
<TextView
android:text="#string/new_label"
android:layout_gravity="right"
android:layout_row="3"
android:layout_column="0" />
<EditText
android:ems="7"
android:singleLine="true"
android:inputType="textCapWords"
android:id="#+id/new_label"
android:layout_row="3"
android:layout_column="1" />
______
<TextView
android:layout_column="0"
android:text="#string/new_price"
android:layout_gravity="right"
/>
<EditText
android:ems="7"
android:singleLine="true"
android:id="#+id/new_price"
android:inputType="numberDecimal"
android:layout_row="4"
android:layout_column="1" />
<TextView
android:layout_column="0"
android:text="#string/new_offset"
android:layout_gravity="right"
android:layout_row="6" />
<EditText
android:ems="7"
android:singleLine="true"
android:id="#+id/new_offset"
android:inputType="number"
android:layout_row="6"
android:layout_column="1" />
<SeekBar
android:layout_row="7"
android:layout_width="150dp"
android:max="10"
android:id="#+id/seek_offset"
android:layout_column="1"
style="#android:style/Widget.Holo.SeekBar"/>
<Space
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill"
/>
<Button
android:text="Cancel"
android:id="#+id/new_cancel"
android:background="#color/colorPrimaryDark"
android:layout_row="10"
android:layout_column="0"
/>
<Button
android:text="Ok"
android:id="#+id/new_ok"
android:background="#color/colorPrimaryLight"
android:layout_row="10"
android:layout_column="8"
/>
</GridLayout>
This works fine, and I see something like in the following image (although I might have defined nonesense in this xml, or something incorrect, as I am a beginning beginner in Android...)
However, I am reusing this layout in some other activity and replace the text of the textView with the id textSubTitleEdit by a shorter text. As a consequence, the Ok-Button is shifted to the left, and is no longer at its place in the lower right corner.
Is there something wrong with my layout? How can the length of a TextView affect the position of a button?
The problem here is that you are using a GridLayout to place all your views in the layout.
If you want your buttons to stick to the bottom, one on each side of the screen, you may consider adding a RelativeLayout above your GridLayout, and move your button there as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
>
<GridLayout
android:id="#+id/grid"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="#+id/new_cancel"
android:alignmentMode="alignBounds"
android:columnCount="10"
android:columnOrderPreserved="false"
android:useDefaultMargins="true">
<TextView
android:id="#+id/textTitleEdit"
android:layout_column="0"
android:layout_columnSpan="10"
android:layout_gravity="center_horizontal"
android:layout_row="0"
android:text="#string/new_title"
android:textSize="32dip"/>
....
<SeekBar
android:id="#+id/seek_offset"
style="#android:style/Widget.Holo.SeekBar"
android:layout_width="150dp"
android:layout_column="1"
android:layout_row="7"
android:max="10"/>
<Space
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_row="4"
/>
</GridLayout>
<Button
android:id="#+id/new_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/colorPrimaryDark"
android:text="Cancel"
/>
<Button
android:id="#+id/new_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="#color/colorPrimaryLight"
android:text="Ok"
/>
</RelativeLayout>
The Android Developers Blog post introducing GridLayout shows this diagram of how spans impact automatic index allocation:
I am attempting to actually implement that using a GridLayout. Here is what I have so far:
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.commonsware.android.gridlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:orientation="horizontal"
app:columnCount="8">
<Button
app:layout_columnSpan="2"
app:layout_rowSpan="2"
android:layout_gravity="fill_horizontal"
android:text="#string/string_1"/>
<Button
app:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="#string/string_2"/>
<Button
app:layout_rowSpan="4"
android:text="#string/string_3"/>
<Button
app:layout_columnSpan="3"
app:layout_rowSpan="2"
android:layout_gravity="fill_horizontal"
android:text="#string/string_4"/>
<Button
app:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:text="#string/string_5"/>
<Button
app:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="#string/string_6"/>
<android.support.v7.widget.Space
app:layout_column="0"
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
<android.support.v7.widget.Space
android:layout_width="36dp"
/>
</android.support.v7.widget.GridLayout>
I had to introduce the <Space> elements to ensure each column had a minimum width, otherwise, I would have a bunch of zero-width columns.
However, even with them, I get this:
Notably:
Despite android:layout_gravity="fill_horizontal", my widgets with column spans do not fill the spanned columns
Despite the android:layout_rowSpan values, nothing spans rows
Can anyone reproduce the diagram from the blog post using a GridLayout?
Thanks!
It feels pretty hacky, but I managed to get the correct look by adding an extra column and row beyond what is needed. Then I filled the extra column with a Space in each row defining a height and filled the extra row with a Space in each col defining a width. For extra flexibility, I imagine these Space sizes could be set in code to provide something similar to weights. I tried to add a screenshot, but I do not have the reputation necessary.
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnCount="9"
android:orientation="horizontal"
android:rowCount="8" >
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="1" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="2" />
<Button
android:layout_gravity="fill_vertical"
android:layout_rowSpan="4"
android:text="3" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_rowSpan="2"
android:text="4" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill_horizontal"
android:text="5" />
<Button
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:text="6" />
<Space
android:layout_width="36dp"
android:layout_column="0"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="1"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="2"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="3"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="4"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="5"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="6"
android:layout_row="7" />
<Space
android:layout_width="36dp"
android:layout_column="7"
android:layout_row="7" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="0" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="1" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="2" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="3" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="4" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="5" />
<Space
android:layout_height="36dp"
android:layout_column="8"
android:layout_row="6" />
</GridLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:columnCount="8"
android:rowCount="7" >
<TextView
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_columnSpan="2"
android:layout_rowSpan="2"
android:background="#a30000"
android:gravity="center"
android:text="1"
android:textColor="#android:color/white"
android:textSize="20dip" />
<TextView
android:layout_width="50dip"
android:layout_height="25dip"
android:layout_columnSpan="2"
android:layout_rowSpan="1"
android:background="#0c00a3"
android:gravity="center"
android:text="2"
android:textColor="#android:color/white"
android:textSize="20dip" />
<TextView
android:layout_width="25dip"
android:layout_height="100dip"
android:layout_columnSpan="1"
android:layout_rowSpan="4"
android:background="#00a313"
android:gravity="center"
android:text="3"
android:textColor="#android:color/white"
android:textSize="20dip" />
<TextView
android:layout_width="75dip"
android:layout_height="50dip"
android:layout_columnSpan="3"
android:layout_rowSpan="2"
android:background="#a29100"
android:gravity="center"
android:text="4"
android:textColor="#android:color/white"
android:textSize="20dip" />
<TextView
android:layout_width="75dip"
android:layout_height="25dip"
android:layout_columnSpan="3"
android:layout_rowSpan="1"
android:background="#a500ab"
android:gravity="center"
android:text="5"
android:textColor="#android:color/white"
android:textSize="20dip" />
<TextView
android:layout_width="50dip"
android:layout_height="25dip"
android:layout_columnSpan="2"
android:layout_rowSpan="1"
android:background="#00a9ab"
android:gravity="center"
android:text="6"
android:textColor="#android:color/white"
android:textSize="20dip" />
</GridLayout>
</RelativeLayout>
You have to set both layout_gravity and layout_columntWeight on your columns
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="سوم شخص"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
/>
<TextView android:text="دوم شخص"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
/>
<TextView android:text="اول شخص"
app:layout_gravity="fill_horizontal"
app:layout_columnWeight="1"
/>
</android.support.v7.widget.GridLayout>
Android Support V7 GridLayout library makes excess space distribution easy by accommodating the principle of weight. To make a column stretch, make sure the components inside it define a weight or a gravity. To prevent a column from stretching, ensure that one of the components in the column does not define a weight or a gravity. Remember to add dependency for this library. Add com.android.support:gridlayout-v7:25.0.1 in build.gradle.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:columnCount="2"
app:rowCount="2">
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="First"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="Second"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="Third"
app:layout_columnWeight="1"
app:layout_rowWeight="1" />
<TextView
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
app:layout_columnWeight="1"
app:layout_rowWeight="1"
android:text="fourth"/>
</android.support.v7.widget.GridLayout>
Starting from API 21, the GridLayout now supports the weight like LinearLayout. For details please see the link below:
https://stackoverflow.com/a/31089200/1296944
I'm arranging elements on a layout, but if I use Linear Layout, everything lines up along the left. If I set gravity of some elements to be "center", for example, some texts, then on a phone, it looks ok. But if is a pad, then the whole view lies on the left side leaving large area on the right.
So how can I make the view in the center on any devices?
The second question is: If I set the text to be longer, then some words disappear (since they are out of the layout). How can I make the words lie in the center with several lines if necessary instead of being just one really long line and some part outside the view?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:layout_gravity="center">
<TextView
android:id="#+id/text"
android:layout_column="0"
android:layout_columnSpan="2"
android:layout_gravity="right"
android:layout_row="0"
android:layout_rowSpan="3"
android:ems="10" />
<RadioGroup
android:id="#+id/first_radiogroup"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="bottom"
android:layout_row="1"
android:layout_rowSpan="3" >
<RadioButton
android:id="#+id/1_1"
android:text="1_1" />
<RadioButton
android:id="#+id/1_2"
android:text="1_2" />
<RadioButton
android:id="#+id/1_3"
android:text="1_3" />
</RadioGroup>
<TextView
android:id="#+id/firsttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_row="2"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1.5"
android:text="please choose from the below:"
android:textColor="#000000"
android:textSize="40px"
android:textStyle="normal"
android:typeface="serif"
android:gravity="center"/>
<RadioGroup
android:id="#+id/secon_radiogroup"
android:layout_column="0"
android:layout_gravity="bottom"
android:layout_row="3"
android:layout_rowSpan="3" >
<RadioButton
android:id="#+id/2_1"
android:text="2_1" />
<RadioButton
android:id="#+id/2_2"
android:text="2_2" />
<RadioButton
android:id="#+id/2_3"
android:text="2_3" />
</RadioGroup>
<TextView
android:id="#+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_columnSpan="2"
android:layout_row="5"
android:shadowColor="#000000"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="1.5"
android:text="please choose from the below:"
android:textColor="#000000"
android:textSize="40px"
android:textStyle="normal"
android:typeface="serif" />
<Button
android:id="#+id/final"
android:layout_column="2"
android:layout_row="6"
android:text="final" />
<Space
android:layout_width="1dp"
android:layout_height="21dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="20dp"
android:layout_column="0"
android:layout_row="4" />
<Space
android:layout_width="1dp"
android:layout_height="174dp"
android:layout_column="0"
android:layout_gravity="fill_horizontal"
android:layout_row="5" />
<Space
android:layout_width="1dp"
android:layout_height="70dp"
android:layout_column="0"
android:layout_row="6" />
<Space
android:layout_width="213dp"
android:layout_height="1dp"
android:layout_column="2"
android:layout_row="0" />
<Space
android:layout_width="104dp"
android:layout_height="1dp"
android:layout_column="0"
android:layout_row="0" />
<Space
android:layout_width="3dp"
android:layout_height="1dp"
android:layout_column="1"
android:layout_row="0" />
<Space
android:layout_width="1dp"
android:layout_height="26dp"
android:layout_column="0"
android:layout_row="1" />
<Space
android:layout_width="1dp"
android:layout_height="166dp"
android:layout_column="0"
android:layout_row="2" />
</GridLayout>
You should make different layout file for different screen size.
here below link may help you
http://developer.android.com/training/basics/supporting-devices/index.html