Search code examples
androidpositionelementcalculatorandroid-virtual-device

Moved activity elements on android emulator


I have elementary summator program. It should accept two int numbers and after click on "+" button, result out on TextView. When I try to click on sum button, button and TextView elements changes position. I'am didn't add any rules for change positions.

public void onclick (View v){
    EditText el1 = (EditText)findViewById(R.id.num1);
    EditText el2 = (EditText)findViewById(R.id.num2);
    TextView restext = (TextView)findViewById(R.id.result);
    int num1 = Integer.parseInt(el1.getText().toString());
    int num2 = Integer.parseInt(el2.getText().toString());
    int resSum = num1 + num2;
    restext.setText(Integer.toString(resSum));  
}

before click on button

after click on button

Also attached content of xml file. Maybe it has some defects.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.admin.test1.MainActivity">

<EditText
    android:id="@+id/num1"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_marginBottom="131dp"
    android:layout_marginTop="39dp"
    android:ems="10"
    android:hint="первое число"
    android:inputType="number"
    app:layout_constraintBottom_toTopOf="@+id/result"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_chainStyle="spread" />

<EditText
    android:id="@+id/num2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="48dp"
    android:ems="10"
    android:hint="второе число"
    android:inputType="number"
    app:layout_constraintStart_toStartOf="@+id/num1"
    app:layout_constraintTop_toBottomOf="@+id/num1" />

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="59dp"
    android:layout_marginEnd="25dp"
    android:text="результат"
    android:textIsSelectable="false"
    android:textSize="30sp"
    app:layout_constraintBottom_toTopOf="@+id/sum"
    app:layout_constraintEnd_toEndOf="@+id/num2"
    app:layout_constraintTop_toBottomOf="@+id/num1" />

<Button
    android:id="@+id/sum"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="151dp"
    android:layout_marginStart="28dp"
    android:onClick="onclick"
    android:text="+"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="@+id/result"
    app:layout_constraintTop_toBottomOf="@+id/result" />


Solution

  • It's because you textView "result" has "wrap_content" width and the button has "app:layout_constraintStart_toStartOf="@+id/result"". So first, you show the hint text in the TextView but when change the text the width is less and move the button to align with the start of the textView.

    If you want to center the button you can use:

    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"