[Android] This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint


Android Error This view is not constrained vertically
This view is not constrained vertically: at runtime it will jump to the top unless you add a vertical constraint

If you get the above error in the activity XML file when trying to add an element in the UI say a Button or an EditText, well it's because you have not set the vertical or horizontal constraints. When you are on the design view you may also see the errors below,

❗️ Not Horizontally Contrained.
❗️ Not Horizontally Contrained.

Solution:
  1. Go to your activity.xml file,
  2. Move to Design View,
  3. Under Pallette, select Layouts -> ConstraintLayout,
  4. Now click on the wand icon -> Infer Constraint.
  5. You should see layout_margin Top and Right added to the XML and error is gone.
Before:
<Button
  android:id="@+id/button4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Button"
  tools:layout_editor_absoluteX="140dp"
  tools:layout_editor_absoluteY="316dp" />
After:
<Button
  android:id="@+id/button4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
 android:layout_marginStart="140dp"
  android:layout_marginTop="316dp"
  android:text="Button"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toTopOf="parent" />
Check a visual GIF demo to make it easy to understand,
Solution This view is not constrained vertically
Solution This view is not constrained vertically



Recent Posts:




Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.