Bennet
Bennet

Reputation: 11

How to change the height of a webview on Runtime?

I have a webview and a table containing 4 buttons. I have set the height of the webview to 400px, so that the buttons are displayed at the bottom of the screen, in portrait mode. My problem is, when changed to landscape mode the buttons are not visible. So I need to change the height of the webview on runtime, when orientation is changed. Do anybody know how to achieve this? I am able to capture the orientation change, using 'onConfigurationChanged' function.


Thanks CommonsWare, I gave the xml as follows. The application crashes on launch.

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

<RelativeLayout android:layout_alignParentTop="true">

<WebView
    android:id="@+id/appView"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" />

</RelativeLayout>
<RelativeLayout
    android:layout_alignParentBottom="true">

<TableLayout
    android:id="@+id/TableLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

<TableRow
    android:id="@+id/TableRow01" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

<ImageButton
    android:id="@+id/more"
    android:padding="1dip"
    android:src="@drawable/more1"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/albums"
    android:padding="1dip"
    android:src="@drawable/albums1"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/videos"
    android:padding="1dip"
    android:src="@drawable/videos"
    android:layout_marginRight="15dip">
</ImageButton>
<ImageButton
    android:id="@+id/artists"
    android:padding="1dip"
    android:src="@drawable/artists1"
    android:layout_marginRight="15dip">
</ImageButton>
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>

Upvotes: 0

Views: 3585

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

Design your layout to avoid the hard-wired value of 400px. Not only will that not work in landscape mode on whatever emulator/device you are testing on, it will not work on smaller or larger screens the way you expect.

Please use a RelativeLayout, with attributes like android:layout_alignParentBottom for the buttons and android:layout_alignParentTop and android:layout_above for the WebView. Then, your layout will dynamically adapt to the available space.

Upvotes: 4

Related Questions