How to set config changes only for one linear/relative layout? - android

In my layout xml file I have webview and LinearLayout.
when I open softinputmethod for webview, webview editText window(like google), is up to keyboard and that is good.
But when keyboard is open, my linearLayout is so squeeze, that all button looks terrifying.
How to set, that keyboard can change webview, but it can't destroy RelativeLayout?
there is some code:
<RelativeLayout
android:id="#+id/homebar"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
>
...//there is some buttons
</RelativeLayout>
<WebView
android:id="#+id/wv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/homebar" />
[sorry for my bad english]

LinearLayout is not a good way to handle the views when dynamic View is expected to get loaded in activity afterwards. Do use relative layout to solve this problem out.
<RelativeLayout 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"
tools:context=".MainActivity" >
</RelativeLayout>

Related

Android not able to scroll up

I have a login layout with two EditText and a Button. When I'm typing some text in EditText, as the keyboard open it overlap the some of my layout. I try to scroll up but not able to scroll up layout to be visible. I'm using Relative Layout to create it. How to solve it?
Thank you.
Place your EditTexts and Button inside a ScrollView. Whatever is inside ScrollView can be scrolled. So your problem will be solved.
Note that ScrollView can host only one child. So you have to place all your Views inside a ViewGroup like LinearLayout or RelativeLayout
Although Julia Zhao's answer is correct, it uses RelativeLayout. If you use RelativeLayout you have to do more work to make your Views appear one below the other. So I suggest you to use LinearLayout with android:orientation="vertical" inside it. It will automatically place one View below the other without any extra effort. So you won't issues like one View overlapping on other.
<?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="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="UserName"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="Password"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Login"/>
</LinearLayout>
</ScrollView>
Try nesting everything in a ScrollView, hope this helps.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#id/RLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
xmlns:android="http://schemas.android.com/apk/res/android"
>
// Your code
</RelativeLayout>
</ScrollView>

Softkeyboard hides elements of my layout that I want to be visibles

I've got a layout that looks like first image and when I open the softkeyboard looks like the second image, but I want it how it seems on third image:
I read a lot about adjustpan and adjustresize on the manifest but nothing works, the xml structure is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blanco"
android:gravity="top"
android:orientation="vertical"
android:weightSum="10" >
<!-- LOGO -->
<LinearLayout
android:id="#+id/logo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.7"
android:background="#color/green_logo"
android:orientation="vertical"
android:gravity="center"
android:paddingTop="65dp" >
<ImageView
android:id="#+id/logo_registro"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/logo_registro" />
</LinearLayout>
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.3" >
</FrameLayout>
</LinearLayout>
First part is the green part with the app logo and inside the FrameLayout I put Fragments that contains simple LinearLayouts or RelativeLayouts with parts of my Login forms.
Put this on your Manifest, inside the activity
<activity android:name=".YourActivity"
android:configChanges="keyboard|keyboardHidden"></activity>
Set your windowSoftInputMode in your manifest for this activity to be adjustResize. THere are 3 possible settings for this- nothing, resize (shrinks your app to fit the remaining space), and pan (scrolls your app so the focused field remains on screen).
Just Add the empty scrollview and it will work
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>

Android layout design: image above form above button. When the keyboard appears my layout is screwed

In my application I want to have a layout like this:
I did it with a Relative Layout holding an ImageView, TableLayout and a Button. The problem is that when I open the keyboard to modify a EditText the keyboard is on top of the EditText, so the user can't see what he is writing. I want the layout to be like this when the keyboard is shown:
The user should be able to scroll up the view to see the whole image while the keyboard is shown, even if he doesn't see the EditText then, because he wants to copy some data of the image into the EditTexts. I tried to do it like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/buttonEditStop"
android:padding="10dip" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/stopImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:clickable="true"
android:contentDescription="#string/entry_img"
android:onClick="ImageClick" />
<TableLayout
android:id="#+id/formLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/stopImg"
android:layout_marginRight="10dp" >
...
</TableLayout>
</RelativeLayout>
</ScrollView>
<ImageButton
android:id="#+id/buttonEditStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:contentDescription="#string/edit_stop"
android:onClick="EditButtonClicked"
android:src="#drawable/ic_menu_edit" />
Using this code my whole layout is screwed. I see the form on top of the image, followed by a ImageButton with the image that should be seen in the imageView.
Anyone knows how I can get a layout like the one I want?
Thanks!
To avoid the EditText to be overlapped I had to use
android:windowSoftInputMode="adjustPan"
in my manifest.

WindowSoftInputMode and ScrollView

My Android app's main activity looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:orientation="vertical"
android:paddingLeft="60dp"
android:paddingTop="53dp" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/billAmountText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/billAmount_string"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/billAmount"
android:layout_width="173dp"
android:layout_height="wrap_content"
android:layout_below="#id/billAmountText"
android:layout_marginTop="3dp"
android:inputType="numberDecimal" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In order to make sure the background image (defined in the first RelativeLayout) does not get squeezed when the keyboard pops up, I have set android:windowSoftInputMode="stateVisible|adjustPan in the manifest file for my activity. All good with the background and layout now, nothing gets resized.
But the problem is that my ScrollView doesn't work now because I suppose the system thinks the window doesn't need resizing due to my above modification to the manifest - so the ScrollView is not activated. The opposite happens when I take android:windowSoftInputMode="stateVisible|adjustPan out, the scrolling works, but the background gets squeezed when the keyboard pops out.
Is there any way to code it so that my background doesn't get squeezed, and my ScrollView still works? Thanks!
Try to use in your activity the property windowSoftInputMode with the following values:
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
I don't know if it's necessary but you can try to add to your layout's ScrollView tag the fillViewPort to true.
So it should look like something I show you below:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<!-- Other Views -->
</ScrollView>
Let me know about your progress.
In your scrollview put a relative layout, child 0 is an imageview, and child 1 is your content. Use the imageview for your background image (src) rather than the background of the scrollview. The imageview will respect scaleTypes, whereas the background of a layout will not.

horizontalscrollview's fillviewport disables scrolling?

I have this layout, and it's scaled weirdly (probably due to inadviseable layout nesting) if I do not use fillviewport=true in my HorizontalScrollView.
Everything works peachy (except for the odd scaling) when fillviewport=false, but when fillviewport=true, the scaling is perfect but no scrolling happens.
This is the layout (Note: I know you're not supposed to put a webview in a scrollview. But webview doesn't have a smoothscrollto nor expose a setscroller method, so ... bleh.)
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webContainer"
android:layout_below="#+id/titlebar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webZ"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</HorizontalScrollView>
Setting android:fillViewport="true" doesn't usually disable scrolling in a view, does it?
It's just supposed to be sure the scrollview fills the viewport regardless of the size of its contents, eh? I think the viewport is the visible area of the screen, and there is definitely more content off the edge of the visible area in the webview, I just can't scroll to it anymore.
I can see from logcat that the scrolling methods are being called, they just don't change the screen. (Unless I set fillviewport to false.)
The problem is that you use "fill_parent," which means "be as big as my parent." Use wrap_content for the width instead.
I encounter a similar problem with a standard ScrollView containing a WebView: the scrolling was not working anymore. Changing of fill_parent to wrap_content did not worked for me.
The example of Romain Guy is working fine when the expanded view is a TextView but is not when it's replaced by WebView.
What is not working:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1.0">
</WebView>
<Button
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Click">
</Button>
</LinearLayout>
</ScrollView>
When I add a small amount of data in the webview it seems ok:
But when I fill the WebView with a lot of data, it does not work as you can see below:
The button is always displayed and the scroll does not work anymore. In fact, the touch scroll does not work and the DPAD scroll works...
I did not found any reason for that but I found a workaround: it consist in adding a LinearLayout containing the WebView
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout android:id="#+id/linearlayout"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1.0"
android:orientation="vertical">
<WebView android:id="#+id/webview"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</WebView>
</LinearLayout>
<Button
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="Click">
</Button>
</LinearLayout>
</ScrollView>
Now it works fine:

Categories

Resources