Android Navigation Component: How do I style toolbar based on navigation graph - android-architecture-components

I have the following nav_graph.xml:
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/nav_graph"
app:startDestination="#id/actualFragment">
<fragment
android:id="#+id/actualFragment"
android:name="com.online.view.ui.ActualFragment"
android:label="Актуальное"
tools:layout="#layout/fragment_actual" >
<action
android:id="#+id/action_actualFragment_to_actualEventFragment"
app:destination="#id/actualEventFragment"
app:enterAnim="#anim/nav_default_pop_enter_anim"
app:exitAnim="#anim/nav_default_pop_exit_anim" />
</fragment>
<fragment
android:id="#+id/actualEventFragment"
android:name="com.online.view.ui.detail.ActualEventFragment"
android:label="Актуальное"
tools:layout="#layout/actual_event_fragment" />
</navigation>
and I have MainActivity with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:id="#+id/coordinatorLayout"
android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_height"
android:background="#color/white"
android:paddingLeft="8dp"
app:contentInsetStart="8dp"
app:contentInsetStartWithNavigation="0dp"
app:titleTextAppearance="#style/TabTitles"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:theme="#style/ToolbarStyle"
/>
<fragment
android:id="#+id/content"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#+id/bottom_tabs"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.587"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_tabs"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
app:labelVisibilityMode="labeled"
app:itemTextColor="#drawable/bottom_nav_colors"
app:itemTextAppearanceActive="#style/bottomBarTextSelected"
app:itemTextAppearanceInactive="#style/bottomBarText"
app:itemIconTint="#drawable/bottom_nav_colors"
android:alpha="1.0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/tabs_navigation"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Now I want to have different styling for activity toolbar when top level fragment is loaded and when its child is loaded (showing back arrow in toolbar).
QUESTION: How can I achieve this different styling for toolbar (appbar) if I use Android Navigation Component and I navigate from root fragment to child fragment using the following code:
Bundle bundle = new Bundle();
bundle.putString("id", ei.id);
MainActivity)context).getNavController().navigate(R.id.action_actualFragment_to_actualEventFragment, bundle);

NavigationUI uses an AppBarConfiguration object to manage the behavior of the Navigation button. The Navigation button’s behavior changes depending on whether the user is at a top-level destination. Have a look at their documentation Update UI components with NavigationUI

Related

Show BottomNavigationView when stop scrolling

I use attribute app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior" to hide BottomNavigationView when I'm scrolling down. It works. When I scroll up, BNV is showed again. Also works. But when I finished scrolling down (BNV is hidden) I want to see BNV without scrolling up. Is it possible?
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2AB670"
tools:context=".presentation.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragment_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/bottom_navigation_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/app_navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/green_01"
app:itemBackground="#drawable/bottom_navigation_selector"
app:itemIconTint="#color/white"
app:itemTextAppearanceActive="#style/ItemTextAppearanceActive"
app:itemTextColor="#color/white"
app:labelVisibilityMode="labeled"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
android:layout_gravity="bottom"
app:menu="#menu/bottom_navigation_menu" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Dynamically load xml control and share UI control

I am new to xamarin. Is it possible to load a shared UI control in an xml file, if yes, how? I am open to better idea.
Below is a static loading via include, where parent.axml statically load child.xml.
<include
android:id="#+id/child"
layout="#layout/child"
android:layout_below="#id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
parent.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<include
android:id="#+id/child"
layout="#layout/child"
android:layout_below="#id/toolbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/parent_layout"
android:layout_below="#id/child">
<TextView
android:id="#+id/parent_text"
android:layout_margin="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click to go to the moon!"
android:textAppearance="?android:attr/textAppearanceSmall"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
Child.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/child_text"
android:layout_margin="16dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You made it!"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
The idea is that it can achieve share of UI controls. In this case, parent.axml loads child.xml into its place holder based on condition.
You can use LayoutInflater to inflate layouts and auto-attach them to the parent (or not):
var linearLayout = LayoutInflater.Inflate(Resource.Layout.Child, parent, true);
var textView = linearLayout.FindViewById<TextView>(Resource.Id.child_text);
textView.Text = "Inflated and attached to Parent";
Also depending upon your use-case, using a self-contained Fragment and adding that to your "parent" might be better a better solution.

Xamarin Bottom Bar Gets Lost

I have a listview, a toolbar and a bottom nav bar. Toolbar is aligned to top while nav bar is aligned to bottom. Its working well unless i add items to listview. After adding 5 items, the bottom bar disappears. Cant i align the bot bar like the toolbar so it just stays still ?
Activity's axml
<?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">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listv_sepet" />
<include
android:id="#+id/toolbar_bottom"
layout="#layout/toolbar_bot"
android:layout_alignParentBottom="true"/>
</LinearLayout>
</RelativeLayout>
Bottom Toolbar axml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
Thanks in the advance.
The problem is that you wrap the height of the ListView and don't tell it to be above the bottom Toolbar.
This can easily be fixed by:
un-nesting your layout as it is not really necessary in this case to do any nesting.
tell the ListView to be below the top Toolbar and above the bottom Toolbar
So something like this:
<?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">
<include
android:id="#+id/toolbar"
android:layout_alignParentTop="true"
layout="#layout/toolbar" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_above="#+id/toolbar_bottom"
android:id="#+id/listv_sepet" />
<include
android:id="#+id/toolbar_bottom"
layout="#layout/toolbar_bot"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
That should fix it.
Rather than using an include in your layout you can set the footer (and header) in code when binding to the listview by using the AddHeaderView and AddFooterView methods
For example:
listView = view.FindViewById<ListView>(Resource.Id.mylistView);
View headerView = View.Inflate(Activity, Resource.Layout.ListHeader, null);
listView.AddHeaderView(headerView);

Toolbar doesn't hide completely on scroll

I try to hide the toolbar during scroll but then this happens! What do I have to do?
This is the layout code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/SimpleTheme.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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/SimpleTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin" />
</android.support.design.widget.CoordinatorLayout>
The view container has following behavior:
app:layout_behavior="#string/appbar_scrolling_view_behavior"
Screenshots:
Before scroll
After scroll
There is something with 'fitsSystemWindows'
Make it false in coordinatorLayout and true in AppBarLayout

How to add LinearLayout with rows of imageView In NavigationDrawer

Hi I have the following layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Add The mainmenu-->
// add a linearLayout with 3 row of imagaeView
<!-- The navigation drawer -->
<Mvx.MvxListView
local:MvxBind="ItemsSource MenuItems; ItemClick SelectMenuItemCommand"
local:MvxItemTemplate="#layout/item_menu"
android:id="#+id/left_drawer"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:choiceMode="singleChoice"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />
</android.support.v4.widget.DrawerLayout>
Problem:
How to add a linearLayout with many rows into this Navigational drawer that using MvxListView.
The NavigationDrawer is on the left side.
Questions:
Must I use FrameLayout?
Can I replace FrameLayout with LinearLayout?
DrawerLayout only supports two child views.
The first element must always be the content view, the second will be what is in the drawer, and you must specify a gravity indicating whether the drawer is coming in from the left or the right.
If you want more than a ListView in the drawer, just wrap the contents in another layout such as RelativeLayout, FrameLayout or LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent">
<!-- add other views here -->
<Mvx.MvxListView
local:MvxBind="ItemsSource MenuItems; ItemClick SelectMenuItemCommand"
local:MvxItemTemplate="#layout/item_menu"
android:id="#+id/left_drawer"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:choiceMode="singleChoice"
android:layout_width="match_parent"
android:layout_height="0"
android:layout_weight="1"
android:background="#111" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Resources