Avoid AbsoluteLayout
ID |
java.android_absolute_layout |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Efficiency |
Language |
Java |
Tags |
android, best-practice, efficiency |
Description
Reports every <AbsoluteLayout> element found in an Android layout resource. AbsoluteLayout is deprecated since API level 3 and produces UIs that do not adapt to screen sizes, orientations, densities, or accessibility sizing.
Rationale
AbsoluteLayout positions children using fixed pixel offsets. This breaks on every device that is not the exact size the designer used: content truncates, overlaps other views, or leaves large empty regions. It also fights against the platform’s text-scaling, split-screen, and foldable behaviour, making the app effectively inaccessible.
<!-- Bad: hard-coded absolute positions -->
<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_x="20dp"
android:layout_y="40dp"
android:text="@string/title" />
</AbsoluteLayout>
Remediation
Use ConstraintLayout for complex arrangements, LinearLayout for stacked flows, or RelativeLayout for anchor-based positioning. All three adapt to the available space and accessibility settings.
<!-- Good: relative positioning -->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="@string/title" />
</androidx.constraintlayout.widget.ConstraintLayout>