xceph
xceph

Reputation: 1046

Setting layouts to the size of their contents

I have a simple XML I am trying to inflate, and struggling with the layout. What I want to achieve is quite simple (or so I thought), so I am sure I am going about it incorrectly. Basically I have three elements, 2 TextViews, and an Imageview. I want the two text boxes placed on top of one another, with the image on the right of them, aligned to the right side, and its view fitted it the size of the image. Like this:

    Text 1                     |==| 
Text 2 would be below text 1   |==|

Heres my XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout

        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

Problem is that I cannot get the image to display without hard coding the size of the LinearLayout containing the two text boxes.

Any help is appreciated!

Upvotes: 0

Views: 216

Answers (3)

Jitender Dev
Jitender Dev

Reputation: 6925

Always Use Relative Layout, it requires lesser no. of code as compared to other Layouts.

Try below, i guess this is what you want, set Padding/Margin according to your requirement.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <TextView
        android:id="@+id/entry1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="@dimen/tab_host_default_height"
        android:text="@string/test_string"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/entry2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/entry1"
        android:ellipsize="marquee"
        android:fadingEdge="horizontal"
        android:gravity="left"
        android:paddingLeft="5dp"
        android:paddingTop="5dp"
        android:text="@string/test_string" />

    <ImageButton
        android:id="@+id/entry3"
        style="@style/ImageButtonBDTheme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/ic_launcher"
        android:contentDescription="@string/click_for_repair_report"
        android:paddingRight="5dp"
        android:src="@drawable/entry_link" />

</RelativeLayout>

Upvotes: 1

Hareshkumar Chhelana
Hareshkumar Chhelana

Reputation: 24848

// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/entry_container"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:padding="5dp"
              android:minHeight="?android:attr/listPreferredItemHeight"
              android:paddingRight="?android:attr/scrollbarSize"
              android:gravity="center">

    <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

        <TextView
                android:id="@+id/entry1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:paddingLeft="@dimen/tab_host_default_height"
                android:text="@string/test_string"
                android:textStyle="bold" />

        <TextView
                android:id="@+id/entry2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:orientation="vertical">

        <ImageButton
                android:id="@+id/entry3"
                style="@style/ImageButtonBDTheme"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/activated_background_holo_light"
                android:contentDescription="@string/click_for_repair_report"
                android:paddingRight="5dp"
                android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/entry_container"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal"
    android:paddingRight="?android:attr/scrollbarSize" >

    <LinearLayout
         android:layout_width="0dp"                  //changes here
         android:layout_weight="0.6"                 //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/entry1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="@dimen/tab_host_default_height"
            android:text="@string/test_string"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/entry2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:gravity="left"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="@string/test_string" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"              //changes here
        android:layout_weight="0.4"                //changes here
        android:layout_height="match_parent"
        android:orientation="vertical" 
        android:gravity="center"
        >

        <ImageButton
            android:id="@+id/entry3"
            style="@style/ImageButtonBDTheme"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/activated_background_holo_light"
            android:contentDescription="@string/click_for_repair_report"
            android:paddingRight="5dp"
            android:src="@drawable/entry_link" />

    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions