user001
user001

Reputation: 991

Textview goes offscreen on small screens

I have a listView and in the row of listView I have 2 TextViews side by side. First TextView has layout_width of 180dp and second of 5dp

It works fine on large screens but on small screens text goes off screen. I was thinking that dp would itself adjust properly on all screen sizes but it is not the case.

Below are the Examples from Large and Small screens

On Larger screen

This is a msg from TextView1 Small msg from TextView2

On Small screen

This is a msg Small msg

If you observe here, the last 2 words from TextView1 and from TextView2 are missing on small screens.

I want everything to come in 1 line without scroll.

How to resolve this?

Please find my xml below

<TextView
        android:id="@+id/tv1"
        android:layout_width="180dp"
        android:layout_height="wrap_content"            
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall" /> 

    <TextView
        android:id="@+id/tv2"
        android:layout_width="5dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/tv1"
        android:layout_toRightOf="@+id/tv1"
        android:layout_alignParentRight="true"
        android:textColor="#ff0000"
        android:gravity="right"
        android:layout_marginRight="5dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall" />

Upvotes: 0

Views: 273

Answers (2)

user001
user001

Reputation: 991

I am able to solve it now by creating different styles for different screens. I have now values-ldpi, mdpi, hdpi, xhdpi and xxhdpi folders and in each of them I have given different styles and now the UI looks better even on smaller screens.

Upvotes: 0

peedee
peedee

Reputation: 3739

That's not how dp works. They are useful when you have screens with different pixel densities, not with different absolute sizes. A screen can have dimensions of 960x540 pixels, with a hdpi density. This means that the screen has a total width of 360dp, since the base reference for dp is mdpi and hdpi=1.5*mdpi.

So in this case, if you define a view to be 400dp wide, it will go off screen.

Would it be an option to express those widths in terms of percentage instead? Your example sounds pretty extreme, but here goes:

<LinearLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal" >

  <TextView
    android:id="@+id/tv1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="36"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" /> 

  <TextView
    android:id="@+id/tv2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_alignBaseline="@+id/tv1"
    android:layout_toRightOf="@+id/tv1"
    android:layout_alignParentRight="true"
    android:textColor="#ff0000"
    android:gravity="right"
    android:layout_marginRight="5dp"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>

But seriously, what kind of text can you possibly fit into 5dp?

Upvotes: 1

Related Questions