Android Move the whole screen upwards when softkeypad is opened? - android

i just want to shift my whole activity screen upwards when the user clicks on the edit text...
i mean the screen will shift upwards and below it we can see the soft keypad...
I have tried using android:windowSoftInputMode="adjustPan|adjustResize" in my Manifest file but it doesn't make any sense..
thanks in advance ...

In your xml layout. Make the scrollview to be root parent, and it will work!
Try this to detect if keyboard is up, and then scroll down (you need your scroll to be root in xml):
final View activityRootView = findViewById(R.id.scroll);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
//keyboard is visible
if (heightDiff > 100) {
scroll.fullScroll(ScrollView.FOCUS_DOWN);
}
}
});

Related

How to hide footer component that appear on keyboard's top when editText gets focused in fragment?

Fragment layout
Actually the footer is set inside an activity class and the edittext is placed inside a fragment.
manifest file
<activity
android:name="HomeController"
android:label="#string/app_name"
android:windowSoftInputMode="stateHidden|adjustPan"/>
Inside my fragment class I added
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
These are the code snippet that I used, but it won't work!
Note: in Fragment UI, the whole screen has a scroll view,
Don't know why it is happening like this?Actually I don't want my footer on the keyboard's top.
Any suggestions on how to solve this behavior? And its appriciatble for the replies :)
Add this to your activity.
I did not test, but this might work. it is a bad way though(
contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
View rootView = getWindow().getDecorView().getRootView();
rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView.getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.d(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
//set bottom navigation(footer) bar to View.GONE
}
else {
// keyboard is closed
//set bottom navigation bar(footer) to View.VISIBLE
}
}
});
Use android:windowSoftInputMode="hidden" in your parent layout in the xml file. also in case it has layoutAbove property then try to remove it.
Try to add the following in manifest file:
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
Also remove the following from fragment:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

Android : How to prevent any View from being hidden by Soft Keyboard?

I have 4 lines of input fields in my Layout.
The problem is that when i click the uppermost EditText, soft keyboard appears and hides the lowermost EditText.
My Goal is to keep every EditText fields visible in screen when soft keyboard pops up.
I tried windowSoftInputMode in Manifest but that's not what I wanted, it just keeps the 'focused' input field visible.
Is there any flags for View type to prevent hiding from soft keyboard, so that the screen always scrolls to show the specified Views?
You can put 4 lines of input fields into a linearlayout view then put this linearlayout into a scrollview, when soft keyboard appears, you scroll the scrollview to the last EditText view position.
int[] loc = new int[2];
lastEditTextView.getLocationOnScreen(loc);
the method to detect keyboard appears:
private ViewTreeObserver.OnGlobalLayoutListener mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
if (activityRootView == null) {
return;
}
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100) {
// keyboard show
scrollView.smoothScrollTo(loc[0],loc[1]);
} else {
// keyboard hidden
}
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);

Android How To hide/show some view when keyboard show/hide?

just like the picture, when the keyboard is show ,it hide the logo.
the question is how to listen the keyboard show/hide even? have some sample?
add this attribute to your activity in manifest
android:windowSoftInputMode="adjustResize"
Get a reference to the layout which you want to hide when keyboard pops up. You can set the visibility of that to GONE when keyboard is shown and to VISIBLE otherwise. So your task now is to detect whether the keyboard is shown or hidden. For that you can use ViewTreeObserver.OnGlobalLayoutListener().
rootView = getWindow().getDecorView().getRootView();
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect rect = new Rect();
rootView.getWindowVisibleDisplayFrame(rect);
int screenHeight = rootView.getHeight();
int keyboardHeight = screenHeight - (rect.bottom - rect.top);
if(keyboardHeight > screenHeight / 3){
//hide the layout
}
else{
//show the layout
}
}
});

What is a good way to pan view a little more up when soft keyboard is shown

I have a activity that has the "adjustPan" option set and it pans my view up just fine when a EditText receives focus and the soft keyboard is shown, but my problem is that there are UI elements below my EditText that needs to be visible also when the keyboard is shown and it is not on smaller screens. Can I some how tell the view to always pan so that those UI elements are visible by linking them to the EditText or something, or do I have to take control of the panning manually and calculate height and all, or what is the good solution here?
What you can do is you can use ViewTreeObserver to pan your content l'il bit more.
I did this is my app.
int edtHeight = 0;
// llContent is the Viewgroup layout which is inside a ScrollView.
ViewTreeObserver viewTreeObserver1 = llContent.getViewTreeObserver();
viewTreeObserver1.addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
// Here edtHeight will get the height of edittext as i wanted my view to scroll that much bit more
edtHeight = edtTemprature.getHeight();
ViewTreeObserver obs = llContent.getViewTreeObserver();
obs.removeGlobalOnLayoutListener(this);
}
});
//llMain is the parent View Group of my XML layout
llMain.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
#Override
public void onGlobalLayout()
{
Rect r = new Rect();
// r will be populated with the coordinates of your view
// that area still visible.
llMain.getWindowVisibleDisplayFrame(r);
int heightDiff = llMain.getRootView().getHeight() - (r.bottom - r.top);
if (heightDiff > 100)
{ // if more than 100 pixels, its probably a keyboard...
if(edtTemprature.hasFocus()){
llContent.scrollTo(0, (int) (edtHeight * 1.5));
}else{
llContent.scrollTo(0, 0);
}
}
else
{
llContent.scrollTo(0, 0);
}
}
});
Use stateHidden in Android manifestfile which class you need ..
android:windowSoftInputMode="stateHidden"
or else,
In Activity,
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Scroll the screen up when keyboard appears

I have an activity with an editText in the middle. When I click on the
editText the keyboard appears, and the screen moves so that the
editText is just above the keyboard, covering the other stuff that is
below.
I don't want to resize or add padding to the layout. I need that
the screen scrolls to the top the amount of space that I need to see
also the other stuff below. In other words, I'd like to select the
position of the editText when the keyboard appears. It would be enough
also if the screen scrolls up completely, showing me the bottom of the
layout.
I tried to put the layout in a scrollView adding this code on the editText
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//make the view scroll down to the bottom
scrollView.scrollTo(0, scrollView.getBottom());
}
});
but it doesn't work. Does anyone have an idea? Thank you in advance!
use android:windowSoftInputMode="adjustPan"
"adjustResize"
The activity's main window is always resized to make room for the soft keyboard on screen.
"adjustPan"
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
For example inn your manifest file:-
<activity
android:windowSoftInputMode="adjustPan"
android:name=".MainActivity"></activity>
Late answer.But surely it helps anyone.
I had a similar problem once, I resolved using:
android:windowSoftInputMode="adjustResize"
in the AndroidManifest.xml.
If you use a RelativeLayout start with the bottom element that must be hooked to the parent bottom and then hook the other element above the previous.
If you use LinearLayout wrap it in a RelativeLayout and hook the LinearLayout to parentBottom.
In this way when the keyboard pops up you will see the bottom of your layout.
Hope this helps.
I had a this problem , I resolved by this code :
myEdittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myscrollview.postDelayed(new Runnable() {
#Override
public void run() {
View lastChild = myscrollview.getChildAt(myscrollview.getChildCount() - 1);
int bottom = lastChild.getBottom() + myscrollview.getPaddingBottom();
int sy = myscrollview.getScrollY();
int sh = myscrollview.getHeight();
int delta = bottom - (sy + sh);
myscrollview.smoothScrollBy(0, delta);
}
}, 200);
}
});
This works for me
android:windowSoftInputMode="stateVisible|adjustResize"
in androidmanifest for the activity
This works: -
There is an issue with the scroll up when Keyboard opens up in Constraint Layout.
Don't use that.
To achieve "Scroll the screen up when keyboard appears"
Try this
Use Linear layout and ScrollView
and fill_parent in Scroll view.
and any Text field in the scroll view.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
</ScrollView>
</LinearLayout>
Add this library to your gradle dependencies:
dependencies {
compile 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.1.0'
}
and use this code:
KeyboardVisibilityEvent.setEventListener(getActivity(), new KeyboardVisibilityEventListener() {
#Override
public void onVisibilityChanged(boolean isOpen) {
if (isOpen && MY_EDIT_TEXT.hasFocus()) {
//scroll to last view
View lastChild = scrollLayout.getChildAt(scrollLayout.getChildCount() - 1);
int bottom = lastChild.getBottom() + scrollLayout.getPaddingBottom();
int sy = scrollLayout.getScrollY();
int sh = scrollLayout.getHeight();
int delta = bottom - (sy + sh);
scrollLayout.smoothScrollBy(0, delta);
}
}
});
It only worked for me after I changed from Constraint Layout to Linear Layout and added
android:windowSoftInputMode="adjustResize".
and removed
<item name="android:windowFullscreen">true</item> from the Activity style
just add
android:screenReaderFocusable="true"
to your XML file, it will work like a charm
The simple way to do it in Kotlin (reused from user user6581344 's answer), and i used it to perform a manual scroll to EditText when focus is set and keyboard is shown, as need it with extra bottom padding between keypad and the EditText:
binding.scrollView.postDelayed(object:Runnable {
override fun run() {
val extraBottomPadding = 100
val manualScroll = txtFullName.y + extraBottomPadding
scrollView.smoothScrollBy(0, manualScroll.toInt())
}
}, 200)
This code works for me, no need to add clickListener() or focusChanhgeListener() in your activity override onUserInteraction() function
#Override
public void onUserInteraction() {
super.onUserInteraction();
if (editText.hasFocus() ) { // to check if user is clicked on edit text
if (scroll.getVerticalScrollbarPosition() != scroll.getBottom()) // if scrollview is not already on the bottom
scroll.post(() -> scroll.scrollTo(0, scroll.getBottom()));
}
}

Categories

Resources