I am using android studio with java for this.
Problem Statement Fix the button at the bottom of the screen when content is small and at the bottom of the content if content is large enough to take the whole screen.
1. When the content is small
I want to fix two buttons at the bottom of the screen when content is small enough to take the whole screen.
2. When the content is large
When the content is large enough to take up the whole screen it should appear at the bottom of the whole content.When the user scrolls down then the button should show up. As in this image I have scrolled completely down if I want to click on the button.
Here is the code for the second implementation
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/eachWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:scrollbars="vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/perWordHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="History" />
<Button
android:id="@+id/perWordBack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I want to implement both scenario using one xml file but with the second implementation when the content is small, then the button comes below the content not at the bottom of the screen.
minHeight attribute is the key to answer for this question.
I first got the app screen size and then subtracted the button and the toolbar size from it. Then I set that as the min size of the linear layout in which my content was there.
I have used the function onWindowFocusChanged as sometimes layout might take time to get fully load and hence getting the button height might fail.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
Configuration configuration = getResources().getConfiguration();//returns the app screen size.
int screenHeightDp = configuration.screenHeightDp-56;// 56 is the max height of the android toolbar.
float density = this.getResources().getDisplayMetrics().density;
screenHeightDp = (int)(screenHeightDp*density);//changes the dp to pixel
Button perWordHistory = findViewById(R.id.perWordHistory);
int heightOfButton = perWordHistory.getHeight()*2; // as I have two buttons
screenHeightDp = screenHeightDp - heightOfButton;
LinearLayout linearLayout = findViewById(R.id.layoutContainingRecycleView);
linearLayout.setMinimumHeight(screenHeightDp);
}
Here is the layout code after some changes.
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/layoutContainingRecycleView">
<android.support.v7.widget.RecyclerView
android:id="@+id/eachWord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:scrollbars="vertical" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/perWordHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_history" />
<Button
android:id="@+id/Help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/help" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.