I have a rather simple ListView row:
<?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" android:orientation="horizontal" > <TextView android:id="@+id/tournament_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:textSize="25dp" /> <TextView android:id="@+id/tournament_winner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textSize="25dp" /> </RelativeLayout>
When the text of "@+id/tournament_name"
is long - it overlaps with the one from "@+id/tournament_winner"
and I don't understand why.
I tried using android:singleLine="false"
to no avail. I also tried using android:inputType="textMultiLine"
as the docu says android:singleLine="false"
is deprecated but then I get the warning: Attribute android:inputType should not be used with <TextView>: Change element type to <EditText> ?
so no good here as well.
I also tried using android:ellipsize="end"
but this doesn't work. I assume it is because the text in the left TextView ("@+id/tournament_name")
is NOT long enough to fill up the full width of the ListView
(which code is not sowing here).
I was sure that if I use android:layout_width="wrap_content"
the two TextView fields shouldn't overlap.
Here is an example (see the second line):
Any further ideas how this could be fixed?
Just set ImageView (Twitter icon) elevation more higher than CardView elevation. Set android:elevation="10dp" to ImageView and set card_view:cardElevation="2dp" to CardView . Thanks for your answer but there's a problem with that solution. It's properly working inside the XML-editor but it doesn't render.
Use a compound drawable. The image will be inside the TextView, so it won't be covered by the text, because it would have its own reserved space inside the TextView. Mind that less Views (and/or layouts) = better performances (this is a dogma). This is the most optimized way to do that.
If relative layout has text or button items aligned to left and right sides they can overlap each other due to localized text expansion unless they have mutual constraints like toEndOf/toStartOf.
To center something in a RelativeLayout, you use android:layout_centerInParent="true" on the child. If you try to center several childs, they'll end up under/over each other.
android:layout_toLeftOf="@id/tournament_winner" in First TextView.
Also set android:maxLines="1"
and Fix width for tournament winner
because when it gets long tournament name cant see...
row.xml
<?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" android:orientation="horizontal" > <TextView android:id="@+id/tournament_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_toLeftOf="@id/tournament_winner" android:maxLines="1" android:text="NAMEEEEEEEEEEEEEEEEEEEEEEEEEEE" android:textSize="25dp" /> <TextView android:id="@+id/tournament_winner" android:layout_width="wrap_content" android:layout_marginLeft="10dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="WINER" android:textSize="25dp" /> </RelativeLayout>
Thank you very much for your answer - sorry it took me some time to respond. I ended up using your android:layout_toLeftOf="@+id/tournament_winner"
but left the single line and the margin to the left unused, as the result seemed perfect to me (hope this is also the case for other devices).
One thing though - in the first text view (tournament_name) I had to use android:layout_toLeftOf="@+id/tournament_winner"
and not android:layout_toLeftOf="@id/tournament_winner"
- pay attention to the added +. For some reason I get an error using android:layout_toLeftOf="@id/tournament_winner"
: Error: No resource found that matches the given name... so it seems that it is possible and NEEDED to define the resource in the time of calling it because the system doesn't know it before it was defined.
<?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" android:orientation="horizontal" > <TextView android:id="@+id/tournament_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/tournament_winner" android:layout_alignParentLeft="true" android:textSize="25dp" /> <TextView android:id="@+id/tournament_winner" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:textSize="25dp" /> </RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With