wutzebaer
wutzebaer

Reputation: 14863

Android TextView with maxwidth

I have an TextView with a variable width, but maximum is 250dp. But somehow Android sets it always to maxwidth.

With a shorter text and without maxwidth it works, but then the text is long enough that the field exceeds the screen width, it overlaps the arrow.

enter image description here

I already tried this two variants, which give the same result as in the picture

<TextView
    android:id="@+id/bubbleText"
    style="@style/MarkerText"
    android:maxWidth="250dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:layout_marginEnd="@dimen/defaultSpacing" />

<TextView
    android:id="@+id/bubbleText"
    style="@style/MarkerText"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/defaultSpacing"
    android:layout_weight="1"
    android:background="#FF0000"
    android:maxWidth="250dp"
    android:singleLine="false" />

I want it to look like this:

enter image description here

Additional infos:

Complete XML

<LinearLayout
    android:id="@+id/markerTextBubble"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="invisible" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/marker_text_background" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="@dimen/tinySpacing"
            android:paddingEnd="@dimen/defaultSpacing"
            android:paddingStart="@dimen/defaultSpacing"
            android:paddingTop="@dimen/tinySpacing" >

            <TextView
                android:id="@+id/bubbleText"
                style="@style/MarkerText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="@dimen/defaultSpacing"
                android:background="#FF0000"
                android:maxWidth="250dp" />

            <mypackage.IconTextView
                xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/MarkerTextIcon"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="@string/icon_map_arrow" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_marginBottom="-5dp"
        android:layout_marginTop="-1dp"
        android:src="@drawable/marker_text_background_rectangle" />
</LinearLayout>
<style name="MarkerText" parent="android:Widget.Holo.Light.TextView">
    <item name="android:textColor">@color/myWhite</item>
    <item name="android:textSize">@dimen/markerTextSize</item>
</style>

<dimen name="markerTextSize">22sp</dimen>

The problem occurs in this independent snippet too:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#FF0000"
    android:maxWidth="250dp"
    android:text="1. Willkommen bei meiner APP"
    android:textSize="22sp" />

Upvotes: 1

Views: 1757

Answers (2)

shalafi
shalafi

Reputation: 3996

Not 100% sure, but I think the problem is that it is a multi-line text.

Does the view behave as expected when the text is shorter? i.e. "1. Welcome"

What I think happens is the following:

The view tries to make it on one line, then it expands until its maximum width, given that is not enough for rendering, makes it a multi-line, but does not stretch the view.

You could try with "1. Willkommen bei\nmeiner APP" to see if it shows the expected behaviour.

Hope this helps.

Upvotes: 2

user3956566
user3956566

Reputation:

Using the maxWidth won't be causing it to stretch.

It would most likely be in your code for:

android:layout_marginEnd="@dimen/defaultSpacing" 

and check any dimensions:

style="@style/MarkerText"

As setting the maxWidth alone will not cause this.

There must be some figure that is putting padding between your text and the right side textview, or possibly some peculiar margin mess up.

Using maxWidth should not force it out to the right like that, as I showed you in the image, I think it's the nested layouts and there is a lot of coding defining margins and padding within your layouts and elements, this could be forcing your texview to move to the right, and the text will automatically wrap, as it cannot fit in the first row, but the textview itself is still being pushed to the right.

image

Upvotes: 0

Related Questions