Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android get screen size in XML

Tags:

android

layout

Hey I have a scrollview from the top of the screen to a bit before the bottom of the screen because this is where my ad banner takes place. I want to have some pixels in between these elements ( between buttons and banner ) due the google policy. So far I did this with setting a value in dp. But as devices differ, this results in having a huge gap between the ad and the scrollview on some hi-res phones. This is why I'd like to set

<ScrollView 
android:layout_height="(Screen_height-banner)-5px"

(of course I didn't even try it in this way, as this is no code, but I think it sums up pretty well what I plan to do)

Thanks a lot for your answers!

like image 250
user2875404 Avatar asked Oct 29 '13 10:10

user2875404


3 Answers

You can't query screen size in XML since it doesn't run at runtime, you can do this by using RelativeLayout and use alignments and margins.

in your case if Screen_height-banner represent a screen height and you want to position it at bottom and make a 5dp separator from end your XML would be

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

        <ScrollView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginBottom="5dp">

</RelativeLayout >

If you need to scale the scroll view instead of position it you xml would be

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding_bottom="5dp">

        <ScrollView 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent">

</RelativeLayout >
like image 154
Ahmad Dwaik 'Warlock' Avatar answered Oct 13 '22 04:10

Ahmad Dwaik 'Warlock'


You can user weight parameter. If you set weight of both layouts 0.5, this will share screen width equals for these layouts.

 <LinearLayout
    android:id="@+id/alt_panel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/orta_panel" >

    <LinearLayout
        android:id="@+id/altsol_panel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


    </LinearLayout>

    <LinearLayout
        android:id="@+id/altsag_panel"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center_horizontal"
        android:orientation="vertical" >


    </LinearLayout>
</LinearLayout>
like image 38
Muzaffer Onur DAĞHAN Avatar answered Oct 13 '22 03:10

Muzaffer Onur DAĞHAN


Use a RelativeLayout, set a Margin of 5px and use layout_above

like image 23
meredrica Avatar answered Oct 13 '22 03:10

meredrica