Bottom Navigation bar moves up with keyboard
Here is the solution, how I resolved the issue using this api: https://github.com/yshrsmz/KeyboardVisibilityEvent.
Steps:
1) In your androidManifest file:
Add the following tag: android:windowSoftInputMode="adjustResize"
As I did here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eapple.tripdatacollection">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main2Activity"
android:windowSoftInputMode="adjustResize"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewSavedLocations" />
<activity android:name=".fireBaseUpdates" />
<activity android:name=".SignInActivity" />
<activity android:name=".ForgotPasswordActivity" />
<activity android:name=".UploadDataActivity" />
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main2" />
</application>
</manifest>
2) Add the following dependency in your build.grade file (App)
dependencies { implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.1.0' }
3) Add the following code to your activity onCreate method:
KeyboardVisibilityEvent.setEventListener(
getActivity(),
new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
// some code depending on keyboard visiblity status
}
});
Here is my Java file:
package com.example.eapple.tripdatacollection;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEvent;
import net.yslibrary.android.keyboardvisibilityevent.KeyboardVisibilityEventListener;
public class Main2Activity extends AppCompatActivity {
private static AddLocationFragment addLocationFragment;
private static ProfileFragment profileFragment;
private static SavedLocationsFragment savedLocationsFragment;
private View rootView;
private final String TAG = "Main2Activity";
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_profile:
loadFragment(profileFragment);
return true;
case R.id.navigation_saved_locations:
loadFragment(savedLocationsFragment);
return true;
case R.id.navigation_add_location:
loadFragment(addLocationFragment);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
addLocationFragment = new AddLocationFragment();
savedLocationsFragment = new SavedLocationsFragment();
profileFragment = new ProfileFragment();
rootView = findViewById(R.id.root_view);
final BottomNavigationView navigation = findViewById(R.id.navBar);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.navigation_add_location);
loadFragment(addLocationFragment);
KeyboardVisibilityEvent.setEventListener(
this,
new KeyboardVisibilityEventListener() {
@Override
public void onVisibilityChanged(boolean isOpen) {
Log.d(TAG,"onVisibilityChanged: Keyboard visibility changed");
if(isOpen){
Log.d(TAG, "onVisibilityChanged: Keyboard is open");
navigation.setVisibility(View.INVISIBLE);
Log.d(TAG, "onVisibilityChanged: NavBar got Invisible");
}else{
Log.d(TAG, "onVisibilityChanged: Keyboard is closed");
navigation.setVisibility(View.VISIBLE);
Log.d(TAG, "onVisibilityChanged: NavBar got Visible");
}
}
});
}
private void loadFragment(android.support.v4.app.Fragment fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
In my case it was enough only to add android:windowSoftInputMode="adjustPan" to my AndroidManifest file as follows:
<activity android:name=".view.MainActivity"
android:windowSoftInputMode="adjustPan"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>