I want to use a RelativeLayout to align horizontally a textview and after that an edittext and then again a textview and then an edittext. I know that you can do that with a LinearLayout, but I want to accomplish that with a relativeLayout.
My Code is the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/userNameLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:layout_alignParentLeft="true" />
<EditText
android:id="#+id/userNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/userNameLbl"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="#+id/pwdLbl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_toRightOf="#+id/userNameText"
android:layout_toEndOf="#+id/userNameText" />
<EditText
android:id="#+id/userNameText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/pwdLbl"
android:minWidth="200dp"
android:text="Sample Text" />
</RelativeLayout>
But it doesn't work. All the controls (edittexts and textviews) are put on each other! What is wrong with my code? I have used layout_toRightOf to put them next to each other.
And because it was mentioned, there is enough place for the controls.
Here is also a picture of the designer, how it is looking like:
I copied your code just to see the exact problem, but everything looked fine. However The last EditText was cramped on the right side due to space-issues.
Are you sure you have enough space so that everything can fit?
With too little space given it might be possible that the views get crammed onto each other.
When you look closer, in android:layout_toRightOf and android:layout_toEndOf attributes you are using #+id/... values. Delete the + in order to refer to an existing resource item.
Ok, the problem seems to be in Xamarin Studio itself which is not rendering the content of the layout correctly. Because when I run this layout on a device the controllers are put next to each other and there is not such a problem, but the designer show it completely different.
Try this code it will align as you need:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:id="#+id/userNameLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Username:" />
<EditText
android:id="#+id/userNameText"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
<TextView
android:id="#+id/pwdLbl"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:id="#+id/userNameText2"
android:layout_width="0dp"
android:layout_weight="0.25"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:text="Sample Text" />
</LinearLayout>
Comment below if you need any further info
Related
I'm trying to figure out why a two-line button in my application is being shifted a couple of pixels lower than the other buttons:
This does not happen if I shorten the text on the third button until it fits on one line, which tells me it has something to do with the line break. Adding android:layout_gravity="top" to the button's layout doesn't seem to help. Any ideas what might be causing this one?
Edit: Here's the layout XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:padding="10dip"
android:layout_width="wrap_content">
<TextView android:id="#+id/error_text"
android:layout_height="wrap_content"
android:layout_marginBottom="5dip"
android:text="Place holder"
android:layout_width="wrap_content"
android:textSize="17dip"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:padding="10dip"
android:layout_width="wrap_content">
<Button android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/black"
android:textStyle="bold"/>
<Button android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:text="#string/cancel_login"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
<Button android:id="#+id/third_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
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.
Having had a look at your layout (on a device), I am not sure why it exhibits this strange behaviour. When debugging layouts I like to put background colours on Views so you can see more clearly the space they are taking up. If we remove all the padding, we see that the buttons simply don't sit on the same top line. If we apply android:gravity="center_vertical" to the containing LinearLayout we see that the first two buttons are centered but the last one sits snugly with the top edge.
One solution to this is just to rewrite the inner container using a RelativeLayout:
<RelativeLayout
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_width="wrap_content">
<Button android:id="#+id/ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ok"
android:textColor="#color/black"
android:textStyle="bold"/>
<Button android:id="#+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_toRightOf="#id/ok_button"
android:text="#string/cancel_login"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
<Button android:id="#+id/third_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dip"
android:layout_toRightOf="#id/cancel_button"
android:textColor="#color/black"
android:textStyle="bold"
android:visibility="gone"/>
</RelativeLayout>
From my testing, by using a RelativeLayout you can get all of the buttons to sit on the same top edge.
I ran your code with the following change and it worked fine:
Change the android:gravity in the horizontal linear layout from "center_horizontal" to "center".
I have add the following lines to your third button in XML and make it's height to "fill_parent" :
android:paddingTop="4dp"
android:layout_height="fill_parent"
For me, 4dp worked fine you can check if you need more or less.
Make these changes and your button will be fine, like its fellows :-)
write android:layout_height="fill_parent" for all three buttons
I have a button help with a margin right and left of 10dp. Then I add a button test to the left of a TextView (centered horizontal), below the button help and align left to the button help.
The problem is that the button test is not exactly aligned to the button help like TextView test1 to the right of button help.
What am I doing wrong?
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<TextView
android:id="#+id/mid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="1"
android:gravity="center"
/>
<Button
android:id="#+id/help"
android:text="Help"
android:layout_width="match_parent"
android:layout_marginTop="35dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/help"
android:layout_marginTop="5dp"
android:background="#87CEFF"
android:text="Test"
android:layout_toLeftOf="#id/mid"
android:layout_alignLeft="#id/help"
/>
<TextView
android:id="#+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/mid"
android:layout_alignBottom="#id/test"
android:layout_alignTop="#id/test"
android:layout_alignRight="#id/help"
android:text="test1\ntest2"
android:gravity="center"
android:textColor="#0000ff"
android:background="#00cc00"
/>
</RelativeLayout>
i think thats just the background of the help-button.
if you use a custom background for it aswell, it will fill up all his space.
wrap test button and textview in a relative layout and align parent right and left each one.
There is nothing wrong in your code only one thing you have to add to your button test is android:background="#android:color/darker_gray" or any colour of your choice, android by default provides padding padding to button.
<Button
android:id="#+id/help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="35dp"
android:background="#android:color/darker_gray"
android:text="Help" />
i have made necessary changes in above code try it hope it solves your issue
I have created a table view inside of a list view in Android studio (see following code) and for various reasons I have used a text view and a checkbox as separate elements rather than just having the checkbox and attaching the text. The problem is the text is on the left of the screen/table but the checkbox is essentially centred where as I want it to be at the far right hand side.
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_above="#+id/Button1"
android:id="#+id/scrollView"
android:background="#drawable/list">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/Maths"
android:id="#+id/Maths"
android:layout_column="1"
android:textColor="#ffffff" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/MathsCheck"
android:layout_column="2"
android:buttonTint="#00ffff"
android:onClick="MathsClicked"/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/History"
android:id="#+id/History"
android:layout_column="1"
android:textColor="#ffffff"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/HistoryCheck"
android:layout_column="2"
android:onClick="HistoryClicked"
android:buttonTint="#00ffff" />
</TableRow>
</TableLayout>
</ScrollView>
There are in reality a lot more of these text view and checkboxes but you get the idea. I have tried using padding and margins but that doesn't really work as it will work ok on some devices displays but I have to hard code it which isn't ideal. I've also tried the gravity feature which for some reason doesn't seem to do anything.
Any help appreciated
Is there a particular reason why you are using a TableLayout?
If not, the desired output (TextViews left-aligned, Checkboxes right-aligned) can be easily achieved with a RelativeLayout within a vertical LinearLayout.
Your XML would then look like this (including only the relevant parts, all other attributes of your code will remain unchanged):
<ScrollView>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentLeft="true" />
<CheckBox
android:layout_alignParentRight="true" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
Edit:
If you want to place multiple checkboxes next to each other, you can do so by making their position reference each other, like this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_alignParentLeft="true" />
<CheckBox
android:id="#+id/checkbox1"
android:layout_alignParentRight="true" />
<CheckBox
android:id="#+id/checkbox2"
android:layout_toLeftOf="#id/checkbox1" />
<CheckBox
android:id="#+id/checkbox3"
android:layout_toLeftOf="#id/checkbox2" />
</RelativeLayout>
Note that you have to define the rightmost checkbox first, and the leftmost last.
Given the width of the checkboxes is identical (which they is in most cases), they will perfectly align.
I have the following code defined in my xml file, but I don't see the buttons getting displayed. I have read way too many posts, and pretty much tried all possible combinations, but no luck :( I'd really appreciate if someone could help me here.
<?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:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:gravity="center"
android:text="#string/hello_world"
android:textSize="32sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/ad_desc"
android:inputType="text" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/phone" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sms" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/maps" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/video" />
</LinearLayout>
I would like to see my App Name on top (Text view - #string/hello_world), right beneath it, a big box that allows me to EditText(#string/ad_desc), and beneath that, all my buttons. The button should be at the bottom, next to each other aligned in this manner - http://www.mkyong.com/android/android-linearlayout-example/ (Linear layout - Horizontal example), except I want my buttons to be at the bottom, and not at the top. Instead I see something else (Image attached)
Please feel free to ask me if you'd like me to attach anything else. I am new to Android programming, and this is totally beyond me on why it isn't working.
If you want to align your Buttons at the bottom then you are better off with a RelativeLayout as the root. Try changing it to the following and notice the properties
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="#string/hello_world"
android:textSize="32sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/ad_desc"
android:inputType="text"
androidi:layout_below="#id/textView2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/phone" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sms" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/maps" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/video" />
</LinearLayout>
</RelativeLayout >
You may have to add some margin to the TextView and EditText for appropriate spacing but this should give you your TextView with the EditText below it and your Buttons in a horizontal LinearLayout aligned at the bottom of the screen.
Edit
Notice this property
androidi:layout_below="#id/textView2"
of the EditText and this property
android:layout_alignParentBottom="true"
Of the LinearLayout that I added.
The easiest thing is to create a new relative layout, and place the item that you need from the palette.
Or start to try and learn to make matrioska layout via xml ;)
http://developer.android.com/guide/topics/ui/declaring-layout.html
been trying to figure out Android layouts and having a bit of an issue. i got as far as figuring out how to get the objects in the places i want, using a RelativeLayout (tried LinearLayout and TableLayout combinations, only to find that gravity and attributes weren't doing things i thought they would...) but i've run into an issue with text bleeding behind other text.
the second item demonstrates the problem. i'd like to have this same layout without the text bleeding. here is my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="wrap_content"
android:padding="6dip">
<TextView android:id="#+id/item_text_product"
android:layout_alignParentLeft="true"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/item_text_total"
android:layout_alignParentRight="true"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#00aa00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/item_text_count"
android:layout_alignParentRight="true"
android:layout_below="#id/item_text_total"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text_count"
android:text="Count: "
android:textSize="12sp"
android:layout_toLeftOf="#id/item_text_count"
android:layout_alignBaseline="#id/item_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Put first your item_text_count and text_count items (I mean, put them above the text_count in the XML)... then:
<TextView android:id="#+id/item_text_total"
android:layout_alignParentRight="true"
android:layout_toLeftOf="#id/text_count"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#00aa00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
As you see, the difference is android:layout_toLeftOf="#id/text_count".
Set a maxEms attribute on the product TextView so that it's width can never exceed n ems.
That way should be the best solution.
I think you just have to add android:layout_toRightOf="#id/item_text_product" to the second textView, but can't try it right now.
The issue is with wrap_content, with it the view is going to expand to fit its content.
What about shrinking the text and using nested Linear Layouts?
You might try and use a LinearLayout if you don't want overlapping behavior.
Something like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="wrap_content"
android:padding="6dip">
<TextView android:id="#+id/item_text_product"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="#+id/item_text_total"
android:layout_alignParentRight="true"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="#00aa00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/item_text_count"
android:layout_alignParentRight="true"
android:layout_below="#id/item_text_total"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text_count"
android:text="Count: "
android:textSize="12sp"
android:layout_toLeftOf="#id/item_text_count"
android:layout_alignBaseline="#id/item_text_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
</LinearLayout>
layout_weight="1" causes that view to expand to fill the available space not taken up by the right-hand views.