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>
Related
I'm attempting to resize buttons on rotate to expand proportionately. Right now, they are a fix length, but I'm unsure of an alternative method to get the buttons to resize (setting the weight doesn't seem to help). I want the buttons to essentially fill the length of the screen both vertically and horizontally. Please let me know if you need more information.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_gravity="center_vertical" >
<Button
android:id="#+id/mainCommentBtn"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:text="#string/commentBtn" />
<Button
android:id="#+id/mainProfileBtn"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:layout_centerInParent="true"
android:text="#string/profileBtn" />
<Button
android:id="#+id/mainDetailBtn"
android:layout_width="119dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/shareBtn" />
</LinearLayout>
Replace your resource with this. Copy the edited changes
Notice how there width is 0dp because they are using weight.
Using Weight:
1. In order to use weight you need to set the weightSum in the parent.
2. Make sure the width or height is set to 0 according to how you are using weight.
(for instance since we are going with horizontal orientation we want to use "0dp" width. This allows weight to control the width of the object.
3. Lastly make sure the weight of all the objects add up to the weightSum.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/mainCommentBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/commentBtn" />
<Button
android:id="#+id/mainProfileBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/profileBtn" />
<Button
android:id="#+id/mainDetailBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/shareBtn" />
</LinearLayout>
You need to specify an orientation for the LinearLayout.
I guess you want to use android:orientation="horizontal".
Then, in order to make android:weight work, you'd need to specify the width of every button as 0px by using android:layout_width="0px" for every button.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
These xml properties size the button to fill the screen both vertically and horizontally.
You can create the layout with the help of layout weight so that it will automatically fill the screen in landscape mode also as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1" />
</LinearLayout>
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.
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 :)
i am desinging a xml
<?xml version="1.0" encoding="utf-8"?>
<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="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:gravity="right"
android:background="#3399FF"
android:text="Add" />
</LinearLayout>
</LinearLayout>
for button "Add" android:gravity=right is not working.i have give a layout:margin,then only its moving right.can anyone help me out.
It's a little unclear what you want, but do notice the following:
gravity affects the contents of the widget i.e. your button text.
layout_gravity affects positioning of the widget in the parent layout
So if you want the button to be on right, change gravity to layout_gravity.
Use RelativeLayout instead of LinearLayout.
and use android:layout_alignParentRight="true" for btnadd.
EDIT :
Code :
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|center"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
Change the LinearLayout to be as RelativeLayout
and instead of android:gravity="right" use android:layout_alignParentRight="true"
It is because you use LinearLayout (orientation horizontal)
Try to use relative layout
you are using linear layout in linear layout your layout depends on orientation like you are using vertical:
use relative layout instead to solve your problem,and give margins you want
also to align your button right use:
android:layout_alignParentRight="true"
your problem would definetly be solved with this.
it was because of you are using android:orientation="horizontal"in Linear Layout
use this android:orientation="vertical"
or use this i modified your code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:background="#000000"
android:orientation="horizontal" >
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#CCCCCC"
android:text="Cancel" />
<Button
android:id="#+id/btnadd"
android:layout_width="82dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#3399FF"
android:text="Add" />
</RelativeLayout>
If you want to achieve something like this:
You can do that either with RelativeLayout or LinearLayout.
For LinearLayout you explicitely add space between the buttons. This is because the android:layout_gravitypositions the view only within the space, that LinearLayout has given. And this space is not the LinearLayout itself, but the sum of all views, that were layed out linearly (as the name says).
Here's my way of putting the two buttons:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/btnadd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
<ImageView
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#android:color/transparent" />
<Button
android:id="#+id/btncancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
I always prefer LinearLayout over RelativeLayout as I experienced LinearLayout as more robust, especially on Android's 2.x versions.
BTW: The difference between android:layout_gravity and android:gravity:
The layout_gravity tells the parent layout of a view to position the whole view within the bounds that the layout defines. In case of LinearLayout this is the space that was allocated by linearly putting the views one after the other.
The gravity tells the view to position its foreground within the available space. You might have seen, that your Add-Button has shown the text on the right side.
I am trying to achieve the following layout on my Android app:
As you can see I want a central login box taking about 70% of the width. I believe there are basically two approaches to achieve this:
1. Create a Linear or Relative layout for the login box, and add layoutMargin both on the left and right sides.
2. Create a wrapper LinearLayout with weightSum="1", and then place the RelativeLayout inside it with layout_width="0dp" and layout_weight="0.7".
Using the first approach the XML would look like this:
<LinearLayout 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:orientation="vertical">
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/logo"
android:contentDescription="#string/logo"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#999999"
android:gravity="center"
android:layout_margin="30dp"
>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/email" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:hint="#string/password" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/login"/>
</LinearLayout>
</LinearLayout>
The second approach would just have a wrapper LinearLayout on top of the current one, with weightSum="1", and the internal layout would get a layout_weight="0.7" attribute.
Which one do you think is the best approach? Thanks in advance.
A rather simple way would be use a linear layout inside a relative layout. Then use layout_centerInParent and set it to "true". Once that is done you can set your margin to what ever you would like (I used 30 dp). Here is an example:
<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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="30dp"
android:orientation="vertical"
android:weightSum="50" >
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/editText1"
android:layout_centerHorizontal="true"
android:inputType="numberPassword" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/editText2"
android:layout_centerHorizontal="true"
android:text="Button" />
</LinearLayout>
</RelativeLayout>
Of course, if you wanted the width of the log in box to be exactly 70 percent of the width of the screen, you could do it programmatically.
There's a 3d alternative, create the login box in a LinearLayout/RelativeLayout, and then align it center horizontally.