I'm using scrollview to scroll my view up when soft keyboard open. But it's make more than required space between button and keyboard.
Can we remove this large space.
Here is my xml code block.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget32"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/searchText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/searchBtn"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginBottom="20dp"
android:hint="#string/search_hint"
android:inputType="number"
android:maxLength="10" />
<Button
android:id="#+id/searchBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#339933"
android:padding="20dp"
android:text="#string/search_button_text"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
To align the click button with the software keyboard, it should be at the layouts bottom.
You should have the click button within the root layout, which should have the click button aligned to bottom, either with linearlayouts gravity or relativelayouts layout_alignParentBottom
So basically you define the layout as follows:
<RelativeLayout>
<Button
android:layout_alignParentBottom="true">
</Button>
</RelativeLayout>
And ta-da! the button is right on top of the software keyboard!
The real problem..
As it turns out in the comments the real problem here is to align the button to the keyboard only when it is visible, which can simply be achieved by having two invidual buttons that visibilitys are toggled based on the keyboard state
So first we define a layout that has two buttons, one for the software keyboard state and one for the "regular" state
<RelativeLayout>
<ScrollView>
<LinearLayout>
<!-- ...... -->
<Button
android:id="#+id/regular_button">
</Button>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/keyboard_aligned_button"
android:layout_alignParentBottom="true">
</Button>
</RelativeLayout>
and now we just hook the keyboard state to the visibility of the buttons, so we need in some way to keep track of the keyboard state, which is explained in many places
and when the keyboard is set to be visible we setVisibility of the regular button to be GONE, and keyboard aligned button to VISIBLE.
After a keyboard is gone visibilities are inversed
Related
My app design makes me having this kind of layout for every form view: a floating button at the bottom of the view. I'm using ConstraintLayout to set dynamically the height of the button with always the same left/right margin no matter the screen width, and so I end up having this layout:
<ConstraintLayout>
<ScrollView>
<EditText/>
<EditText/>
<!-- ... -->
<ConstraintLayout /> (1)
</ScrollView>
<Button/>
</ConstraintLayout>
(1) A clear view with the height of the bottom button in order not to hide the views at the bottom in the scroll view that would be hidden by the button
Basically, that's what it looks like:
Now the problem I encounter is when I tap an edit text at the bottom, for example here the 4th one:
The edit text moves up the keyboard, but not the floating button, and it often comes to be hidden by it. I know that I have to do something in the edit text's onFocusChanged() method, but I don't know what...
I know that I have to do something in the edit text's onFocusChanged()
method.
No. You don't have to do things manually. Simply add this attribute to the button. This will make the button positioned on the bottom of the parent layout rather that on the bottom of the EditText (You can skip this if are already doing it).
app:layout_constraintBottom_toBottomOf="parent"
And add this attribute to the activity tag which represent that specific activity. It will reduce the height of the viewable area by an amount which is equal to the height of the soft keyboard in such a way that it won't go behind the content.
android:windowSoftInputMode="adjustResize"
See the doc for more detailed explanation.
Also note that a ScrollView can host only one direct child as in the below example.
<ScrollView
...>
<LinearLayout
...>
<!-- You can place multiple views here -->
</LinearLayout>
</ScrollView>
1- you can move your edit text to top of the scrollView when they focus changed to true :
here is code :
java Code :
LinearLayout containerOfScrollViewViews= findViewById(R.id.containerOfScrollViewViews);
for (int i = 0; i < containerOfScrollViewViews.getChildCount(); i++) {
containerOfScrollViewViews.getChildAt(i).setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
int[] xy = new int[2];
v.getLocationInWindow(xy);
if (hasFocus) ((ScrollView)findViewById(R.id.scrollView))
.smoothScrollBy(xy[0], xy[1]);
}
});
}
My XML :
<?xml version="1.0" encoding="UTF-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
app:layout_constraintTop_toBottomOf="parent"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="#id/btn">
<LinearLayout
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:hint="A"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="B"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="C"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="D"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="E"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="F"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="H"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="J"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="K"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
<EditText
android:hint="Z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="add"
app:layout_constraintBottom_toBottomOf="parent" />
</android.support.constraint.ConstraintLayout>
2 - or your can attach your button to bottom of the scroll view so when keyboard appears than scrollView and button both goes above the scrollView
3- you can listen to keyboard visibility Chang and hide/show your button
here is link for this solution :
SoftKeyboard open and close listener in an activity in Android?
AndroidManifest.xml add android:windowSoftInputMode="adjustPan|stateHidden"in your activity ;
Or, setKeyboardListener, changed your scrollview position
Use releative Layout and put in scrollview android:layout_above="floating button"
this should solve overlaping or hiding by that button.
also softinput adjust pan
Scoll view should contain only one view
so put all edit text inside a linear or any layout inside scroll view
<android.support.constraint.ConstraintLayout>
<ScrollView .........>
<LinearLayout..........>
<EditText ....... **//Add EditText around 6 to 7**
..............
.............. />
</LinearLayout>
</ScrollView>
<RelativeLayout **// Fix the button in bottom gravity.**
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bottom Gravity" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
And please set androidmanifest.xml file
<activity
android:windowSoftInputMode="adjustPan"/>
I've solve this problem hope so it will help you.
First off all let me say that I've tried in the manifest android:windowSoftInputMode="adjustPan|stateHidden" and android:windowSoftInputMode="adjustResize|stateHidden" and the result is the same.
I have a layout with an Header and a Footer and in between I have an ScrollView. Inside this, I have a RelativeLayout and inside this layout, I have one EditText and a LinearLayout containing another EditText (this layout makes sense in my app).
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scroll"
android:layout_below="#+id/header"
android:fillViewport="true"
android:layout_above="#+id/footer">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="40dp">
<EditText
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/edittextback"
android:layout_marginBottom="40dp" />
<LinearLayout
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/one"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone"
android:layout_marginBottom="40dp">
<EditText
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextback"
android:gravity="center"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
When I enter the activity and start entering text in the first edittext, the keyboard pops up, the scroll goes up a little so the I can see the text that I'm inserting. The problem is that the edittext is not all visible (I mean the bottom boundary). The keyboard stays just bellow the inserted text and not bellow the bottom of the edittext.
Now, since I already have the keyboard open, I scroll a bit so that I can see the top of the second edittext. When I touch the second edittext, it gains focus, and now, it automatically scrolls a bit not like the first edittext, that is, just enough to see the inserted text, but to the bottom of the edittext making the edittext all visible. This is what I want.
How can I make the behavior to be show always all the edittext?
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.
I have a vertical LinearLayout with 3 Buttons vertically aligned one below the other. In between each button, i have set a LinearLayout whose visibility is GONE initially. When the 1st button is clicked, the LinearLayout beneath it is changed to VISIBLE and the list view which i have defined in that layout comes in view while the remaining 2 Buttons remain attached to the LinearLayout.
The problem is that if the list view within the LinearLayoutt (which i have added using java code) is too big, then the 2 Buttons below the 1st one go out of view. What remains is the layout beneath the 1st button.
I want the 2 Buttons to be bound to the view. Any help will be appreciated. Thanks.
Here is my layout:
LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="350dp"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/buttonContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contacts"
android:background="#drawable/button_style"
android:textColor="#E6E6E6"/>
<LinearLayout
android:id="#+id/layoutContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<Button
android:id="#+id/buttonLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call Logs"
android:background="#drawable/button_style" />
<LinearLayout
android:id="#+id/layoutLog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" >
</LinearLayout>
<Button
android:id="#+id/buttonVm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Voicemail"
android:background="#drawable/button_style"/>
<LinearLayout
android:id="#+id/layoutVm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
</LinearLayout>
If i understand your problem correct, you want the last buttons to be in the view irrespective of the list height. Then i would suggest you to use weight for the hidden LinearLayout, use android:layout_weight=1 to achieve your requirement.
android:scrollbars="true"
Try putting scrollbars
I am using following code to display button at the bottom of activity.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
/>
</RelativeLayout>
and listview above it. when i display more data in listview this button pannel is moved down.can any one guide me how can i fix it at the bottom of activity?
any help would be appreciated.
The answer selected as correct is faulty, the button will hide the lower part of the list view. The correct way is to declare the button first and position the list above the button.
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true"/>
<ListView
...
android:layout_above="#id/btnGetMoreResults"/>
The android:layout_alignParentBottom attribute has to be declared in an element of the RelativeLayout not in the RelativeLayout himself (unless there is another RelativeLayout as a parent).
You should do something like this, with the ListView inside the RelativeLayout also :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ListView ...>
<Button android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Get more"
android:layout_alignParentBottom="true" />
</RelativeLayout>
If you had, for example, all the scrollable elements in a ScrollView, you should do like the following:
<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"
style="#style/rootElement"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- texts, buttons, images and anything that you want to scroll -->
</ScrollView>
<LinearLayout
android:orientation="vertical"
style="#style/footer"
android:id="#+id/footer"
android:layout_alignParentBottom="true"
>
</LinearLayout>
</RelativeLayout>
Note that if you want the footer to be fixed, then you shouldn't put it in the ScrollView, where the scrollable content will be placed. Make it child of RelativeLayout and set layout_alignParentBottom to true. Maybe you'll need to add a padding at the bottom of the ScrollView in this case (so that the last element do not get hidden by the footer).
The idea is similar for elements other than ScrollView