-1

Is there a way to get width and height of an android device and use them in the XML file? my goal is to make a dynamic application both for small and big screens.

Sandesh
  • 1,190
  • 3
  • 23
  • 41
Branky
  • 373
  • 8
  • 23

5 Answers5

2

u can do it programatically though and use in code.

DisplayMetrics displaymetrics = new DisplayMetrics();
    context.getWindowManager().getDefaultDisplay()
            .getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    int screenWidth = displaymetrics.widthPixels;
Ana
  • 841
  • 10
  • 28
1
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width  = mDisplay.getWidth();
int height = mDisplay.getHeight();

This way you get the screen size.

Oli
  • 3,496
  • 23
  • 32
1

If you set the height and width as fill parent. Then it will take the whole device screen height and width.

Like this:

android:height="fill_parent"
android:width="fill_parent"

Edited:

If you want to use like 20%. Then make the android:weightSum of parent layout to 5.0 and child parent layout:weight="1.0" like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/main_rel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="5.0" >

        <RelativeLayout
            android:id="@+id/child_one"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#0000FF" >
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/child_two"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1.0"
            android:background="#00FF00" >
        </RelativeLayout>

    </LinearLayout>

It will set the layout fit to every screen size.

Avijit
  • 3,834
  • 4
  • 33
  • 45
0

Create folders inside res folder by the name of values-mdpi, values-hdpi, and so on. Android will load the correct file at runtime. You can create many types of file for reference Link

Hussain Mansoor
  • 2,934
  • 2
  • 27
  • 40
0

You can calculate display size using following code . you can find it Here- get screen size in pixels

and then Make different layout according to your sizes . It is Explained Here- resize-your-image-according-to-screen-sizes

Hope This will help you

Community
  • 1
  • 1
Straw Hat
  • 902
  • 14
  • 38