Take screen size in xml

Asked

Viewed 1,282 times

1

I would like Layout to have the size of the screen horizontal. But when the screen turned, it would still be the same size. I’ve made a little sketch of how I intend it to look:

Exemplo

The current xml is only :

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

Edited

I ended up using even the code in the create da acivity:

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

  • What do you mean? The image doesn’t appear here for me.

  • http://i.stack.Imgur.com/UPS1Z.png

  • I still can’t see :/ Let me see if I understand: you want the layout to be the same size, regardless of the orientation of the canvas (landscape or portrait)?

  • The width at least. In portrait mode it fills the width, in landscape mode it does not change the width.

  • Do you want it to look like it is in the sketch or do you want it to look like a landscape? Post xml.

  • @Rodrigosantiago as is your layout file currently?

  • I got the same question @ramaral

  • I want it to look like in the sketch! I didn’t post the xml pq and only a layout with fill_parent.

  • 1

    The correct is that the width of the layout adapts according to the orientation of the device. This width can be different according to the various models of devices we have on the market. If you really need the layout according to the sketch, you will have to take the dimension of shorter length and configure it in the runtime layout_width. Let your need be known, maybe we can help you.

  • Rodrigo, if you managed to solve the problem, post the solution as an answer just below and mark as "accepted".

Show 5 more comments

3 answers

1


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

To obtain the width and height of a view which have these values stated in xml as match_parent or wrap_content use this code:

private final int largura;
private final int altura;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    ....
    final View aSuaView = findViewById(R.id.aSuaView);
    aSuaView.getViewTreeObserver().addOnGlobalLayoutListener(new 

        ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //Remove o listenner para não ser novamente chamado.
                aSuaView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

                //Coloca a largura igual à altura
                largura = aSuaView.getWidth();
                altura = aSuaView.getHeight();
            }
        });
}

To know if the device is in landscape mode use:

getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE

You must keep the value of the width that the view has in portrait mode and when to pass the landscape mode assigns it to her.

To save the value you can use the savedInstanceState.

1

The only way to do this is to change the width at runtime. Since xml does not support mathematical operations. In 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 dimension that will be the width of the layout.

-2

All you want to do is use layout_weight. Follow 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>
  • From what I understand, the red line is not dividing the layout but showing the width.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.