skip to Main Content

I need help. I’m using android studio to build an app. I want to display my page like this. Desired display design. But there is an error. The page didn’t display as I want. It displays like this even with using constraint layout. Error display design. I want my design match with displayed design in any size. Here I attached my code and image.

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

<androidx.drawerlayout.widget.DrawerLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/page">

        <include
            android:id="@+id/include2"
            layout="@layout/main_toolbar" />

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="58dp"
            android:layout_height="52dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:src="@drawable/lynx"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/include2"
            app:layout_constraintHorizontal_bias="1.0"
            app:layout_constraintStart_toStartOf="@+id/include2"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="157dp"
            android:layout_height="182dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.135"
            app:srcCompat="@drawable/one" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Leader"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.504"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/include2"
            app:layout_constraintVertical_bias="0.354" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="153dp"
            android:layout_height="166dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/imageView3"
            app:layout_constraintHorizontal_bias="0.061"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.499"
            app:srcCompat="@drawable/two" />

        <TextView
            android:id="@+id/textMember1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Madi"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/include2"
            app:layout_constraintVertical_bias="0.635" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="153dp"
            android:layout_height="166dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.932"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.499"
            app:srcCompat="@drawable/three" />

        <TextView
            android:id="@+id/textMember2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Ain"
            android:textSize="15dp"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.837"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/include2"
            app:layout_constraintVertical_bias="0.627" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="153dp"
            android:layout_height="166dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.05"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.834"
            app:srcCompat="@drawable/four" />

        <TextView
            android:id="@+id/textMember3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Syahmina"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.184"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/include2"
            app:layout_constraintVertical_bias="0.911" />

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="153dp"
            android:layout_height="166dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.933"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.834"
            app:srcCompat="@drawable/five" />

        <TextView
            android:id="@+id/textMember4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Adlina"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:textSize="15dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.837"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/include2"
            app:layout_constraintVertical_bias="0.912" />
    </androidx.constraintlayout.widget.ConstraintLayout>

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white">
        <include layout="@layout/main_nav_drawer"></include>
    </RelativeLayout>
</androidx.drawerlayout.widget.DrawerLayout>

2

Answers


  1. You are seeing this behavior because you are using fixed size on your views, different devices will have different screen size, and android:layout_width="157dp" will not look the same on every device.

    You can use percents tp make your layout responsive:

    All you need to do, is to add android:layout_width="0dp" and
    android:layout_height="0dp" to your layout items and accordingly give them app:layout_constraintHeight_percent="0.y" or
    app:layout_constraintWidth_percent="0.x"


    For example:

    A view with height equal to 20% of his parent view size and height equal to 40% of his parent view (for you case the parent view is the screen):

      android:layout_width="0dp"
      android:layout_height="0dp" 
      app:layout_constraintHeight_percent="0.2"
      app:layout_constraintWidth_percent="0.4"
    
    Login or Signup to reply.
  2. You can take a LinearLayout containing the Image and Text. The apply constraints on those layouts. I think it will reduce complexity in your case.

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="157dp"
            android:layout_height="182dp"
            android:scaleType="centerCrop"
            app:srcCompat="@drawable/one" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/adamina"
            android:gravity="center_horizontal"
            android:text="Leader"
            android:textAlignment="center"
            android:textColor="@color/black"
            android:textSize="15dp"
            android:textStyle="bold" />
    </LinearLayout>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search