I am having problems using a RelativeLayout in a ListView. I would like to have the two EditText fields one above the other, instead they completely overlap one another. I have more text to put in each row, so side by side isn't an option.
I tried taking out all extraneous code to make it work, so this is what I have:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is my relative layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<EditText
android:id="#+id/room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/time"
android:textSize="20sp" />
</RelativeLayout>
Any help would be greatly appreciated. Thank you.
Replace
android:layout_below="#id/subject"
With
android:layout_below="#+id/time"
So That EditText with id room will be below to EditText with id time.
You need to replace android:layout_below="#id/subject"
to android:layout_below="#+id/time"
Use LinearLayout for this use case
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical>
<EditText
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<EditText
android:id="#+id/room"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
</LinearLayout>
Related
In my app I currently have two text views in an alert dialog to display the temperature and the weather description. The problem with this is that whenever the description changes, the alignment changes.
I tried having my layout as a relative layout and move around both of them with one depending on the other but this does not help me out. Any suggestions?
This is my current code:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<TextView
android:id="#+id/climaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/desc"
android:layout_marginTop="18dp"
android:layout_marginStart="120dp"
android:textColor="#android:color/white" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/climaText"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:textColor="#android:color/white" />
</RelativeLayout>
It is happening due to change in data size , when the data is increased it will wrap the data when you will use wrap_content . I suggest you can use a parent view and fix the height.here is the code, let me know if its working. thanks
<?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="wrap_content"
android:layout_height="250px">
<TextView
android:id="#+id/climaText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/desc"
android:layout_marginStart="120dp"
android:layout_marginTop="18dp"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="250px">
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/climaText"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
I have a question for you all. When I have a simple text in an xml file in eclipse. Why doesn't the title show up?
I have searched for similar problems, but can't find it on stackoverflow. Hopefully someone can help me.
unfortunately, I can't post pictures. Well, the text: #string/ondergewicht is the title of the text.
#/string/ondergewichttekst is the text wich I've used.
And this is the code I've used. Sorry if this is a stupid question, and I'm sure it is..
<?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="horizontal" >
<TextView
android:id="#+id/ondergewichttekst"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="italic"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="#string/ondergewichttekst"
android:textSize="16sp"/>
<TextView
android:id="#+id/ondergewicht"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textStyle="italic"
android:gravity="center"
android:layout_marginTop="1dp"
android:text="#string/ondergewicht"
android:textSize="30sp"
/>
</LinearLayout>
So, the ondergewichttekst is showed, but the overgewicht is not. Maybe is there something like an text order?
Hopefully someone can help me out.
Jacob
Try this code ... It works fine for me.
<?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/ondergewichttekst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="#string/ondergewichttekst"
android:textSize="16sp"/>
<TextView
android:id="#+id/ondergewicht"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:gravity="center"
android:layout_marginTop="1dp"
android:text="#string/ondergewicht"
android:textSize="30sp"
/>
</LinearLayout>
Try this one
<?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">
<TextView
android:id="#+id/ondergewicht"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:gravity="center"
android:layout_marginTop="1dp"
android:text="#string/ondergewicht"
android:textSize="30sp" />
<TextView
android:id="#+id/ondergewichttekst"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="#string/ondergewichttekst"
android:textSize="16sp" />
</LinearLayout>
You have horizontal orientation in your Linear Layout and your first TextView has layout_width = match_parent This doesn't make sense, there is no more room for the second TextView.
Use vertical orientation of layout or use wrap_content instead of match_parent for widths and heights.
You have android:layout_width="match_parent" in both TextViews of a horizontal LinearLayout so the first TextView is taking up all of the width. Change them to wrap_content.
<TextView
android:id="#+id/ondergewichttekst"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="italic"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="#string/ondergewichttekst"
android:textSize="16sp"/>
<TextView
android:id="#+id/ondergewicht"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textStyle="italic"
android:gravity="center"
android:layout_marginTop="1dp"
android:text="#string/ondergewicht"
android:textSize="30sp"/>
Is it possible to align a view in XML in a RelativeLayout centered horizontal or vertical according another already existing view.
For example: lets say there is something like this:
The second text view should be displayed centered below the first text view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="72dp"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:layout_marginLeft="43dp" <!-- Is there a rule to center it? -->
android:text="centered below text 1" />
</RelativeLayout>
Is is possible to implement something like that in XML? Is there a rule that i have missed yet? I do not want to calculate the position programmatically
I have a much better solution than the accepted one. NO EXTRA NESTING! You can do this by combining two attributes on the smaller view. if you are centering horizontally you can use both align_start & align_end to the bigger view. Make sure the text gravity is centered btw "android:gravity="center". For Vertical alignment use both align_top & align_bottom. below is the modified implementation of your layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="43dp" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/second"
android:layout_alignEnd="#+id/second"
android:gravity="center"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView"
android:gravity="center"
android:text="centered below text 1" />
</RelativeLayout>
No need for unnecessary nesting like the accepted answer.
Use a separate parent layout for those views and add it in your main layout(can contain other things if you have)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_marginLeft = "30dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="centered below text 1" />
</LinearLayout>
...
...
other veiws go here
...
</RelativeLayout>
Use the followings which suits you
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_centerVertical="true"
Correct solution is to use ConstraintLayout
I tried to align a textview horizontally below a button in a RelativeLayout but it was not possible as align_bottom and layout_below didn't play well together. Finally i picked up constraintLayout and tried the same logic and it worked like a charm. here is the code below.
<android.support.constraint.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/episode_recording_header_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.399" />
<TextView
android:id="#+id/button_selected_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="12sp"
android:text="text which is exactly center aligned w.r.t the Button above"
app:layout_constraintEnd_toEndOf="#+id/episode_recording_header_stop"
app:layout_constraintStart_toStartOf="#+id/episode_recording_header_stop"
app:layout_constraintTop_toBottomOf="#+id/episode_recording_header_stop"
/>
</android.support.constraint.ConstraintLayout>
The final output is attached below
Do this->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" <!-- Is there a rule to center it? -->
android:text="centered below text 1" />
</RelativeLayout>
I found the solution:
Wrap the content into another RelativeLayout and then you can place this LayoutWrapper wherever you want:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="58dp"
android:layout_marginTop="58dp" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="dynamic text" />
<TextView
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerInParent="true"
android:text="centered below text 1" />
</RelativeLayout>
</RelativeLayout>
If you need to use RelativeLayout please don't use the nested layouts as far as possible. Today we have many other efficient cases to solve your task. But with use of the old faithful RelativeLayout the better way is to use attributes android:layout_alignTop, android:layout_alignBottom and android:gravity. For example, you would like to align TextView according to EditText vertically.
<RelativeLayout ...>
...
<TextView
android:id="#+id/tv1"
...
android:gravity="center"
android:layout_alignTop="#+id/edt1"
android:layout_alignBottom="#id/edt1"/>
<EditText
android:id="#+id/edt1"
...
android:layout_toRightOf="#id/tv1"/>
</RelativeLayout>
In the same way you can use android:layout_alignLeft, android:layout_alignRight and android:gravity or android:layout_alignStart, android:layout_alignEnd and android:gravity horizontally.
I've searched how can I align the app to the right side and the text to be written from right to left.
The only thing I've found and succeeded to do is to make the text be written from right to left by adding android:gravity="right" in the EditText and the Button, but still their sort in the line if from the left to the right.
The activity_main.xml file -
<?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:gravity="right"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText android:id="#+id/text_message"
android:gravity="right"
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/text_message" />
<Button android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:text="#string/send_button"
android:onClick="ButtonClicked" />
<Button android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:text="#string/delete_button"
android:onClick="DeleteClicked" />
</LinearLayout>
What I meant in "the sort is from the left to the right" is that the EditText is written before the "Send" button, and the "Send" button id written before the "Delete" button - so what's shown is "[____x] [Send][Delete]" and not "[Delete][Send][_____]", as I would want to.
You can use instead of LinearLayout, RelativeLayout and give your buttons some ids using
android:id="#+id/your_button_id
and then use the tags android:layout_toRightOf and android:layout_toLeftOf. In your case, something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="#+id/delete_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/delete_button"
android:onClick="DeleteClicked" />
<Button android:id="#+id/send_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="#+id/delete_button"
android:text="#string/send_button"
android:onClick="ButtonClicked" />
<EditText android:id="#+id/text_message"
android:layout_weight="1.0"
android:layout_width="0dp"
android:layout_toRightOf="#+id/send_button"
android:layout_height="wrap_content"
android:hint="#string/text_message" />
</RelativeLayout>
Try using
android:layout_gravity
You could also just use a RelativeLayout add
android:align_ParentRight="true"
Change
android:gravity="right"
to
android:layout_gravity="right"
i try show layout like image 1 (textView and editText in same line) but my out put shown like image 2 !
i try with this code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
>
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:layout_alignBaseline="#+id/entry"
android:layout_alignParentLeft="true"/>
/>
<EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label"
android:layout_alignParentTop="true"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="30px"
android:paddingLeft="30px"
android:text="Go..."
/>
</LinearLayout>
please help me
thanks
Try this
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="URL:"
android:layout_alignBaseline="#+id/entry"
android:layout_alignParentLeft="true"/>
/>
<EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/label"
android:layout_alignParentTop="true"
/>
</LinearLayout>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="30px"
android:paddingLeft="30px"
android:text="Go..."
/>
</LinearLayout>
Or use RelativeLayout
Use RelativeLayout instead of Linear one and get use of android:rightOf.
As far as I understood, you cannot use such _toRightTop, _BOTTOM attributes with a linear Layout. Use RelativeLayout and all should be fine.
Use relative layout instead of linear layout.One advantage of this is that it improves your performance.if you try to search and id suing a linear layout,it will check with every component of the linear layout and try to match with its id but with relativer layout it will be faster as all elements are defined relative to each other.