Search code examples
javaandroidxmlandroid-studiotextview

android studio button textwiew gestures


They are constantly moving how do I make them fixed

I made a random application but when you press the button, the textview and button are constantly changing. How can I prevent this? As below pictures

enter image description here

enter image description here

enter image description here

the codes I use in these

<TextView
        android:id="@+id/tw1"
        android:textSize="15dp"
        android:text=""
        android:gravity="center"
        android:textIsSelectable="true"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <Button 
        android:id="@+id/btn1"
        android:text="random"
        android:textSize="50dp"
        android:layout_centerInParent="true"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="200dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

Solution

  • The problem is that you are using a LinearLayout and its children are laid out horizontally by default. Based on the length of the text in your TextView is 'pushing' the Button to the right (horizontally). If you want to change this behavior consider using a different layout or change the orientation of the current LinearLayout by doing so:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical">
    

    LinearLayout children orientation