Get screen size in xml

1

I'd like Layout to have the screen size horizontal. But when the screen turned, it would still be the same size. I made a small sketch of how I plan to stay:

Thecurrentxmlisjust:

<LinearLayoutandroid:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="16dp"></LinearLayout>

Edited

I ended up using even the acivity no create code:

DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    int minimo=Math.min(dm.widthPixels,dm.heightPixels);
View layoutAlto = findViewById(R.id.login_layoutalto);
    layoutAlto.getLayoutParams().width = minimo;

Thanks for the help

    
asked by anonymous 19.01.2016 / 12:37

3 answers

1

I'm not going to give you a ready solution because it would be a bit extensive, and I can not find a solution to the case where the application is initially executed in landscape mode.

To get the width and height of a view that have these declared values in xml as match_parent or wrap_content use this code:

19.01.2016 / 13:15
1

The only way to do this is by changing the width at runtime. Since xml does not support math operations. No onCreate:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int minimo=Math.min(dm.widthPixels,dm.heightPixels);
View layoutView = findViewById(R.id.iddolayout);
layoutView .getLayoutParams().width = minimo;

Using the min operation, because it is smaller than the width of the layout.

    
19.01.2016 / 15:22
-2

For what you want, just use layout_weight. Here's the example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#123">
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#678">
    </LinearLayout>       

</LinearLayout>
    
19.01.2016 / 13:15