Use this custom relative layout to detect soft keyboard using ur xml
import android.app.Activity; import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class RelativeLayoutThatDetectsSoftKeyboard extends RelativeLayout { public RelativeLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) { super(context, attrs); } public interface Listener { public void onSoftKeyboardShown(boolean isShowing); } private Listener listener; public void setListener(Listener listener) { this.listener = listener; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int height = MeasureSpec.getSize(heightMeasureSpec); Activity activity = (Activity)getContext(); Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight(); int diff = (screenHeight - statusBarHeight) - height; if (listener != null) { listener.onSoftKeyboardShown(diff>128);
Then implements RelativeLayoutThatDetectsSoftKeyboard.Listener for your Actiity class
RelativeLayoutThatDetectsSoftKeyboard mainLayout = (RelativeLayoutThatDetectsSoftKeyboard)V.findViewById(R.id.dealerSearchView); mainLayout.setListener(this); @Override public void onSoftKeyboardShown(boolean isShowing) { if(isShowing) { } else { } }
Based on keyboard visibility, move the layout up and down using the layout options
source share