Issue
I want a few EditTexts with buttons on their right. Right now I just have the Edittexts filling the parent with the 0dp trick. If I add the button the height of the EditText will shrink with wrap_content. If I do match_parent one EditText fills the entire screen.
RelativeLayout doesn't work because you can't make it match the parent in the same way as LinearLayout.
I thought of getting the screen height and then setting the layout height to 1/7 that (since I have 7 EditTexts). Plausible?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:id="@+id/box_0"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_task"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:layout_gravity="end"/>
<!--red-->
</LinearLayout>
<EditText android:id="@+id/box_1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<EditText android:id="@+id/box_2"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<EditText android:id="@+id/box_3"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<EditText android:id="@+id/box_4"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<EditText android:id="@+id/box_5"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
<EditText android:id="@+id/box_6"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:saveEnabled="true"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"/>
</LinearLayout>
I only did the double LinearLayout in the first EditText.
http://i.imgur.com/t4hN6nO.jpg
See how the second one is larger than the rest to compensate and the first is smaller?
Solution
Why don't you add a ScrollView in there? There will be tons of devices that will never fit correctly 7 editText in the screen.
Anyway it's not raccomended to use nested LinearLayout with weight. But you can achive what you want to do this way.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<EditText
android:id="@+id/box_6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="@string/edit_task"
android:inputType="textCapSentences"
android:maxLength="32"
android:padding="10dp"
android:saveEnabled="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/button_send" />
<!--red-->
</LinearLayout>
A better solution that require a bit more work is, as you said, calculating the height with screenHeight/7
Answered By - Mario Lenci
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.