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"/>
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>
In my app I have the following layout, and no matter what value I give the textview doesn't change its width. Can someone please help me in correcting this layout.
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginTop="15dp"
android:descendantFocusability="blocksDescendants" >
<TableRow
android:layout_width="match_parent"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/rec_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/fill_rece"
android:ems="10"
android:focusable="false"
android:padding="10dp"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/btn_rec_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/content"
android:src="#drawable/ipad_postcare_landscape_from" />
</TableRow>
</TableLayout>
I also get a warning near TableRow saying "This TableRow layout or its TableLayout parent is useless". Please guide me.
Thanks in advance.
Try this one Insted of table use Linear Layout with and set LAYOUT_WEIGHT property. This property shares the equal amount of space for text view and image button
<?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="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/rec_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/fill_rece"
android:focusable="false"
android:padding="10dp"
android:textColor="#FFFFFF" />
<ImageButton
android:id="#+id/btn_rec_delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="#string/content"
android:src="#drawable/ipad_postcare_landscape_from" />
</LinearLayout>
try this....,you can add android:textSize="some value" which you want t o give
<TextView
android:id="#+id/rec_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:padding="10dp"
android:text="hllo"
android:textSize="25dp" />
You might need to use
android:layout_weight
I am mentioning one example for your help, also make sure your every element has height and weight with it, to display everything correctly
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/layout_border"
android:weightSum="1.0"
android:id="#+id/r1c1r2">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textIsSelectable="true"
android:id="#+id/key"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:textIsSelectable="true"
android:id="#+id/key2" />
</LinearLayout>
Give the textsize using:
android:textSize="some value"
try to give size according to your requirement you have the width of the table row and textview set as match_parent hence it(textview) can't change it's size.
android:layout_width="50dp"
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 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>
I´m developing an instant message application for an university course, similar to Whatsapp as you could guess seeing the icon in the attached screenshot :).
I have a ListFragment being populated with a custom CursorAdapter. It gives two type of views, sent messages (aligned to right) and received messages (aligned to left).
The layout for sent messages works nicely, but I cannot say the same for received messages.
Here is a screenshot:
Sent message 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="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFCCCCCC"
android:padding="5dp" >
<TextView
android:id="#+id/ceo_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFBBBBBB"
android:gravity="center"
android:textSize="10sp" />
<TextView
android:id="#+id/ceo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/ceo_timestamp"
android:background="#FFAAAAAA"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
Received message 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="wrap_content"
android:gravity="left" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FF999999"
android:padding="5dp" >
<TextView
android:id="#+id/cei_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FF888888"
android:gravity="center"
android:textSize="10sp" />
<TextView
android:id="#+id/cei_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/cei_timestamp"
android:background="#FF777777"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
Those ugly background colors are added just to see the space taken for each view and layout. As you can see in the screenshot, send messages (right-aligned) work great. Received messages (left-aligned) aren't aligning well, because RelativeLayout takes all horizontal space available, insted of just wrapping content.
Anyone knows how to fix it, or maybe a better layout design to accomplish what I need?
Thanks a lot.
In Sent message layout replace android:layout_alignParentRight="true" parameter with the android:layout_alignParentLeft="true".
When RelativeLayout's child is alignParentRight, it's width will be auto changed to match_parent. TextView cei_timestamp 's android:layout_alignParentRight="true" make it's parent's width change from wrap_content to match_parent silently.
U have two ways to correct:
do not use RelativeLayout
<?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="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FF999999"
android:padding="5dp" >
<TextView
android:id="#+id/cei_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF777777"
android:gravity="right"
android:textSize="16sp" />
<TextView
android:id="#+id/cei_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:background="#FF888888"
android:gravity="center"
android:textSize="10sp" />
</LinearLayout>
</RelativeLayout>
do not use wrap_content, both use specific width like 60dp.
I know it is late but I am posting it for someone looking for the solution of same problem.
To align the received message to the left just remove this line from second TextView in xml for received messages.
android:layout_toLeftOf="#+id/cei_timestamp"
I tried it with a simple example and it worked.
Here is the code and screen shot
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.myapplication1.MainActivity">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FF4081">
<TextView
android:id="#+id/textView"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center"
android:text="Hello"
android:textSize="23sp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/textView"
android:gravity="right"
android:text="New Text"
android:textColor="#FFF"
android:textSize="23sp" />
</RelativeLayout>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout centerHorizontal="true"
android:layout marginTop = "104dp"
android:text="New Button" /></RelativeLayout>`