user2875404
user2875404

Reputation: 3218

Android get screen size in XML

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!

Upvotes: 16

Views: 36147

Answers (4)

Valn1
Valn1

Reputation: 169

If the following codes don't work for you, you're probably using a newer version of something, and you could try using android:bottom="80dp" instead of android:layout_marginBottom="5dp"

Upvotes: 0

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 >

Upvotes: 5

Muzaffer Onur DAĞHAN
Muzaffer Onur DAĞHAN

Reputation: 71

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>

Upvotes: 4

meredrica
meredrica

Reputation: 2563

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

Upvotes: 2

Related Questions