F. Alvarado
F. Alvarado

Reputation: 43

Screen dimensions not effective?

I know and apply the form to get the dimensions of the screen, but something fails because apparently it is not effective in all the resolutions. If for example I define the location of some Buttons for a small resolution, those Buttons slides down as I run the application in higher resolutions. In the drawing below I have given the example of how the Buttons appear in an Emulator 2.7 QVGA API 25 (small) and in a Nexus 7 API 25 (xlarge). Have I to conclude that Android Studio has a failure? Please, help me to see my problem since I have searched and set it out before but I haven’t received any correct answers. This is my xml:

<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=".MainActivity">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="15"
    android:orientation="vertical">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:components="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/layout1">

    </RelativeLayout>
</ScrollView>

And this is my java:

public class MainActivity extends AppCompatActivity {

View pulsado;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    //Width and height of screen
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    int width = metrics.widthPixels;
    int height = metrics.heightPixels;

    // Buttons are placed
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
    for (int i = 1; i < 5; i++) {
        RelativeLayout.LayoutParams rel_btn = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        // Size of Buttons
        rel_btn.width = 4 * width / 100;
        rel_btn.height = 4 * width / 100;
        // Position of Buttons
        switch (i) {
            case 1:  // top left
                rel_btn.leftMargin = 0;
                rel_btn.topMargin = 0;
                break;
            case 2: // top right
                rel_btn.leftMargin = 96 * width / 100;
                rel_btn.topMargin = 0;
                break;
            case 3:  // lower left
                rel_btn.leftMargin = 0;
                rel_btn.topMargin = 80 * height / 100;
                break;
            case 4:  // lower right
                rel_btn.leftMargin = 96 * width / 100;
                rel_btn.topMargin = 80 * height / 100;
                break;
        }
        Button btnTag = new Button(this);
        btnTag.setLayoutParams(rel_btn);
        btnTag.setBackgroundColor(Color.BLUE);
        btnTag.setId(0 + i);
        btnTag.setOnClickListener(prueba);
        layout.addView(btnTag);
    }
}

    private View.OnClickListener prueba = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (pulsado != null) {
                Button button1 = (Button) pulsado;
                button1.setBackgroundColor(Color.BLUE);
            }
            Button button2 = (Button) view;
            GradientDrawable drawable = new GradientDrawable();
            drawable.setShape(GradientDrawable.RECTANGLE);
            drawable.setStroke(8, Color.RED);
            button2.setBackgroundDrawable(drawable);
            pulsado = view;
        }
    };
}

enter image description here

Upvotes: 0

Views: 49

Answers (1)

Aman Verma
Aman Verma

Reputation: 3325

You can try to set the button in layout file itself.

<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=".MainActivity">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:layout_weight="15"
        android:orientation="vertical">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:components="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:id="@+id/layout1">

            <Button
                android:id="@+id/one"
                android:layout_width="wrap_content"
                android:layout_height="40dp" 
                android:background="@color/colorPrimary"
                />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignParentRight="true"
                android:background="@color/colorPrimary"
                />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:background="@color/colorPrimary"
                />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:background="@color/colorPrimary"/>
        </RelativeLayout>
    </ScrollView>
</android.support.constraint.ConstraintLayout>

Upvotes: 1

Related Questions