Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Add transparent nav bar - add edge to edge support. #600

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
Expand All @@ -28,6 +29,7 @@ import com.google.samples.apps.sunflower.adapters.GardenPlantingAdapter
import com.google.samples.apps.sunflower.adapters.PLANT_LIST_PAGE_INDEX
import com.google.samples.apps.sunflower.databinding.FragmentGardenBinding
import com.google.samples.apps.sunflower.utilities.InjectorUtils
import com.google.samples.apps.sunflower.utilities.doOnApplyWindowInsets
import com.google.samples.apps.sunflower.viewmodels.GardenPlantingListViewModel

class GardenFragment : Fragment() {
Expand All @@ -44,15 +46,23 @@ class GardenFragment : Fragment() {
savedInstanceState: Bundle?
): View? {
binding = FragmentGardenBinding.inflate(inflater, container, false)
val adapter = GardenPlantingAdapter()
binding.gardenList.adapter = adapter
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.addPlant.setOnClickListener {
navigateToPlantListPage()
}

subscribeUi(adapter, binding)
return binding.root
with(binding.gardenList) {
val adapter = GardenPlantingAdapter()
this.adapter = adapter
subscribeUi(adapter, binding)
doOnApplyWindowInsets { view, windowInsets, rect ->
view.updatePadding(bottom = rect.bottom + windowInsets.systemWindowInsetBottom)
windowInsets
}
}
}

private fun subscribeUi(adapter: GardenPlantingAdapter, binding: FragmentGardenBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayoutMediator
import com.google.samples.apps.sunflower.adapters.MY_GARDEN_PAGE_INDEX
import com.google.samples.apps.sunflower.adapters.PLANT_LIST_PAGE_INDEX
import com.google.samples.apps.sunflower.adapters.SunflowerPagerAdapter
import com.google.samples.apps.sunflower.databinding.FragmentViewPagerBinding
import com.google.samples.apps.sunflower.utilities.doOnApplyWindowInsets

class HomeViewPagerFragment : Fragment() {

Expand All @@ -39,7 +42,20 @@ class HomeViewPagerFragment : Fragment() {
val tabLayout = binding.tabs
val viewPager = binding.viewPager

binding.coordinatorLayout.doOnApplyWindowInsets { view, windowInsetsCompat, rect ->
view.updatePadding(
top = rect.top + windowInsetsCompat.systemWindowInsetTop,
bottom = rect.bottom - windowInsetsCompat.systemWindowInsetBottom
)
// Uses for interception on viewPager child fragments
ViewCompat.dispatchApplyWindowInsets(viewPager, windowInsetsCompat)
windowInsetsCompat
}
viewPager.adapter = SunflowerPagerAdapter(this)
viewPager.doOnApplyWindowInsets { view, windowInsetsCompat, rect ->
view.updatePadding(bottom = rect.bottom + windowInsetsCompat.systemWindowInsetBottom)
windowInsetsCompat
}

// Set the icon and text for each tab
TabLayoutMediator(tabLayout, viewPager) { tab, position ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.observe
import com.google.samples.apps.sunflower.adapters.PlantAdapter
import com.google.samples.apps.sunflower.databinding.FragmentPlantListBinding
import com.google.samples.apps.sunflower.utilities.InjectorUtils
import com.google.samples.apps.sunflower.utilities.doOnApplyWindowInsets
import com.google.samples.apps.sunflower.viewmodels.PlantListViewModel

class PlantListFragment : Fragment() {

private lateinit var binding: FragmentPlantListBinding
private val viewModel: PlantListViewModel by viewModels {
InjectorUtils.providePlantListViewModelFactory(this)
}
Expand All @@ -42,17 +45,25 @@ class PlantListFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentPlantListBinding.inflate(inflater, container, false)
binding = FragmentPlantListBinding.inflate(inflater, container, false)
context ?: return binding.root

val adapter = PlantAdapter()
binding.plantList.adapter = adapter
subscribeUi(adapter)

setHasOptionsMenu(true)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(binding.plantList) {
val adapter = PlantAdapter()
this.adapter = adapter
subscribeUi(adapter)
doOnApplyWindowInsets { view, windowInsets, rect ->
view.updatePadding(bottom = rect.bottom + windowInsets.systemWindowInsetBottom)
windowInsets
}
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(R.menu.menu_plant_list, menu)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.samples.apps.sunflower.utilities

import android.graphics.Rect
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

fun View.doOnApplyWindowInsets(block: (View, WindowInsetsCompat, Rect) -> WindowInsetsCompat) {
val initialPadding = Rect(paddingLeft, paddingTop, paddingRight, paddingBottom)
ViewCompat.setOnApplyWindowInsetsListener(this) { v, windowInsets ->
block(v, windowInsets, initialPadding)
}
requestApplyInsetsWhenAttached()
}

fun View.requestApplyInsetsWhenAttached() {
if (isAttachedToWindow) {
requestApplyInsets()
} else {
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
v.removeOnAttachStateChangeListener(this)
v.requestApplyInsets()
}

override fun onViewDetachedFromWindow(v: View) = Unit
})
}
}
18 changes: 6 additions & 12 deletions app/src/main/res/layout/activity_garden.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,12 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<FrameLayout
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:id="@+id/nav_host"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_garden"/>

</FrameLayout>
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_garden"/>

</layout>
6 changes: 4 additions & 2 deletions app/src/main/res/layout/fragment_view_pager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
android:fitsSystemWindows="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:fitsSystemWindows="false"
android:theme="@style/Theme.Sunflower.AppBarOverlay">

<com.google.android.material.appbar.CollapsingToolbarLayout
Expand Down
25 changes: 25 additions & 0 deletions app/src/main/res/values-land-night-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>

<style name="Theme.Sunflower" parent="Base.Theme.Sunflower">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>

</resources>
25 changes: 25 additions & 0 deletions app/src/main/res/values-land-v21/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>

<style name="Theme.Sunflower" parent="Base.Theme.Sunflower">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-night-v21/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<style name="Theme.Sunflower" parent="Base.Theme.Sunflower">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<style name="Theme.Sunflower" parent="Base.Theme.Sunflower">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>

</resources>