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.
5 Answers
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;
 
    
    - 841
- 10
- 28
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width  = mDisplay.getWidth();
int height = mDisplay.getHeight();
This way you get the screen size.
 
    
    - 3,496
- 23
- 32
- 
                    but i can't use them in XML file. – Branky Dec 27 '13 at 13:12
- 
                    1You can't quey screen size in xml – Oli Dec 27 '13 at 13:13
- 
                    there is no alternative? – Branky Dec 27 '13 at 13:15
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.
 
    
    - 3,834
- 4
- 33
- 45
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
 
    
    - 2,934
- 2
- 27
- 40
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
 
     
     
    