T
T
thatmaniscool2018-08-03 09:21:11
Java
thatmaniscool, 2018-08-03 09:21:11

Apk android is crooked, the display of elements is incorrect, how to fix it?

Started learning android in android studio.
With the help of the editor, I brought out a couple of buttons and a text field, added a button handler, etc.
Collected the project, uploaded to the phone. But the display is not correct. All items are in the top right corner.
The code:

public class MainActivity extends AppCompatActivity {
    private TextView text = null;
    private Button OK = null;
    private Button cancel = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        setContentView(R.layout.my_screen);

        text = (TextView) findViewById(R.id.textView);
        OK = (Button) findViewById(R.id.button);
        cancel = (Button) findViewById(R.id.button2);

        OK.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                text.setText("You pressed OK button!");
                OK.setEnabled(false);
                cancel.setEnabled(true);
            }
        });

        cancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                text.setText("You pressed Cancel button!");
                OK.setEnabled(true);
                cancel.setEnabled(false);
            }
        });
    }
}

XML code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="179dp"
        android:layout_height="42dp"
        android:text="Hello this world"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="16dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="75dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="118dp"
        android:layout_height="46dp"
        android:text="Cancel"
        tools:layout_editor_absoluteX="34dp"
        tools:layout_editor_absoluteY="149dp" />
</android.support.constraint.ConstraintLayout>

It is worth that the rendering of the smartphone window caused an error and I had to change the line in Grald Scripts, build.grald from
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
to
implementation 'com.android.support:appcompat-v7 :28.0.0-alpha1'

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2018-08-03
@klim76

well, you should at least google for decency

V
Valentin Hetman, 2018-08-03
@spotyk

You need to make a binding, Google to the rescue

O
Orkhan, 2018-08-03
Hasanly @azerphoenix

Hello!
You are using ConstraintLayout. Find the Infer Constraints button in Android Studio and click (available in the compositor in visual editor mode). This will add constraints and bind.
https://stackoverflow.com/questions/46977540/infer...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question