Home > Mobile >  Android studio margin effects other items
Android studio margin effects other items

Time:04-04

I try to put a textview top of a button and this button is centered. If I add margin to textview then button shift as well. How can I fix the button and add some margin to textview ?

Here my xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="#fff"
    android:orientation="horizontal"
    android:weightSum="1" >
    >

   <TableLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content">
       <LinearLayout android:layout_marginBottom="100sp"

           >
           <TableRow android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               >
               <TextView
                   android:text="asdasdasd"
                   android:textColor="@color/black" >

               </TextView>
           </TableRow>
       </LinearLayout>
       <LinearLayout >
           <TableRow android:layout_height="wrap_content"
               android:layout_width="wrap_content"
               android:background="@color/white">
               <Button
                   android:id="@ id/button"
                   android:layout_width="100sp"
                   android:layout_height="100sp"
                   android:layout_weight="1"
                   android:background="@drawable/round_button"
                   android:text="Başlat"
                   android:textColor="@color/white" />
           </TableRow>
       </LinearLayout>
   </TableLayout>

</RelativeLayout>

CodePudding user response:

If it's not a requirement to use RelativeLayout I would suggest using ConstraintLayout. First, you can set desired positions for the Button, and after setting the text bottom to be constrained to your Button. After that, you can add margin to your text without being afraid to influence Button position

Your layout can looks like:

<?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"
    android:layout_gravity="center"
    android:background="#fff">


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toTopOf="@ id/linearLayout"
        app:layout_constraintEnd_toEndOf="@ id/linearLayout"
        app:layout_constraintStart_toStartOf="@ id/linearLayout">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:text="asdasdasd"
                android:textColor="@color/black">

            </TextView>
        </TableRow>
    </LinearLayout>

    <LinearLayout
        android:id="@ id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <Button
                android:id="@ id/button"
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_weight="1"
                android:background="@drawable/round_button"
                android:text="Başlat"
                android:textColor="@color/white" />
        </TableRow>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

PS: Out of the topic of the question, but I would recommend using dp for setting the view size because sp is used for defining the size off the text.

Also, both LinearLayout and TableRow seem redundant and you probably can go directly with Button and TextView

  • Related