Currently I'm creating an app and ran into a weird issue. My activity has this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:customfont="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayout_all"
android:layout_width="320dp"
android:layout_height="407dp"
android:background="#color/background"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/rel_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/id_result"
style="#style/font_Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="105dp"
android:gravity="center"
android:maxLines="1"
android:maxWidth="216dp"
android:textColor="#color/grey" />
<TextView
android:id="#+id/id_unit"
style="#style/font_Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/id_result"
android:layout_alignBottom="#+id/id_result"
android:layout_alignStart="#+id/id_image"
android:maxLines="1"
android:maxWidth="80dp"
android:textColor="#color/grey" />
<ImageView
android:id="#+id/id_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/id_unit"
android:layout_marginBottom="27dp"
android:layout_toEndOf="#+id/id_result" />
<ImageView
android:id="#+id/id_image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/id_image"
android:layout_alignTop="#+id/id_result" />
</RelativeLayout>
</RelativeLayout>
In my onCreate() I call the Views by their ID and set the texts and the drawables. Depending on a meassured result, there are two scenarios:
1) Show the meassured result (let's say 42.9 GW/ms)
TextView id_result: '42.9'
TextView id_unit: 'GW/ms'
ImageView id_image: #drawable/image1_1
ImageView id_image2: #drawable/image2_1
Everything is fine
2) The result is too high
TextView id_result: 'HI'
TextView id_unit: visibility GONE
ImageView id_image: visibility GONE
ImageView id_image2: visibility GONE
Now here is the problem. I expected the text 'HI' to be shown centered on screen. Instead, only "H" is shown. When I set android:maxLines="2", it is revealed, that Android wraps the text after 'H'. 'I' sits on the second line. Why is this the case? '42.9' is longer and doesn't get wrapped.
Does anyone has a solution?
Related
I'm trying to create a message layout in a chat application.
This message layout should contain:
Message Textview in a rounded rectangle
Message status check icon ImageView within that same rectangle
Message send time TextView outside the rounded rectangle and to its right.
When text is short, the whole layout should align itself to the right.
When the text is long, message TextView is going to expand itself to the left and upon touching the left side of the screen, it expands to a new row.
The problem is: Rounded rectangle stretches to the whole screen regardless of the message being short or long although underlying FrameLayout width is set to wrap_content.
Here is an image of the problem:
The layout I am using is:
<RelativeLayout
android:id="#+id/outer_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="8dp">
<TextView
android:id="#+id/text_message_time"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:textSize="10sp"
app:showDate="#{message.postedAt}" />
<RelativeLayout
android:id="#+id/bubble_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/text_message_time"
android:background="#drawable/rounded_rectangle_bubble_sent"
android:padding="8dp">
<ImageView
android:id="#+id/iv_check"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="4dp"
app:setStatusPng="#{message.status}" />
<TextView
android:id="#+id/messagetext_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="2dp"
android:layout_toLeftOf="#id/iv_check"
android:singleLine="false"
android:text="#{message.messageText}"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
The inner RelativeLayout named bubble_ll has its width equals wrap_content.
What I am expecting is:
text_message_time TextView should be attached to the right of the screen due to its layout_alignParentEnd property set true.
bubble_ll stands left of the text_message_time view, due to its layout_toLeftOf property set. And it should have a width of its contents.
I have tried other layouts such as Linear, ConstraintLayout etc.
The LinearLayout approach did not work, since the width is set to wrap_content for TextView always overlaps status info checks and message time make them disappear when the message in the TextView is long.
With ConstraintLayout I could not align bubble to stay still on the right. It stays on the center of the screen horizontally since it is constrained on both horizontal sides of the screen.
Any help will be appreciated.
I have come up with the following layout which is a combination of RelativeLayout and LinearLayout. I hope this serves your purpose. Please change the drawables and the ids if necessary.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/outer_rl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="8dp">
<TextView
android:id="#+id/text_message_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/message_container"
android:layout_alignParentRight="true"
android:layout_margin="4dp"
android:text="10:30 am"
android:textSize="10sp" />
<LinearLayout
android:id="#+id/message_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_toLeftOf="#+id/text_message_time"
android:background="#android:color/holo_blue_dark"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/messagetext_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="1"
android:padding="4dp"
android:singleLine="false"
android:text="You have a long message here. This is so long that it takes two lines inside the TextView. No wonder, now its taking three lines and now its four. How long this message can go? I now have that question!!!"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/iv_check"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_centerVertical="true"
android:layout_margin="8dp"
android:src="#android:drawable/presence_online" />
</LinearLayout>
</RelativeLayout>
I have 3 TextViews inside a linearLayout (horizontal), and when the last textview is too big or there is no space for it, it is matching parent and looking like the image below:
Image Here
What I want is to, IF the textview reaches the linearlayout's width, then, the textview should be moved below "Property" textView. (next line)
Take a look at how it should be:
Image here
That's a snippet of my XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="Property"
android:id="#+id/txtPropertyType" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 127 m"
android:id="#+id/txtArea"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 3 roomsssssssssdddd"
android:singleLine="false"
android:id="#+id/txtNumRoom"
android:layout_marginLeft="5dp" />
</LinearLayout>
Thanks in advance x)
Modify your xml and add another TextView below the LinearLayout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="Property"
android:id="#+id/txtPropertyType" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 127 m"
android:id="#+id/txtArea"
android:layout_marginLeft="10dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 3 roomsssssssssdddd"
android:singleLine="false"
android:id="#+id/txtNumRoom"
android:layout_marginLeft="5dp" />
</LinearLayout>
<TextView
android:visibility="gone"
android:layout_below="#+id/linearLayout"
android:id="#+id/long_room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="dfgdfdfgddfgdfgdd"/>
Now, you have 2 options:
OPTION 1: Check the length of the room number textview. If it's more than, say, 12 (you have to find the number considering all letters to be widest ones - like w), then make the visibility of txtNumRoom GONE and that of long_room visible.
OPTION 2: Find the height of the txtNumRoom before it gets rendered and displayed. If this height is greater than height of txtArea, that means the textview has gone multiple-lines. Again, in that case,make the visibility of txtNumRoom GONE and that of long_room visible.
Refer to this question to know how to find height of a textview programmatically before it gets rendered.
Just add android:singleLine="true" on each text view. Your problem here is that your TextView is spreading on more than one line.
Since August 2020, there is the option to use a ConstraintLayout with the virtual layout Flow.
Your elements should be wrapped in a ConstraintLayout together with a Flow element like in the following sample XML:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="Property"
android:id="#+id/txtPropertyType"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 127 m"
android:id="#+id/txtArea"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearance"
android:text="/ 3 roomssssssssssssssssssssss"
android:singleLine="false"
android:id="#+id/txtNumRoom"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.constraintlayout.helper.widget.Flow
android:id="#+id/flow"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:constraint_referenced_ids="txtPropertyType, txtArea, txtNumRoom"
app:flow_horizontalGap="4dp"
app:flow_horizontalStyle="packed"
app:flow_horizontalBias="0.0"
app:flow_wrapMode="chain"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/flow"
app:layout_constraintStart_toStartOf="parent"
android:textAppearance="?android:attr/textAppearance"
android:text="$2000.000"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The elements that are involved in the wrapping (all elements in the line that must wrap if too big) must have ids which are then added in the app:constraint_referenced_ids tag of Flow.
app:flow_horizontalStyle="packed" and app:flow_horizontalBias="0.0" are necessary to left-align your text (flow_horizontalBias won't work unless elements are packed), but can be changed depending on the layout you want to achieve.
app:flow_wrapMode tells the flow-layout how to position elements, and must be set to either chain or aligned, once again chain was necessary to be able to left-align the text.
It is also important to set the height of the Flow element to 0dp so it can determine how much space it needs by itself.
The TextView below should be constrained to the Flow element so that it can be positioned dynamically depending on the position of the elements inside.
Screenshot of the layout with small text
Screenshot of the layout with overflowing text
I am trying to put a text below an image but they are not showing up in android layout.
Here is the screenshot:
I dont' see any text below image.
And my code: inside a relative layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_footer"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/panelcolor" >
<ImageView
android:id="#+id/searchicon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingRight="60dp"
android:src="#drawable/search_white" />
<TextView
android:id="#+id/searchText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="60dp"
android:textColor="#color/whitetext"
android:layout_below="#+id/searchicon"
android:text="Search" />
<ImageView
android:id="#+id/homeicon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingRight="60dp"
android:layout_toRightOf="#+id/searchicon"
android:src="#drawable/ic_home" />
<TextView
android:id="#+id/hometext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="60dp"
android:textColor="#color/whitetext"
android:layout_below="#+id/homeicon"
android:text="Home" />
<ImageView
android:id="#+id/wishicon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#+id/homeicon"
android:src="#drawable/ic_favorite" />
<TextView
android:id="#+id/wishtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="60dp"
android:textColor="#color/whitetext"
android:layout_below="#+id/wishicon"
android:text="Wish List" />
<ImageView
android:id="#+id/viewicon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingLeft="60dp"
android:layout_toRightOf="#+id/wishicon"
android:src="#drawable/ic_eye" />
<TextView
android:id="#+id/viewtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="60dp"
android:textColor="#color/whitetext"
android:layout_below="#+id/wishicon"
android:text="Viewed" />
</RelativeLayout>
Not sure what is wrong?
Assuming you follow Googles Design guidelines
(Tabs with icons and text Height - 72dp;
Icon - 24 x 24dp)
Change 'match_parent' to 'wrap_content' for the ImageViews
The problem is you have put android:layout_height="match_parent" for image views and android:layout_height="wrap_content" for text views. Either use wrap_content for image view or use use a LinearLayout with android:weight property. Using LinearLayout would also solve your problem of alignment if it occurs!
Try setting android:layout_alignParentBottom="true" instead of android:layout_below="#+id/homeicon".
This aligns the text at the bottom of the parent. You might also need to set android:layout_alignLeft="" and align it to the corresponding imageView.
This method will only work, if there is enough padding below the image. This is because the text will now overlap the image.
EDIT:
Another way would be, to set the relative layout to a fixed height you know (like 60dp) and set the imageView to a fixed height (like 40dp). Then the TextView will also show up below the ImageView.
I have a problem regarding Textview on my battery widget.
The has a picture and on it textview showing the % the problem is that there seems to be something wrong with my code regarding the text width because it allows only 2 characters per verse so it shows for ex. 50 in the first verse and % in the second. How do i set it right? Also side question: can i somehow make the text to always be on the bottom and center? I can do it with padding but it shows differently on each screen size. So anyway here's the code regarding text EDIT: Here is entire xml file-:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imageView1_widget"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:src="#drawable/bateria50_1" />
<TextView
android:id="#+id/textView1_widget"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1_widget"
android:singleLine="true"
android:layout_gravity="bottom|center_horizontal"
android:shadowColor="#000000"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="1"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
For LinearLayout:
android:singleLine="true" and android:layout_gravity="bottom|center_horizontal"
For RelativeLayout:
android:layout_alignParentBottom="true" and android:layout_centerHorizontal="true"
I'm attempting to create a a Heading + button similar to the Google Music App, e.g. where there is a "Songs" Header on the Left and then on the right there is a Button with the text "X more"..
I've using a RelativeLayout for the TextView and Button
My problem is that the button is taking up the size of the layout that contains the text the height is all wrong and the padding doesn't seem to do anything.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
[REMOVED for clarity]
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/list_foreground"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="#string/photos"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/photo_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#color/actionbar_background"
android:padding="10dp"
android:text="test" />
</RelativeLayout>
What am I doing wrong here?
RelativeLayouts are designed to have children in the layout "relative" to each other. In other words, if you want the Button to the right of the Textview, you need to tell it.
Because you are aligning relative to the parent LEFT / RIGHT, it appears that things are "kind of" working.
You may be better off with a LinearLayout, depending on your needs. LinearLayouts use "orientation" not RelativeLayouts.
You should look over some tutorials (like this one: http://mobile.tutsplus.com/tutorials/android/android-layout/) but ultimately you will probably put your button in first and then your text view so that the textview content will wrap appropriately.
To get the same effect as the Music App I ended up using a RelativeLayout but instead of a Button I'm using another TextView, this is giving the impression it's a button but it gives me more scope to format the background etc. I think just setup a OnClickListener in the code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/photo_title">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="#string/photos"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/more_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#color/actionbar_background"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="10 MORE"
android:textColor="#color/button_text"
android:textSize="12sp" />
</RelativeLayout>