Android: EditText on top of WebView? - android

I would like to imitate the behavior of the default android browser - The EditText (the address bar) on top will start to "disappear" once the user scrolls down. The only thing I can think of right now is to put the EditText and the WebView in a ScrollView but that would create other problems, and is not recommended.
What do you think?

Create a custom View by extending WebView and override onScrollChanged(). With a little calculations you can accomplish your task.

What problems does your ScrollView cause? This one should work fine:
<?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:stretchColumns="0" android:orientation="vertical">
<ScrollView android:layout_width="fill_parent" android:layout_gravity="top" android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="0" android:orientation="vertical">
<EditText android:layout_width="fill_parent" android:id="#+id/editText1" android:text="yourtexthere" android:layout_gravity="bottom" android:layout_height="wrap_content">
</EditText>
<WebView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="#+id/myWebView"></WebView>
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

Android Webview with child at bottom

I've been searching both Google and stackoverflow for a while now and haven't found anything answering my question.
I'm wondering about putting a LinearLayout (with no specific known height in pixels) at the bottom of a WebView. This LinearLayout will later be populated with a fragment containing comments for the article displayed in the WebView.
The issue of the problem is that the WebView is almost always larger than the screen so the user has to scroll to read the full article. At the bottom of the scrolling I want the CommentsFragment to be inserted (through the LinearLayout, it takes a few parameters so it can't be loaded directly from the XML).
I've tried a bunch of solutions but all of them make the LinearLayout stick to the bottom of the layout the all time, not on the bottom of the WebView.
My current code is the following, of course not working:
<?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" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Thank you for your help!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/comments_container"
android:layout_below="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
got it working after a few rounds. The solution was using a ScrollView then inside that a relativelayout mirroring #Karan_rana's answer.
If you want to put the comments fragment below web view then you can try as following and tell me if it works for you
<?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" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/comments_container"/>
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try this, make sure to have layout_height="wrap_content" in your WebView
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<WebView
android:id="#+id/article_webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/comments_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>

ScrollView does not respond to keyboard

I have a problem with EditText scroll behaviour in ScrollViews, normally it scrolls up content inside scroll if keyboard opens over it. But if you fill the layout inside the scrollview dynamically, keyboard push up whole screen, anyone else had this problem before.
Thanks in advance.
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header_area" >
<LinearLayout
android:id="#+id/contentHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
This sample works normal, linear layout scrolls in scroll view but it shows scrolls even keyboard closed. I can not use fixed heights.
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:layout_below="#+id/header_area" >
<LinearLayout
android:id="#+id/contentHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
I have tried xml below, it works as expected on a new project but push up window in my project. I have found some other people having this problem, I'll share solution asap i found it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header_area"
android:layout_width="match_parent"
android:layout_height="50dip" >
</RelativeLayout>
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/contentHolder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
<application
android:icon="#drawable/is_logo_iphone"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
Problem is "Fullscreen" theme does not support resize. Using
android:theme="#android:style/Theme.Black.NoTitleBar"
solves the problem.
Thanks for answers.
That is because your scrollview is resizeable. Try to set the height of your scrollview to match_parent instead.
Try also setting android:fillViewport="true" inside your scrollview.
Also you wouldn't happen to have added something like following within your activity tag of your manifest: android:windowSoftInputMode="adjustResize" That would pretty much ruin all our effort. If you have following within the manifest remove it.
Your probably have to configure your manifest. Here is a list of possible configurations for soft keyboard. It might be android:windowSoftInputMode="adjustPan" you want to use.

Can't display my button at the bottom of a list of layouts

I have spent hours on this problem, I hope somebody can help me.
Sorry in advance if the question is stupid, I am fairly new to android development and also, I am using an app builder which has generated already a lot of code. I am basically trying to tweak this code with what I want...
The question is just about xml layouts, I don't think it involves code. I want to display a simple button at the bottom of this screen.
So I have this screen which builds a group of buttons in a grid. I also display an ad banner at the top. The parameters of these buttons are specified through the web app builder on a web interface. No problem with these buttons. They are displayed correctly in this screen layout below:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/containerView"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="123"
ads:adSize="SMART_BANNER"
ads:testDevices="123"
ads:loadAdOnCreate="true">
</com.google.ads.AdView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/verticalScrollView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TableLayout>
</ScrollView>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerViewHorizontal"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerViewHorizontalBottom"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/horizontalScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/containerHorizontalButtons"
android:background="#android:color/transparent"
android:padding="0dip"
android:layout_margin="0dip"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
Now what I am trying to do, is to add at the bottom of this group of buttons, a button that does not belong to the list generated through the web app builder, but manually created:
<Button
android:id="#+id/bmenu2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
I have already taken care of the code for this button, it works well, but I don't manage to display it at the bottom of my screen.
I guess it has to do with the general layout of the screen. I have to admit I am completely lost in the series of layout nested in one another, I don't understand what is exactly doing what.
If that helps, I have managed to display my button at the top of the screen, even just below my ad banner. But not at the bottom.
Also if that helps, I guess one of the layout takes up the whole heightspace (the scrollview?), living 0 height for the button I want to add...
Thanks a lot for your time and if you can help me!
Juyt implement a Linear layout instead of a relative one for the list and the button.
Then give the list a height of 0dp and a weight of 1
The button will have whatever attributes:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

Button at Bottom

Im not very good in creating android layouts so I am not able to align the button to the bottom in the MainView.
Picture:
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<ListView android:id="#+id/lv_pizza" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"></ListView>
<Button android:layout_width="fill_parent" android:id="#+id/bt_add"
android:layout_height="wrap_content" android:text="hinzufügen"></Button>
<RelativeLayout android:layout_width="fill_parent" android:id="#+id/relativeLayout2" android:layout_height="fill_parent">
</RelativeLayout>
</LinearLayout>
Please help
This works for a linear layout, note the layout_weight 1 on the list -- that's what pushes the button to the bottom:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView android:id="#+id/lv_pizza"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button android:id="#+id/bt_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hinzufügen" />
</LinearLayout>
This then looks like this in the UI editor:
Hi you can set like this
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<ListView android:id="#+id/lv_pizza" android:layout_width="fill_parent"
android:layout_above="#+id/bt_add" android:layout_height="fill_parent" ></ListView>
<Button android:layout_width="fill_parent" android:id="#+id/bt_add"
android:layout_gravity="bottom" android:layout_height="wrap_content" android:text="hinzufügen"></Button>
</RelativeLayout>
Removing the RelativeLayout will push the button to bottom of the screen...
Use a layout_weight for the components inside the LinearLayout. If the ListView has a weight of 1.0, it should push the button to the bottom. I've done this with a ScrollLayout and it works well, allowing the component inside the scroll layout to actually scroll. You could put your ListView into a ScrollLayout to let it grow beyond the visible size.
Also, the second relativelayout also isn't necessary as it doesn't add anything.
You can set a marginTop in the Button until the bottom or change the layout to relative and set the position.

ScrollView Not Responding

I have the following XML and am not doing anything fancy with overriding scrolling behaviors:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/myscrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:layout_weight="1">
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
<TextView
android:id="#+id/my_empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/my_foo"
android:gravity="center"
android:visibility="gone"
/>
</LinearLayout>
However, while the embedded listview does respond to presses and long presses, it does not scroll. What am I doing wrong?
The trailing TextView is the listview's emptyview.
ScrollView does not play nicely with any other view that scrolls natively, i.e WebView, ListView, etc.
Try a Relative layout and see if this does what you need.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/mylistview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alingParentTop="true"
/>
<TextView
android:id="#+id/my_empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_foo"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
In android it should not put ListView inside a ScrollView. It will not work.
Take the ListView out of the ScrollView. It causes problems, and is unnecessary (ListView handles scrolling on its own.)

Categories

Resources