My activity consists of a WebView and a couple buttons. I'm trying to display the buttons at the bottom of the activity, but ensure that the user must scroll through the content if it is taller than the screen. As part of my solution I have set the WebView's layout_height attribute to wrap_content.
Problem is, even though I call WebView.loadData() from within onCreate(), the WebView does not size itself right away. In the case where the web content is taller than the screen, the buttons initially appear at the bottom of the activity, and then move off-screen when the web content appears. I want to prevent the buttons from appearing at all before the web content is sized, so that when they do appear they always appear in the right place. To this end I have tried using onPageFinished (it gets called too early) and onPictureListener (it is never called at all).
How can I determine when the WebView is ready for layout?
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.webkit.WebView
android:id="#+id/wv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/btns"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:gravity="bottom" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickButton2" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Related
EDIT
I originally thought this issue was related to the search widget and keyboard.
Original Question
I added the search widget to my app recently. Now when I land on MainActivity, the view is cut right where the keyboard would pop up. The content scrolls within that smaller box. When I hit the search widget in the action bar, the keyboard pops up and fills the blank space. When I collapse the keyboard, my view is complete and there is no blank space. Check this photo to see what I mean.
Here are my layouts
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null" />
</LinearLayout>
feed_item.xml (inflates listview)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/feed_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="#dimen/feed_item_margin"
android:layout_marginRight="#dimen/feed_item_margin"
android:layout_marginTop="#dimen/feed_item_margin"
android:background="#drawable/bg_parent_rounded_corner"
android:orientation="vertical"
android:paddingBottom="#dimen/feed_item_padding_top_bottom"
android:paddingTop="#dimen/feed_item_padding_top_bottom" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="#dimen/feed_item_padding_left_right"
android:paddingRight="#dimen/feed_item_padding_left_right" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/vendorPic"
android:layout_width="#dimen/feed_item_vendor_pic_width"
android:layout_height="#dimen/feed_item_vendor_pic_height"
android:scaleType="fitCenter" >
</com.android.volley.toolbox.NetworkImageView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="#dimen/feed_item_profile_info_padd" >
<TextView
android:id="#+id/timestamp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/timestamp"
android:textSize="#dimen/feed_item_timestamp" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/txtHeadlineMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="#dimen/feed_item_status_pad_left_right"
android:paddingRight="#dimen/feed_item_status_pad_left_right"
android:paddingTop="#dimen/feed_item_status_pad_top" />
<com.example.myapp.FeedImageView
android:id="#+id/feedImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:scaleType="fitXY"
android:visibility="visible" />
</LinearLayout>
</LinearLayout>
Here's my searchable config xml.
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Let me know if you need more to go on, thanks!!
P.S. min sdk is 12.
UPDATE
The problem ended up being that the content didn't really fill the whole screen until the images were loaded from the Volley request. The text content was loaded and set the height of the listview, and subsequently loaded content did not dynamically change the listiview height. In production this won't be a problem probably, but is there a way to make sure that the ListView height can change dynamically?
It sounds like the keyboard is trying to expand when you open the activity. It is strange that it stays blank and doesn't show the actual keyboard, but maybe preventing the keyboard from opening in the beginning will help. Try setting
android:windowSoftInputMode="stateHidden"
in your <activity> tag for this Activity in AndroidManfest.xml.
try this in the manifest.
android:windowSoftInputMode="adjustResize"
and to your list view("id"),try adding
android:alignParentBottom:"true"
This ended up having nothing to do with the search widget or keyboard. the sizing was coincidental.
The problem lies in the fact that each item in the ListView had an image that was being loaded from the API via volley. the height of the ListView was being set by the content that was loaded first (i.e. text). Once the image loaded, it expanded each item in the list, but did not change the height of the parent ListView. This left the ugly gap at the bottom of the view.
On advice from #mattgmg1990, I changed the height of the ListView to match_parent in order to allow for late loading images.
In my app I am making some computations. The whole layout is inside a Scrollview.
The result is being shown on top. Xml is like that:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/back2back"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="526dp"
android:background="#drawable/back4"
android:orientation="vertical" >
<TextView
android:id="#+id/nothing"
android:layout_width="181dp"
android:layout_height="51dp"
android:layout_gravity="center"
android:textColor="#000000"
android:textStyle="bold" />
rest of the xml
</LinearLayout>
</ScrollView>
So I want when pressing the calculate button the focus of the screen to be set to the top of my page (or to be more specific in the upper textview) so the user will be able to see a result. I searched in SO but I have not found something similar.
If I understand your question, simply call:
scrollView.scrollTo(0, 0);
in your calculation Button's OnClickListener.
I'm using a Relative layout as the root or parent container and I have two buttons to place inside this Layout. The buttons need to be placed one on top of the other. The problem is that I want to position these buttons so that they appear below the center of the view but not directly below. That is within the bottom half of the view I want the buttons to appear halfway along that half portion. I tried adding a the buttons as children of a RelativeLayout (that was centered in the middle) inside the parent RelativeLayout and that sort of achieves what I'm trying to but then the Eclipse complains with a warning stating that one set of Relative Layout tags is useless and that I should consider getting rid of it.
Then I tried giving a one of the buttons a top margin with respect to its parent and then placing the other button under the this button with the top margin. This seems to work until I try it out in other virtual devices and I find out that depending on the screen sizes and dimensions it might or might not appear where I want it to (especially not the case with tablet devices).
Ok, so then I'm not sure how to achieve what I want the right way (without warnings or errors). Here's my current code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/default_real" >
<Button
android:id="#+id/sm_panel_email_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="300sp"
android:background="#drawable/info_view_email_button" />
<Button
android:id="#+id/sm_panel_web_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/sm_panel_email_button"
android:background="#drawable/info_view_web_button" />
</RelativeLayout>
How about using two layouts and the android:layout_weight attribute
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/default_real"
android:orientation="vertical" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/ >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<Button
android:id="#+id/sm_panel_email_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="#drawable/info_view_email_button" />
<Button
android:id="#+id/sm_panel_web_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/sm_panel_email_button"
android:layout_centerHorizontal="true"
android:background="#drawable/info_view_web_button" />
</RelativeLayout>
</LinearLayout>
The default screen of my app shows a ListView. When the user selects an entry a new Activity is displayed with a Title (TextView), some information gained from xml (WebView).
I want to place an Ad aligned to the bottom of this second Activity.
My layout currently is this:
<LinearLayout android:id="#+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" style="#style/info">
<TextView android:id="#+id/title" style="#style/title" />
<WebView android:id="#+id/info" style="#style/info" />
</LinearLayout>
This works perfectly, but I can't get the Ad to always be at the bottom.
I've tried RelativeLayout, but no matter what, I always get the same results!
If the HTML shown in the WebView is short enough for the AdView to be displayed (but not at bottom, underneath WebView) or its too long and the AdView is not shown at all!
I want the AdView always visible always, even if the WebView scrolls!
Hope this makes sense!
Neil
PS: I'll continue to look for answers and update if I find one....
Figured out a way to solve this, but is it the best way??
This is how I did it:
<LinearLayout android:id="#+id/linearlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:neildeadman="http://schemas.android.com/apk/res/com.neildeadman.android" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" style="#style/Info">
<LinearLayout android:id="#+id/info" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" style="#style/info">
<TextView android:id="#+id/title" style="#style/title" />
<WebView android:id="#+id/info" style="#style/info" />
</LinearLayout>
</LinearLayout>
The "layout_weight" attributes allowed me to force the inner LinearLayout to fill the remaining gap left by the add, which then contained my layout from above!
Works lovely....
So I have a webview I'd like to display as a dialog. I'd like the webview to fill the entire screen, except for a button below it that I'd like to stay at the bottom of the dialog regardless of how much content is in the webview. Currently my webview fills up the dialog just enough to push the button off the screen. I'm sure this is something pretty easy but for the life of me, I haven't been able to find the magical combination of layouts, views and attribute values to get it to play nice. Just to be clear, I've gotten it so the button floats over the webview but I'd like the webview to stop just above the button and scroll, if that makes sense.
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button android:text="Ok"
android:id="#+id/btnOk"
android:layout_width="120px"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
You will want to use android:layout_above="#+id/btnOk" for your webview, and do fill_parent for width and height of the webview.
However, it is important to note, that in 1.5 and below, RelativeLayout views need to be specified in order in your xml to be recognized correctly.. in other words, you have to have your Button first, then the WebView, since the WebView will reference the button. I think this has been changed in 1.6 or 2.0, but I am not positive which.
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button android:text="Ok"
android:id="#+id/btnOk"
android:layout_width="120px"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
<WebView android:id="#+id/webview"
android:layout_above="#+id/btnOk"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>