Fragment not filling up entire screen/parent layout Android - android

I have a FrameLayout in my app that is being replaced by a Fragment that contains a RecyclerView, but when it does that, the RecyclerView doesn't fill up the whole FrameLayout, there's a border around it.
Parent Layout, this is what is called from MainActivity, it contains the FrameLayout that is going to be replaced:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="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:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.jggdevelopment.wannacook.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/relative_layout_for_fragment"
tools:showIn="#layout/navigation_toolbar"
tools:context="com.jggdevelopment.wannacook.MainActivity"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Fragment it's being replaced with, a RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true">
<android.support.v7.widget.RecyclerView
android:id="#+id/fridge_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"/>
<com.getbase.floatingactionbutton.FloatingActionsMenu
android:id="#+id/fab_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/floating_button"
android:layout_alignParentEnd="true"
android:layout_gravity="bottom|end"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
fab:fab_labelStyle="#style/menu_labels_style"
fab:fab_labelsPosition="left"
fab:fab_icon="#drawable/ic_plus_white_24dp"
fab:fab_addButtonColorNormal="#color/colorAccent"
fab:fab_addButtonColorPressed="#color/colorAccentPressed">
<com.getbase.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addNewItem"
fab:fab_colorNormal="#color/white"
fab:fab_colorPressed="#color/whitePressed"
fab:fab_title="Add new item"
fab:fab_icon="#drawable/ic_restaurant_black_24dp"
/>
</com.getbase.floatingactionbutton.FloatingActionsMenu>
</android.support.design.widget.CoordinatorLayout>
Code in MainActivity used to replace Fragment:
else if (id == R.id.nav_fridge) {
FridgeFragment fridgeFragment = new FridgeFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(
R.id.relative_layout_for_fragment,
fridgeFragment,
fridgeFragment.getTag()
).commit();
}

Please remove padding on FrameLayout.
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"

Make this changes in your Frame Layout
remove:
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
Or use this Frame Layout in your layout (i have made the changes )
<FrameLayout
android:id="#+id/relative_layout_for_fragment"
tools:showIn="#layout/navigation_toolbar"
tools:context="com.jggdevelopment.wannacook.MainActivity"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
</FrameLayout>

Related

cannot resolve R when applying BottomNavigationView bottombar

when I add bottomNavigationView, I got cannot resolve R , but when I remove the bottomNavigationView the error disappear, so why I am getting such error ?
the below code result to unresolved R
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.recyclerviewgrid.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:background="#android:color/holo_blue_light"
app:menu="#menu/my_navigation_items" />
</android.support.design.widget.CoordinatorLayout>
when remove it as the below the error disappear
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.learn2crack.recyclerviewgrid.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="#+id/card_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
If your XML's contain errors your generated file might not build try erasing most of their content and making sure id's are not duplicated.
Check if you have imported the right lib to work with BottomNavigation and none of your props are null.
Here is my Activity code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemIconTint="#drawable/bottom_menu_selector"
app:itemTextColor="#drawable/bottom_menu_selector"
app:menu="#menu/bottom_navigation_menu" />
<ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/bottom_navigation"/>

Can't eliminate margins on all side of a toolbar in android

I'm trying to include a toolbar widget into an activity, but, the toolbar has extra spacing on all sides.
toolbar.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"
android:minHeight="?android:attr/actionBarSize"
android:background="?attr/colorPrimary"
local:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
local:contentInsetEnd="0dp"
local:contentInsetStart="0dp"
local:contentInsetLeft="0dp"
local:contentInsetRight="0dp"
/>
Preview of toolbar.xml:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_gravity="center"
android:orientation="vertical"
tools:context="com.infotoall.newsapp.FacebookLoginActivity">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
/>
</LinearLayout>
Preview of layout:
Remove Padding from your Linear Layout.
Remove these lines
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
Check the XML for your Activity and remove the padding attributes. The Activity is the holder for your toolbar and thus the padding attributes will apply for to all of it's child elements.
You are providing paddings in the root LinearLayout of activity_main.xml.Change your activity_main.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context="com.infotoall.newsapp.FacebookLoginActivity">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
/>
</LinearLayout>

Android element is hidden under bottom dock

So I just created a new basic activity:
activity_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="com.example.nika.chatapp.Chat">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_chat" />
</android.support.design.widget.CoordinatorLayout>
content_chat is a layout inside coordinator layout which has
height="match_parent" and I have an element at the bottom of it and it's
hidden beneath bottom deck.
Is there a way to solve this problem ?
content_chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.nika.chatapp.Chat"
tools:showIn="#layout/activity_chat">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Remove app:layout_behavior="#string/appbar_scrolling_view_behavior" from the RelativeLayout in content_chat.xml
Also as another commenter noted, you will want to add a top-margin to your RelativeLayout via android:layout_marginTop="?attr/actionBarSize", so it plays nicely with the AppBarLayout

I want the ViewPager to be in full screen - Android Studio

I have some issues with the ViewPager I am using. I want it to be in full screen, but for some reason there are some edges that won't disappear. How can I solve this?
Code from the XML-file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can try this:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Try to remove paddings from all four sides. By default when you create the activity using AS, AS will add padding to your main view. Below is the layout without external padding.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You can try as like following way
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".Main.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
remove all of android:padding from RelativeLayout
and remove android:layout_centerVertical and android:layout_centerHorizontal
from ViewPager

Material Design Floating Button (FAB) Blocking RecyclerView

I'm facing a strange issue while trying to use a material design FAB button in my app. I'm using this library (https://github.com/makovkastar/FloatingActionButton).
My activity xml has a RecyclerView and the FAB. But the FAB is actually blocking the lower portion of the entire screen. Something like this :
http://i.stack.imgur.com/FtEcb.png
It can be seen in the picture that the RecyclerView containing the cards is being blocked by the Floating Button. Here's the code I'm using:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
class="com.rubberduck.zoop.fragments.OffersFragment"
android:id="#+id/fragment_enter_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
tools:layout="#layout/fragment_offer" />
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="8dp"
android:elevation="6dp"
android:src="#drawable/ic_place_white_24dp"
fab:fab_colorNormal="#color/color_accent"
fab:fab_colorPressed="#color/color_accent_pressed"
fab:fab_colorRipple="#color/ripple" />
</LinearLayout>
fragment_offer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.rubberduck.zoop.fragments.OffersFragment">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_offers"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include
android:id="#+id/card_offer"
layout="#layout/offer_card" />
</LinearLayout>
When you use a LinearLayout as the container for your fragment and FloatingActionButton, items do not overlap: The LinearLayout gives the full width to each component when you use orientation="vertical". Instead, as per the FloatingActionButton readme, you should use a FrameLayout (which allows items to overlap - ensuring the FloatingActionButton appears above the RecyclerView):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
class="com.rubberduck.zoop.fragments.OffersFragment"
android:id="#+id/fragment_enter_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_offer" />
<com.melnykov.fab.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="8dp"
android:elevation="6dp"
android:src="#drawable/ic_place_white_24dp"
fab:fab_colorNormal="#color/color_accent"
fab:fab_colorPressed="#color/color_accent_pressed"
fab:fab_colorRipple="#color/ripple" />
</FrameLayout>

Categories

Resources