2

I have a layout in a layout xml file. I have it with fixed width 110dp. Inside the layout I have a TextView with fixed width 100dp.

When I inflate the view with a LayoutInflater and add it in a LinearLayout, the TextView and it's parent RelativeLayout ignore the set widths and stretch to the text's size. I want the text to wrap in the specified width and take as much height as it requires.

I have tried to force the view's width after inflation ( converting dps to pxs) but still nothing.

Any suggestions?

Layout XML

<?xml version="1.0" encoding="utf-8"?>  

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="110dp"
    android:layout_height="match_parent"
    android:padding="1dp" >

    <RelativeLayout    
        android:layout_centerInParent="true"
        android:id="@+id/info_layout"
        android:layout_width="108dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="2dp"
        android:background="@drawable/rounded_corners_grey_3">

    <ImageView 
        android:contentDescription="@string/image"
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"/>                

    <ImageView 
        android:contentDescription="@string/image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@drawable/image_overlay"
        android:layout_centerInParent="true"/>

    <LinearLayout 
        android:id="@+id/detailsLayout"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="left"
        android:layout_margin="3dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        >            

        <TextView 
            android:id="@+id/name"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="12sp"
            android:gravity="center" 
            android:layout_weight="1"
            android:ellipsize="none"
            android:maxLines="100"
            android:scrollHorizontally="false"
            />            

        <TextView 
            android:id="@+id/details"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:textColor="@color/bethere_green"
            android:textSize="12sp"
            android:layout_weight="1"
            android:ellipsize="none"
            android:maxLines="100"
            android:scrollHorizontally="false"/>                      

    </LinearLayout> 

    <ImageView 
        android:contentDescription="@string/image"
        android:id="@+id/noPhotos"
        android:layout_width="55dp"
        android:layout_height="38dp"
        android:src="@drawable/photos_icon"
        android:layout_centerInParent="true"/>

    </RelativeLayout>

</RelativeLayout>
Panos
  • 7,227
  • 13
  • 60
  • 95
  • write your `xml` code, ill help – Jemshit Apr 08 '15 at 17:56
  • you want you text to have exact 100dp widthand relative layout inside have 110dp width? outer relative layout fills parent right? – Jemshit Apr 08 '15 at 18:56
  • the 100dp in the TextViews was a test in case it works with a fixed width. Actually I wanted it to be match_parent with an ~2dp margin. But it even ignores the 100dp...it can reach as far as the text goes. Views like this are added dynamically in a Horizontal LinearLayout which is inside a HorizontalScrollView – Panos Apr 08 '15 at 19:00
  • well i edited your xml but i don't still know what you need exactly. If you have image of screen or some sketch it would be clear – Jemshit Apr 08 '15 at 19:05

1 Answers1

6

My guess would be that your are doing this:

inflater.inflate(R.layout.textview, null, false);

instead of:

inflater.inflate(R.layout.textview, parent, false);

If you use the latter, the TextView will not lose its LayoutParams. If this was not case, could you edit your question to include code (XMLs and the inflation code), so we could see how you were attempting to do it.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
  • @ Daniel Zolnai I have included my xml. Have in mind that it contains various tries to fix this issue, like android:layout_weight="1" android:ellipsize="none" android:maxLines="100" android:scrollHorizontally="false" – Panos Apr 08 '15 at 18:55
  • Hey @Panos try this post :http://stackoverflow.com/questions/13960673/android-inflate-ignoring-utilizing-root-layout-width-height-defined-in-style i think this will solve your problem – Josef Apr 08 '15 at 19:39