TextView Position Shifting after adding Button Programmatically in Android

0

I have a question on adding Button to the fragment programmatically in the Android. Here below is the xml layout for my fragment.

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_blank_01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment_01">

    <!-- TODO: Update blank fragment layout -->
    <ImageView
        android:layout_width="800px"
        android:layout_height="480px"
        android:src="@color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/myTextView_Text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is Text #1"
        android:textSize="35px"
        android:textColor="@color/white"
        android:layout_marginTop="200px"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/myTextView_Text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is Text #2"
        android:textSize="25px"
        android:textColor="@color/white"
        android:layout_marginTop="260px"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here below is the layout shown on the Android Emulator before adding of the Button

Before adding Button to fragment

But after I add the Button with the following code, the layout of the TextViews have been changed.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_blank_01, container, false);

        //==========================================================================================//

        ConstraintLayout r1 = (ConstraintLayout) view.findViewById(R.id.fragment_blank_01);

        Button TestButton = new Button(getActivity());
        TestButton.setId(0);
        TestButton.setHeight(75);
        TestButton.setWidth(250);
        TestButton.setX(275);
        TestButton.setY(330);
        TestButton.setText("Button");
        TestButton.setBackgroundColor(0xffffffff);
        r1.addView(TestButton);

        //==========================================================================================//   
        
        return view;
    }

When simulate through the Emulator, the position of the TextViews are changed

After adding Button to fragment

May I ask what's going wrong in my code? Thanks a lot!

android
android-fragments
asked on Stack Overflow May 5, 2021 by Nadau

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0