Search code examples
androidandroid-linearlayoutandroid-constraintlayout

Set weight for a View inside ConstraintLayout


I have the following project in Github : https://github.com/Ali-Rezaei/Contacts, where I show flags of countries based on contact's numbers.

The following layout includes flagItem as a LinearLayout in which I add the flag imageViews to it programmatically :

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/contact_selector"
    android:minHeight="64dp">

    <TextView
        android:id="@+id/image_text"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:layout_marginStart="16dp"
        android:layout_marginLeft="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="A.R" />

    <TextView
        android:id="@+id/contact_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:paddingStart="16dp"
        android:paddingLeft="16dp"
        android:textSize="17sp"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toTopOf="@+id/guideline"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toEndOf="@+id/image_text"
        tools:layout_constraintEnd_toStartOf="@id/phone_type"
        tools:text="Ali Rezaei" />

    <LinearLayout
        android:id="@+id/flagItem"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingStart="16dp"
        android:paddingLeft="16dp"
        app:layout_constraintStart_toStartOf="@+id/contact_name"
        app:layout_constraintTop_toTopOf="@+id/guideline" />

    <TextView
        android:id="@+id/phone_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp"
        app:layout_constraintStart_toEndOf="@id/flagItem"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="+989121895634" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <TextView
        android:id="@+id/phone_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-light"
        android:paddingEnd="8dp"
        android:paddingRight="8dp"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="@+id/guideline"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline"
        tools:text="Mobile" />

    <TextView
        android:id="@+id/line_number"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:layout_marginEnd="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/contact_circle"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/guideline"
        tools:text="2" />

    <View
        android:id="@+id/line"
        style="@style/LineStyle"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/image_text" />

</android.support.constraint.ConstraintLayout>

Problem is : enter image description here ImageViews may conflict with line_number view. Is there any solution to show three dotsat the end of imageViews before conflicting with line_number as we show in TextView using :

android:ellipsize="end"
android:maxLines="1"

Addenda :

I tried :

<LinearLayout
    android:id="@+id/flagItem"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toStartOf="@id/line_number" />

But it is not a solution because it moves phone_number to the right corner which is not desired.


Solution

  • I ended up adding an imageView to the layout :

    <ImageView
            android:id="@+id/flagImageView"
            android:layout_width="@dimen/dimen_flag_image_view_width"
            android:layout_height="@dimen/dimen_flag_image_view_height"
            android:layout_marginStart="16dp"
            app:layout_constraintStart_toStartOf="@+id/contact_name"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            tools:ignore="ContentDescription"
            tools:src="@drawable/flag_sweden" />
    
        <LinearLayout
            android:id="@+id/flagItem"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingStart="16dp"
            android:paddingEnd="@dimen/dimen_flag_image_view_margin_end"
            app:layout_constraintEnd_toStartOf="@id/line_number"
            app:layout_constraintStart_toStartOf="@+id/contact_name"
            app:layout_constraintTop_toTopOf="@+id/guideline"
            tools:ignore="RtlSymmetry" />
    

    And display flag in ImageView when we have only one number :

    if (numbers.size() == 1) {
       flagImageView.setImageResource(getFlagResID(context, countryCodeNumber.regionCode));
    }
    

    Otherwise add runtime ImageViews to LinearLayout:

    List<ImageView> list = new ArrayList<>();
    
    for (int childPosition = 0; childPosition < numbers.size(); childPosition++) {
    
         ContactPhoneNumber phoneNumber = numbers.get(childPosition);
    
         ImageView imageView = getFlagImageView(context, phoneNumber.regionCode);
         if (!list.contains(imageView)) {
             flagItem.addView(imageView);
             list.add(imageView);
         }
    }