SELMANON SELMANON - 1 year ago 122
Android Question

How to correctly update my layout (based weight attribute) with cardview?

I am working to update my app with

CardCiew
widget, I already update some simple layout.

The problem that when I use a
LinearLayout
set with
layout_weight
attribute I get a different result I want.

layout.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:id="@+id/technologie_ll_item">

<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content">

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

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iv_logo_technologie"
android:layout_width="75dp"
android:layout_height="75dp"
app:border_width="2dp"
app:border_color="#99069d78"
android:src="@drawable/after"
android:paddingLeft="0dp" />

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
android:id="@+id/tv_nom_technologie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textAllCaps="true"
android:paddingLeft="10dp"
android:gravity="center_vertical"
android:text="Orthodontics"/>

</LinearLayout>

<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/indic" />

</LinearLayout>

</LinearLayout>

</android.support.v7.widget.CardView>

</LinearLayout>


Result



enter image description here

Answer Source

Maybe this can help you.

Approach 1

(Note i have changed your CircleImageView with a Imageview so i could make it works without errors ;))

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/technologie_ll_item">

<android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:weightSum="10"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

            <ImageView
                android:id="@+id/iv_logo_technologie"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:paddingLeft="0dp" />
        </LinearLayout>

        <TextView
            android:id="@+id/tv_nom_technologie"
            android:layout_width="0dp"
            android:layout_weight="7"
            android:layout_height="wrap_content"
            android:textSize="17sp"
            android:textAllCaps="true"
            android:paddingLeft="10dp"
            android:layout_gravity="center_vertical"
            android:text="Orthodontics" />

        <LinearLayout
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="fill_parent"
            android:gravity="right"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

As you can see you will have 1 LinearLayout which android:weightSum is 10 (just to make the proportion better) that holds the 3 components. The first LinearLayout container that holds the CircleImageView (aka ImageView in my code) will have a android:layout_weight of 2, the text will have a android:layout_weight of 7, and the arrow container will have a android:layout_weight of 1.

Maybe you can get rid of the LinearLayout that holds the arrow, and set the weight directly in the ImageView, i think that if you apply the arrow as android:src it will be resized respecting the scale of the image given, but I am not sure about this now.

Approach 2:

Another solution will be the RelativeLayout, as the code below:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/technologie_ll_item">

    <android.support.v7.widget.CardView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!-- CircleImageView here -->
            <ImageView
                android:id="@+id/iv_logo_technologie"
                android:layout_width="75dp"
                android:layout_height="75dp"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:paddingLeft="0dp" />

            <!-- Arrow drawable here -->
            <ImageView
                android:id="@+id/arrow"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <!-- Text here, between circle and arrow -->
            <TextView
                android:id="@+id/tv_nom_technologie"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textSize="17sp"
                android:layout_toRightOf="@id/iv_logo_technologie"
                android:layout_toLeftOf="@id/arrow"
                android:textAllCaps="true"
                android:paddingLeft="10dp"
                android:layout_centerVertical="true"
                android:text="Orthodontics" />
        </RelativeLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

With this you remove the LinearLayout and you will have only 3 components, maybe better for perfomance.

Hope some of this will help you mate.

Regards!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download