Gap between status and app bar when pressing back - android

When I load my app, it looks fine as shown:
However when I tap into a ListItem and press the back button, a gap is created as shown:
Not sure what is causing this:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="0dp"/>
</android.support.design.widget.AppBarLayout>
<fragment
android:id="#+id/car_nav_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_transport" />
</LinearLayout>
</layout>
And I am using Android JetPack's Navigation Controller
nav_transport.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"
app:startDestination="#+id/car_list_fragment">
<fragment
android:id="#+id/car_list_fragment"
android:name="com.example.transportdirectory.fragments.CarListFragment"
android:label="#string/cars_title"
tools:layout="#layout/fragment_car_list">
<action
android:id="#+id/action_car_list_fragment_to_car_detail_fragment"
app:destination="#id/car_detail_fragment"
app:enterAnim="#anim/slide_in_right"
app:exitAnim="#anim/slide_out_left"
app:popEnterAnim="#anim/slide_in_left"
app:popExitAnim="#anim/slide_out_right" />
</fragment>
<fragment
android:id="#+id/car_detail_fragment"
android:name="com.example.transportdirectory.fragments.CarDetailFragment"
android:label="#string/car_detail"
tools:layout="#layout/fragment_car_detail">
<argument
android:name="vin"
app:argType="string" />
</fragment>
</navigation>
This is how I invoke the navigation
val direction = CarListFragmentDirections.ActionCarListFragmentToCarDetailFragment(vin)
it.findNavController().navigate(direction)

Related

back button does not work well when using search view

I'm new in android and I have a search view in a fragment that when its text changes the results appear in a recycler view. The problem is when back button is pressed, only the query text is removed and it does not go to the previous fragment. What shall I do to make the back button work as it should.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_margin="8dp">
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:layoutDirection="rtl"
app:iconifiedByDefault="false"
app:layout_constraintTop_toTopOf="parent"
app:searchIcon="#drawable/ic_baseline_search_24" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layoutDirection="rtl"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toBottomOf="#id/searchView" />
</androidx.constraintlayout.widget.ConstraintLayout>
.
<?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"
android:id="#+id/shopping_nav"
app:startDestination="#id/homeFragment">
<fragment
android:id="#+id/homeFragment"
android:name="com.example.shopingofmine.ui.home.HomeFragment"
android:label="Home" >
>
<action
android:id="#+id/action_homeFragment_to_searchFragment"
app:destination="#id/searchFragment"
/>
</fragment>
<fragment
android:id="#+id/searchFragment"
android:name="com.example.shopingofmine.ui.search.SearchFragment"
android:label="SearchFragment" >
</fragment>
</navigation>

Android Navigation Graph not displaying start fragment

I'm running into a wall debugging what should be a simple issue: I have an app with one activity, which contains a navigation graph, which should display a fragment on start. But it isn't.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".MainActivity"
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"/>
</FrameLayout>
res/navigation/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/listFragment"
>
<fragment
android:id="#+id/listFragment"
android:name="org.example.my_app.ui.ListFragment"
android:label="fragment_list"
tools:layout="#layout/fragment_list"
>
<action
android:id="#+id/action_listFragment_to_detailFragment"
app:destination="#id/detailFragment"
/>
</fragment>
<fragment
android:id="#+id/detailFragment"
android:name="org.example.my_app.ui.DetailFragment"
android:label="fragment_detail"
tools:layout="#layout/fragment_detail"
/>
</navigation>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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"
tools:context=".ui.ListFragment"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This should be visible"
/>
</FrameLayout>
ListFragment.kt
class ListFragment : Fragment(R.layout.fragment_list)
Despite this dead-simple setup, I see a blank screen (just an app bar) and debug listeners added to the ListFragment class init don't ever get called. Why isn't my navGraph initializing an instance of the fragment?
Documentation:
NavHostFragment provides an area within your layout for self-contained navigation to occur.
You are missing to declare the fragment placeholder as a NavHostFragment in the FragmentContainerView via the android:name attribute; and therefore the navigation won't occur.
<androidx.fragment.app.FragmentContainerView
....
android:name="androidx.navigation.fragment.NavHostFragment"

How to change fragment action bar title color in bottomnaviagtionview

following is the navigation xml I wish there could be option as labelcolor any other solution to change label color as label appears on action bar
<?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/app_navigation.xml"
app:startDestination="#id/Fragment1"
>
<fragment
android:id="#+id/Fragment1"
android:label="Fragment1"
tools:layout="#layout/fragment1" />
<fragment
android:id="#+id/Fragment2"
android:label="Fragment2"
tools:layout="#layout/fragment2" />
<fragment
android:id="#+id/Fragment3"
android:label="Fragment3"
tools:layout="#layout/fragment3" />
</navigation>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbarNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#color/white"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme" >
add app:titleTextColor="#color/white" in toolbar. It'll work.

NavigationFragment does not show content

I have followed a guide on creating android navigation with the androidx navigation package. However, I am having troubles displaying anything. When the app starts it shows and empty activity and not the content of my fragment.
Do you have to do anything extra in order to let it show the start screen of the navigation graph?
Launcher xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_height="match_parent"
android:layout_width="match_parent">
<fragment
android:id="#+id/launcherNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="#navigation/navigation_launcher" />
</FrameLayout>
Navigation graph (navigation_launcher)
<?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/navigation_launcher"
app:startDestination="#id/launcherFragment">
<fragment
android:id="#+id/launcherFragment"
android:name="com.myapp.Views.LauncherFragment"
android:label="fragment_launcher"
tools:layout="#layout/fragment_launcher" >
<action
android:id="#+id/action_launcherFragment_to_mainActivity"
app:destination="#id/mainActivity" />
</fragment>
<activity
android:id="#+id/mainActivity"
android:name="com.myapp.Views.MainActivity"
android:label="activity_main"
tools:layout="#layout/activity_main" />
</navigation>
The fragments are just basic fragments with a text view inside. Nothing special here.
Any ideas on what is missing?
As far as I see from the code you posted there are some errors in your configuration:
The nav host fragment should be contained in activity_main.xml (or your main activity layout file, the main activity should be defined in your manifest file and it's the one responsible to load the first fragment displayed by the app
The launcher fragment layout should not contain a NavHostFragment itself, but it should have some views inside the frame layout (a textview displaying some text for example)
- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/navigation_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment_launcher.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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_height="match_parent"
android:layout_width="match_parent">
<TextView android:text="I'm the launcher fragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</FrameLayout>

How to navigate back to the master screen from the details with Nav controller on Android

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"
app:startDestination="#id/users_fragment"
android:id="#+id/nav_graph">
<fragment
android:id="#+id/users_fragment"
android:name="com.package.UsersFragment"
tools:layout="#layout/users_fragment"
android:label="UsersFragment">
<action
android:id="#+id/action_users_fragment_to_profile_fragment"
app:destination="#id/profile_fragment"
app:popUpTo="#id/users_fragment"
app:popUpToInclusive="true"/>
</fragment>
<fragment
android:id="#+id/profile_fragment"
android:name="com.package.ProfileFragment"
android:label="ProfileFragment">
<argument
android:name="userId"
app:argType="string" />
</fragment>
</navigation>
The main activity xml is:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewModel"
type="com.package.UsersViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="#color/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#id/app_bar_layout"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
To navigate to the details I call:
val action = UsersFragmentDirections
.actionUsersFragmentToProfileFragment(
userId = user.userId,
)
navController.navigate(action)
where UsersFragmentDirections is generated by the safe-args plugin.
The problem is that when I press back the master fragment is not shown: the activity is closed.
If in the action I use app:popUpToInclusive="false" then the back button does not work anymore.
What should I do to be able to go to the UsersFragment from the ProfileFragment when the user press back?
You're popping users fragment while moving to profile fragment. So when you press back, there is nothing in the backstack and hence activity finishes..
So, to fix this issue you have two options:
1) Manually re-open/navigate users fragment
Or
2) Remove these two lines
app:popUpTo="#id/users_fragment"
app:popUpToInclusive="true"

Categories

Resources