Faiza Ahmad
Faiza Ahmad

Reputation: 57

How to fix the width in Horizontal Linear Layout?

how do I fix the width of each element in the horizontal linear layout. As shown in this picture?

I have updated the question with the code. Help would be appreciated. thank you!

enter image description here

<

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/l1"
android:orientation="horizontal"
android:layout_height="87dp"
android:layout_width="match_parent"
android:paddingBottom="9dp"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:paddingTop="9dp"
android:gravity="center"
>

<ImageView
    android:id="@+id/fbprofile_picture"
    android:layout_width="63dp"
    android:layout_height="63dp"
    android:src="@drawable/ic_logo_new"/>

<TextView
    android:id="@+id/fbprofile_name"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:fontFamily="sans-serif-condensed"
    android:paddingLeft="17dp"
    android:paddingRight="17dp"
    android:text="Faiza Zainab Ahmad " />

<TextView
    android:id="@+id/btn_fbfriends_connect"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_weight="2.31"
    android:background="@color/ThemeLightBlue"
    android:gravity="center"
    android:padding="8dp"
    android:text="CONNECT"
    android:textColor="#ffffff"
    android:textSize="16sp"
    />

</LinearLayout>

>

Upvotes: 0

Views: 8454

Answers (5)

Ajil O.
Ajil O.

Reputation: 6892

The better layout implementation would be like this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

Use weights to divide the layout into proportional widths.

Upvotes: 2

azisuazusa
azisuazusa

Reputation: 317

You can add weightSum in LinearLayout, and add layout_weight to the child of that LinearLayout, for example:

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:weightSum="3"
      android:orientation="horizontal">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:src="@drawable/ic_group_white_24dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>

  </LinearLayout>

Upvotes: 1

from56
from56

Reputation: 4127

Try this, the ImageView and the Button will keep their size and the TextView will fill the rest of the space.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:scaleType="fitCenter"
        android:src="@drawable/my_image" />


    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Some text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="The Button" />

</LinearLayout>

Upvotes: 4

Fatih Santalu
Fatih Santalu

Reputation: 4701

You can set your image's width statically. ie: android:layout_width="96dp"

And it's best to use RelativeLayout rather than LinearLayout with weights in this case. ie:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?listPreferredItemHeight"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@+id/button"
        android:layout_toRightOf="@+id/image"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

Upvotes: 1

SiSa
SiSa

Reputation: 2684

To achieve top LinearLayout you can use layout_weight for TextView with value 1 to fit the space between ImageView and Button

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

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

Upvotes: 0

Related Questions