android – How to wrap content views rather than background drawable?-ThrowExceptions

Exception or error:

How can I get a LinearLayout (or any other ViewGroup) to assume the size of it’s child views rather than assuming the size of the background image?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@drawable/large_image300x300pix">

 <TextView android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello world!"/>
</LinearLayout>

The linear layout becomes the same size as the background image.
How can I get my linear layout to assume the same size as the textview?

How to solve:

OK, so this thread is a little old, but I have a solution that someone might someday find useful. I think Android has problems scaling large images down, so the LinearLayout size ends up getting bumped by the background drawable bitmap, and the ImageView ends up forcing up the size of the parent container.

Unless you use a relative layout. You can make the ImageView relative to the position of the LinearLayout, even when the ImageView is behind the layout in the parent. My solution looks something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
    android:src="@drawable/activation_popup"
    android:scaleType="fitXY"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignTop="@+id/activation_layout"
    android:layout_alignBottom="@id/activation_layout"
    android:layout_alignLeft="@id/activation_layout"
    android:layout_alignRight="@id/activation_layout"
    android:contentDescription="@string/act_code_label" />
<LinearLayout
    android:id="@id/activation_layout"
    android:clipToPadding="true"
    android:padding="25dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <!-- LinearLayout wraps a bunch of smallish views here -->
</LinearLayout>
</RelativeLayout>

I tried this on a top of display sizes and OS versions, seems to work great. Note that the padding in the LinearLayout is a trick to make space for a shadow border in the background image graphic. The LinearLayout doesn’t need any relative positioning because top left is assumed.

###

You can create a FrameLayout and put an ImageView and your LinearLayout there. So you’ll be able to configure the layout of your background image.

###

This happens because the Android view calculates its minimum size based on its background drawable size.

Check my answer here in this another post which covers the same problem which will help you to achieve your layout configuration.

###

If your image lends itself to being converted to a scalable 9-patch image, then doing that would cause the background to scale around the TextView.

###

I believe the best solution here is to set android:clipToPadding=”true”. What this does is excludes the padding for the main layout and wraps your layout to its children.

Leave a Reply

Your email address will not be published. Required fields are marked *