Muhammed Refaat
Muhammed Refaat

Reputation: 9103

WrapContent around layout elements not bg

I'm designing a regular android LinearLayout which consists of a punch of elements, I set layout parameters both to wrap_content and that makes it wraps around either bg or elements according to which one is bigger, So, how to assign the layout so that it wraps around it's elements whatever which is bigger(bg/elements)?

for ex:

<LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/overlay">

        <TextView 
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</LinearLayout>

I want it to wrap around name whatever it's more big or overlay is bigger.

example images:

enter image description here

enter image description here

Upvotes: 1

Views: 63

Answers (4)

Quang Doan
Quang Doan

Reputation: 662

Try this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<TextView
    android:layout_width="wrap_content"
    android:padding="2dp"
    android:background="#FF4081"
    android:layout_height="wrap_content"
    android:text="this is text" />

Upvotes: 1

Narendra Motwani
Narendra Motwani

Reputation: 1115

Try this

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:background="#C3C3C3">

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

Upvotes: 0

Anil Prajapati
Anil Prajapati

Reputation: 457

Here's idea of doing tht..

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#000000">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView"
        android:background="#ffffff" 
        android:layout_margin="3dp"
        />

</LinearLayout>

Main line is... android:layout_margin="3dp" that gives you margin from all side as much you want.. Put right answer if its worthy to you.

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can replace

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/overlay">

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

with

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView 
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/overlay" />

</LinearLayout>

So background would be applied to TextView only. Anyhow, LinearLayout is of width wrap_content, so it will be fine.

Upvotes: 1

Related Questions