0

I'm facing an issue with an xml layout.

This is the full xml:

<FrameLayout
android:id="@+id/root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    tools:src="@drawable/segment_cultura"/>

<LinearLayout
    android:id="@+id/message_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:background="@color/black_divider"
    android:padding="4dp">

    <TextView
        android:id="@+id/message"
        style="@style/TextAppearance.AppCompat.Caption"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:maxLines="2"
        android:textColor="@color/white_secondary_text"
        />

    <ImageView
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_gravity="end"
        android:src="@drawable/ic_message"/>
</LinearLayout>

<ImageView
    android:id="@+id/cancel"
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_gravity="top|end"
    android:layout_margin="4dp"
    android:src="@drawable/ic_close"/>
</FrameLayout>

The goal: Use this as an item in a fixed height horizontal RecyclerView, using ONLY the image to determine the size of the item. Other views, such message_container or cancel will need to adapt to this size.

The problem: message_container needs to fill the width of the item, but it shouldn't modify the item width when there is a long text in message. It should go to the 2nd line and then get ellipsized. What happens instead is message never goes to the second line and makes the parents (message_container and root) enlarge to fit its text.

I'm looking for a solution that only involves xml, if I can't find it a custom view is preferred to some logic in the adapter.

Thank you for your time.

David Corsalini
  • 7,958
  • 8
  • 41
  • 66
  • Was going to tell you to consider this http://stackoverflow.com/questions/29956014/why-should-we-use-xml-layouts but then i read that last line. Have fun messing with your xml layouts. – Nanoc Nov 06 '15 at 11:34
  • What you ask there is done with 0 lines of codes using Percent***Layout. Also, you absolutely don't need layout based on pixel-density. By the way my last sentence state that if there is no solution using just xml layouts, I'll use a custom view. Have fun reinventing the wheel. – David Corsalini Nov 06 '15 at 11:53
  • I really have fun creating all my UI by code, and i never have rendering problems on any devices, even my 3 years old app will look perfect on any 2015 device, also its faster having only 1 RelativeLayout while you will need a lot of different Layouts (even some empty ones just to make empty spaces) to make the same UI by xml. – Nanoc Nov 06 '15 at 12:01
  • Faster and RelativeLayout are not words to put together. Also, if you need to use a RelativeLayout I think you use its children management, so basically you are just putting views in Java instead of XML and not actually customizing your onDraw method. You could do what you do in XML, but you choose to write it in Java. Rendering problems and good UI are 2 different things, not having a specialized UI for tablet is not good UI. Also, you can use Space if you need space, but I can't remember a single time I've used it. – David Corsalini Nov 06 '15 at 12:17
  • All im trying to say is that you probably would not have needed to ask this question if werent using xml layouts, having an issue is wasting more time than it takes to write code instead of xml. If i could be able to get the current device screen dimensions from xml i wouldnt complain about it. also note that by just using findViewById isnt going to be fast. – Nanoc Nov 06 '15 at 12:53
  • For the cases where you can't use just XML you can use a custom view. Using code in all cases is just a waste of time. – David Corsalini Nov 06 '15 at 13:55

1 Answers1

0

I solved this by using:

image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
         if (root != null && image.getWidth() != 0)
             root.getLayoutParams().width = image.getWidth();
    });
David Corsalini
  • 7,958
  • 8
  • 41
  • 66