More on UI design covering main layouts
Thu, 14 Nov 2024
android:orientation
:horizontal
: Places the childs in horizontal direction(left to right).vertical
: Places the childs in vertical direction(top to bottom).android:gravity
:android:weightSum
:android:layout_weight
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DesignActivity"
android:orientation="vertical"
android:weightSum="10">
<LinearLayout
android:id="@+id/llTop"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center"
android:orientation="vertical"
android:background="#890E0E">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@color/white"
android:text="height = 30% of parent"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llCenter"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:weightSum="100"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="#0C990C">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:gravity="center"
android:textSize="20sp"
android:padding="8dp"
android:textColor="@color/white"
android:background="#000000"
android:text="20%"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:gravity="center"
android:textSize="20sp"
android:padding="8dp"
android:textColor="@color/white"
android:background="#505050"
android:text="30%"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:textSize="20sp"
android:padding="8dp"
android:textColor="@color/white"
android:background="#000000"
android:text="50%"/>
</LinearLayout>
<LinearLayout
android:id="@+id/llBottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:background="#060695"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="@color/white"
android:text="height = 40% of parent"/>
</LinearLayout>
</LinearLayout>
EditText
can be put inside a LinearLayout
with vertical orientation.layout_alignParentTop
/ layout_alignParentBottom
layout_alignParentLeft
/ layout_alignParentRight
layout_centerInParent
layout_toRightOf
/ layout_toLeftOf
layout_above
/ layout_below
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#6BB1B4B2"
android:layout_margin="16dp"
android:padding="4dp">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#00ff00"
android:contentDescription="close icon"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:src="@drawable/baseline_close_24"/>
<ImageView
android:id="@+id/ivAlert"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="battery alert icon"
android:src="@drawable/baseline_battery_alert_24"
app:tint="#F22B2B" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ivAlert"
android:padding="4dp"
android:text="Battery low. Charge your device"
android:textSize="20sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
LinearLayout
.LinearLayout
and RelativeLayout
.ConstraintLayout
, we have to fix the constraints on 4 sides.<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="My First App"
android:textSize="25sp"
android:textStyle="bold" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="My First App"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
layout_constraintTop_toTopOf
layout_constraintBottom_toBottomOf
layout_constraintStart_toStartOf
/ layout_constraintEnd_toEndOf
layout_constraintHorizontal_bias
/ layout_constraintVertical_bias
layout_constraintDimensionRatio
layout_constraintStart_toEndOf
layout_constraintEnd_toStartOf
layout_constraintTop_toBottomOf
layout_constraintBottom_toTopOf
app:layout_constraintEnd_toStartOf="@+id/textViewRight"
textViewRight
.<view_constraint> _ <other_constraint> of <other_view_id>
layout_constraintEnd
. This is constraint for current view.toStartOf
. This is constraint for other view.textViewRight
.textViewRight
.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#890E0E"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="height = 30% of parent"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/llCenter"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#0C990C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout2">
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:background="#ffffff"
android:contentDescription="close icon"
android:src="@drawable/baseline_close_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewLeft"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:padding="8dp"
android:text="33%"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/textViewMiddle"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewMiddle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#505050"
android:gravity="center"
android:padding="8dp"
android:text="33%"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/textViewLeft"
app:layout_constraintEnd_toStartOf="@+id/textViewRight"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textViewLeft"
app:layout_constraintTop_toTopOf="@+id/textViewLeft" />
<TextView
android:id="@+id/textViewRight"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:padding="8dp"
android:text="33%"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="@+id/textViewMiddle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/textViewMiddle"
app:layout_constraintTop_toTopOf="@+id/textViewMiddle" />
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#060695"
android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".4"
app:layout_constraintTop_toBottomOf="@+id/llCenter">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="height = 40% of parent"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Views are like a component like for showing text, taking input etc.
TextView
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="18sp"
android:textColor="#000000"
android:padding="16dp" />
Hello, World!
EditText
<EditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"
android:inputType="text"
android:maxLength="20"
android:padding="16dp" />
TextInputLayout
with TextInputEditText
to have more customization.<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</com.google.android.material.textfield.TextInputLayout>
Button
<android.widget.Button
android:id="@+id/buttonAddPrefix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="@drawable/button_ripple"
android:text="Add prefix"
android:textColor="#ffffff"
android:textSize="18sp" />
drawable/button_ripple.xml
file.<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#002300">
<item android:id="@android:id/mask">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<solid android:color="#002300"/>
</shape>
</item>
<item android:id="@android:id/background">
<shape android:shape="rectangle">
<corners android:radius="4dp"/>
<solid android:color="#026102"/>
</shape>
</item>
</ripple>
Progress Bar
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:indeterminate="true"
/>
This is a comment 1.
This is a comment 2.