Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap content views rather than background drawable?

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?

like image 557
Petrus Avatar asked Nov 30 '10 09:11

Petrus


3 Answers

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.

like image 103
Eric Ruck Avatar answered Nov 11 '22 19:11

Eric Ruck


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.

like image 20
Michael Avatar answered Nov 11 '22 18:11

Michael


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.

like image 1
Alexander Haroldo da Rocha Avatar answered Nov 11 '22 17:11

Alexander Haroldo da Rocha