0

I have a simple GridLayout:

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    android:rowCount="1" >

    <TextView
        android:layout_column="0"
        android:layout_row="0"
        android:text="Label"/>

    <EditText
        android:inputType="text"
        android:layout_column="1"
        android:layout_row="0"
        android:text="asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf"
        android:layout_gravity="fill_horizontal"/>
</GridLayout>

However this causes the EditText to extend off the screen, as can be seen below (image from the IDE but it does the same when running on a device).

enter image description here

It appears that the EditText is actually the full width of the screen rather than the width of the GridLayout cell, which is why it is going off-screen. What has gone wrong in the layout to cause this?

Bryan
  • 14,756
  • 10
  • 70
  • 125

1 Answers1

0

I am not sure why you need GridView for the case, maybe you know better then me, but you could find something useful for you in this post Gridview with two columns and auto resized images.

In your case GridView has 2 columns and inside every one of them you have views which have not set any values for width and height params and in this case Android is excepting them for wrap_content.

When TextView and EditText have layout_width="wrap_content" they will extend them self on one line until they wrap the whole content. That's why you are going outside of the phone screen.

Try this xml and you will see the expected behavior:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="1">
<TextView
    android:layout_width="20dp"
    android:layout_height="wrap_content"
    android:layout_columnWeight="0.5"
    android:layout_column="0"
    android:text="Label with some long text event more text"/>
<EditText
    android:layout_width="50dp"
    android:layout_height="wrap_content"
    android:layout_columnWeight="1.5"
    android:inputType="textMultiLine"
    android:layout_column="1"
    android:text="Some really long text here and more and more and more ....  "
    android:layout_gravity="fill_horizontal"/>

What we are doing here is just to set some hardcoded values to your children (TextView and EditText), about they android:layout_width attributes, and give them android:layout_columnWeight attribute as well. This will set some proportions of your columns (play with this value just to set it for your needs).

With EditText we are doing also something small (android:inputType="textMultiLine") just to make sure that your text will be wrapped on multiple lines.

BTW: If you are doing some input forms I will suggest you to use other ViewGroups like LinearLayout for this.

Thanks any questions pls comment.

Community
  • 1
  • 1
Stoycho Andreev
  • 6,163
  • 1
  • 25
  • 27
  • Thank you for your response. The example was cut down to focus on the problem which is why it is simplistic. The solution was pointed to in the comment to the question, which was to set layout_width to 0dp. –  Aug 29 '16 at 14:09
  • I am not sure why you giving me -1 for this response when you are doing same stuff to solve the problem (setting 0dp to your width attr), but any why I can't do anything about that. My answer is not wrong to receive negative vote – Stoycho Andreev Aug 29 '16 at 14:38
  • I haven't selected your answer as correct because it uses fixed widths, which would not provide the required outcome to fill the width of the screen but not fall off the edge. –  Aug 29 '16 at 14:50
  • 1
    I don't need my answer to be accepted , I was just trying to help you in this situation , and I received negative vote which is not right because my answer is working for sure. I am sure that you did not even try my xml :), because if you did you will see that everything is working, and key part is not only my fixed width but layout_columnWeight as well, fixed width is just hack in this case the layout_columnWeight is playing the big role and it control how much is for the column 0 and how much is for column 1. This was my last commend about the case. Good look. – Stoycho Andreev Aug 29 '16 at 20:58