I want to put in the same row a TextView, and Edittext and a button but I am having the problem that the button is not aligned properly to left and in small screens edittext fills entire with.
Small screen:
Big Screen:
My codification is as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</RelativeLayout>
</LinearLayout>
Apply a weight to your EditText so it will take up as much room as it can while letting the other two elements do the normal wrap_content. To do this, remove the relative layout container and then change the EditText width to "0dp" and give it a layout_weight of "1" as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</LinearLayout>
First, many people will tell you that hint is Android's solution for not needing the label. I don't care if you use the label or not but it does save you space, especially on smaller screens. That was just an FYI.
Now, your RelativeLayout that only has a Button appears to be useless...I would remove that. You can use layout_weight so that each View takes up the appropriate amount of space. Make sure to make the layout_width="0dp". So it may look something like
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Go" />
</LinearLayout>
Here I used 2,3,1 for the weights of your TextView, EditText, and Button respectively. You may need to change those to get exactly what you want but that should give you a start.
Layout weigth is ideal for designing layouts that adjust to screen size. However, make sure to set layout_width to 0dp, or it won't work properly.
Use like this:
android:layout_width="0dp"
android:layout_weight="1"
I'm going to assume you mean the button is not properly aligned to the right.
It's because your RelativeLayout's android:width="wrap_content", but it should be android:width="match_parent".
Also, you'd be better off setting your EditText's android:width="0dp" and adding android:weight="1" so that it expands/contracts between screen sizes.
Related
I have a LinearLayout with two buttons. But, when a button has some longer text that needs breaking, the button's height does not expand to fit the text. Instead it crops the text.
Here's what I have:
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/previous_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="#string/previous_button"
android:textColor="#color/light_blue" />
<Button
android:id="#+id/next_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="#string/next_button"
android:textColor="#color/light_blue" />
</LinearLayout>
Any help on why button crops the text?
Try adding android:singleLine="false" property to the problematic button. Hope this solve your problem.
If it doesn't work, probably you'll need to remove the button's default padding by adding android:padding="0dp"
You can make the text to align in multi-lines.
A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.
To disable this behaviour, set android:baselineAligned="false" on the LinearLayout
For Example,
<LinearLayout
android:id="#+id/bottom_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<Button
android:id="#+id/two_board_btn_continue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/continue_bidding"
android:textColor="#android:color/white" />
<Button
android:id="#+id/two_board_btn_checkOut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/checkout"
android:textColor="#android:color/white" />
<Button
android:id="#+id/two_board_btn_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/add_to_cart"
android:textColor="#android:color/white" />
</LinearLayout>
Just remove #style from Both the buttons.
How much long your Button name. you have already used the match parent. So, only you can do to reduce the textSize of Button. That's All.
I want to present in my layout an EditText and a Button side by side with a small space (margin) between them. I don't want to set a fixed size (I think that's a bad habit).
How to do that?
My Try:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_alignParentLeft="true" />
<Button
android:id="#+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:text="Search" />
</RelativeLayout>
You're using a RelativeLayout; however, you cannot created a flexible design within that type of ViewGroup. You must use a LinearLayout.
We use android:layout_weight="1" along with the android:layout_width="0dp" to create a flexible control. Adjust the weight number for different size ratios.
Afterwards, we use android:layout_margin on both controls so the resulting weighted size of each is equal.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />
<Button
android:id="#+id/btn_search"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Search"
android:layout_marginLeft="8dp" />
</LinearLayout>
You can use Linear layout with horizontal orientation and add an EditText and Button in the following way
<LinearLayout
orientation="horizontal"
layoutwidth="match_parent"
layoutheight="wrap_content">
<EditText
layoutwidth="0dp"
layoutheight="wrap"
layout_weight=".8"/>
<Button
layoutwidth="0dp"
layoutheight="wrap"
layout_weight=".2"/>
</LinearLayout>
Hope this will solve your problem. Make sure to change the weights according to your needs.
Thanks
Use Linear layout to show both side by side and use 'match_parent' and 'wrap_content' appropriately.
Here's a piece of xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" >
<EditText
android:id="#+id/et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!!" />
</LinearLayout>
I've created a custom listview. All works fine though I struggle with the layout of the listitem.
The idea is to have a textview which can have a variable length (multilines is possible) and two buttons next to the textview all on one line.
This is what I have:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight=".80" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:layout_weight=".10"
android:layout_toRightOf="#id/textView1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_weight=".10"
android:layout_toRightOf="#id/button1" />
This works fine though my problem is that the textview fills out the parent and pushes the buttons of the screen. (when I have a short text the buttons are vissible)
So basically, what I need is a 80% wide textview aligned to the left and two 10% wide buttons aligned to the right. Can anyone guide me in the right direction?
Thanks
You can't use layout_weight with RelativeLayout. Try this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight="1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />
</LinearLayout>
Change to LinearLayout. You cannot use layout_weight with RelativeLayout.
Good Luck
In the following UI, I hope the two buttons both btnAddress and btnDelete take minimum space,
and editnumber take max space, how can I do? Need I use RelativeLayout layout? Thanks!
BTW, the following code, the btnDelete button only display a part.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:orientation="horizontal" >
<EditText
android:id="#+id/editnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:ems="10" >
</EditText>
<Button
android:id="#+id/btnAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnAddressMin" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnDeleteMin" />
</LinearLayout>
This should do it. Giving editnumber a weight of 1. Leve the rest as it is.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:orientation="horizontal" >
<EditText
android:id="#+id/editnumber"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:phoneNumber="true"
android:ems="10" >
</EditText>
<Button
android:id="#+id/btnAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnAddressMin" />
<Button
android:id="#+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/BtnDeleteMin" />
</LinearLayout>
Instead of fixing the length of EditText with ems, use layout_weight to fill the remaining space.
<EditText
android:id="#+id/editnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:phoneNumber="true" />
Edit: I'm not sure if android:phoneNumber="true" is still recommended or not, but I would change it to android:inputType="phone" instead.
Give the android:layout_weight="1 to edit text .No need to change it to relative layout
<EditText
android:id="#+id/editnumber"
android:layout_width="0dp"//no need to specifing the width if parent layout is horizontal
android:layout_height="wrap_content"
android:phoneNumber="true"
android:layout_weight="1";>
</EditText>
Doing this Edittext will take maximum space and remaing space will be left for other View.Other then this no need to change anything in your xml
I want to layout 3 element Button , TextView , Button on the bar like this
[Button1] --- TextView --- [Button2]
2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). How can I do it??
It would look something like this.-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Notice that you still need to set other properties such as texts, ...
You can use LinearLayout with android:weightSum
Try Like this, By using android:weightSum, you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="LeftButton" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:layout_weight="4"
android:gravity="center"
android:text="Center TextView text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="Right Button" />
</LinearLayout>
android:weightSum work fine for me, I resolved all my troubles :)