0

A layout is designed using RecycleView to display images. A button is added below RecycleView to save the selected items from the list.button is not visible not in Design View nor when I run the app . Couldn't trace where the problem is. The code is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="com.example.user.recycleview.imagesview">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="4dp"
    android:theme="@style/AppTheme.AppBarLayout">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/titlebar"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<view
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    class="android.support.v7.widget.RecyclerView"
    android:id="@+id/recycler_view" />

<Button
    android:id="@+id/button7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/savebutton"
    android:text="@string/save"
    android:textColor="@color/white"
    android:textSize="12pt" />


<!--
Deleted fab element
-->

</LinearLayout>
Pouya Danesh
  • 1,557
  • 2
  • 19
  • 36
Shekar
  • 149
  • 3
  • 14

3 Answers3

5

1. Add a RelativeLayout below AppBarLayout and put Button and RecyclerView inside RelativeLayout.

2. Add attribute android:layout_alignParentBottom="true" to Button to align it at the bottom of screen.

3. Add attribute android:layout_above="@id/button7" to RecyclerView to show it above Button.

Update your layout as below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:theme="@style/AppTheme.AppBarLayout">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:text="Save"
            android:textColor="@android:color/black" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@id/button7"/>

    </RelativeLayout>
</LinearLayout>

OUTPUT:

enter image description here

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
0

You have the recyclerview as fill_parent. So it will occupy the full screen, and the button will not be visible. Try using weights as mentioned below.

android:layout_width="fill_parent"
android:layout_height="fill_parent"

You can use weights to give dynamic height

<LinearLayout ...
android:layout_weight="10">

   <view
        android:layout_width="fill_parent"
        android:layout_weight="9"
        android:layout_height="0px"
        class="android.support.v7.widget.RecyclerView"
        android:id="@+id/recycler_view"
        android:layout_marginBottom="130px"/>


    <Button
        android:layout_width="100px"
        android:layout_height="0px"
        android:text="Save"
        android:layout_weight="1"/>

</LinearLayout>

Or you can set the height dynamically in java by calculating the height of the screen

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

int height = metrics.heightPixels;
int width = metrics.widthPixels;
Santosh
  • 1,871
  • 1
  • 19
  • 36
  • How can i set to fixed height as the display changes as per the mobile piece dimensions. Please correct me i understood it wrongly. – Shekar May 23 '17 at 15:00
0

Your not able to see the button because you have set height of your RecyclerView to fill_parent.

There are several ways to solve this problem: Change the height to wrap_content or set

  1. Change the height to wrap_content or set a fixed height
  2. Use RelativeLayout as base layout and for RecyclerView add layout_above attribute. So that RecyclerView doesn't push your Button below.
  3. Use 1st and add margin/padding

Note: You should use match_parent as fill_parent is now deprecated

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52