Hello all readers. In this blog,I am going to tell you how to restrict a particular view (Relative or Linear) to be always at parent bottom of your activity even when the keyboards appears.
Lets us first look at the xml, activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ssss" />
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tv">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="1"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="2"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="3"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="4"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="5"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="6"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="7"
android:singleLine="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="8"
android:singleLine="true" />
</LinearLayout>
<View
android:id="@+id/viewTemp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="@+id/rlBottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/ll"
android:background="@android:color/holo_blue_light">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hit" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
In this xml, i have used a Textview, 8 Edittext, a View and a Button.
Now the Java file, MainActivity.java
private RelativeLayout rlBottom;
private View viewTemp;
private int viewWidth, viewHeight, temp, screenHeight, screenWidth, scrollWidth, scrollHeight;
Copy the code in onCreate of your Activity,
rlBottom = (RelativeLayout) findViewById(R.id.rlBottom);
viewTemp = findViewById(R.id.viewTemp);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHeight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;
Log.v("width is", screenWidth + "");
Log.v("height is", screenHeight + "");
ViewTreeObserver viewTreeObserver = rlBottom.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
rlBottom.getViewTreeObserver().removeOnGlobalLayoutListener(this);
viewWidth = rlBottom.getWidth();
viewHeight = rlBottom.getHeight();
Log.v("view width is", viewWidth + "");
Log.v("view height is", viewHeight + "");
temp = (screenHeight - (2 * viewHeight));
viewTemp.setMinimumHeight(temp);
Log.v("view height calculated", temp + "");
}
});
}
Here i have calculated width and height of the android phone. After calculating that, width and height of Relative layout with id rlBottom is calculated using ViewTreeObserver. A View is set to minimum height which is calculated by
temp = (screenHeight - (2 * viewHeight));
Build your project and Run,Relative layout will always be a parent bottom and doesn't come up when keyboard appears.
Note:In your manifest file,use windowSoftInputMode=adjustResize of your particular avtivity.
That's all. Hope it helps :)
0 Comment(s)