Dynamically obtain the status bar and navigation bar height, to automatically and correctly place the Toolbar and Map UI Controllers in MerchantsFragment.

This commit is contained in:
Severiano Jaramillo 2019-01-31 13:36:38 -06:00
parent 6e5c6b50b8
commit c7138c5e22
2 changed files with 16 additions and 4 deletions

View file

@ -102,6 +102,11 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
private var showMerchantsMarkers = true
private var showTellerMarkers = true
// Variables used to dynamically obtain the status bar and navigation bar height, to automatically and correctly
// place the Toolbar and Map UI controllers
private var statusBarSize = 0
private var navigationBarSize = 0
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
// Hide the activity's Toolbar so that we can make the trick of the translucent navigation and status bars
val activityToolbar: Toolbar? = activity?.findViewById(R.id.toolbar)
@ -118,6 +123,16 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// Dynamically obtain status bar and navigation bar heights, and account for the status bar height to add
// the correct top margin to the Toolbar and place it just below the status bar
view.setOnApplyWindowInsetsListener { v, insets ->
statusBarSize = insets.systemWindowInsetTop
navigationBarSize = insets.systemWindowInsetBottom
val layoutParams = toolbar.layoutParams as ViewGroup.MarginLayoutParams
layoutParams.topMargin = statusBarSize
insets
}
// Set the fragment's toolbar as the activity toolbar just for this fragment
(activity as AppCompatActivity).setSupportActionBar(toolbar)
(activity as AppCompatActivity).supportActionBar?.setDisplayHomeAsUpEnabled(true)
@ -343,8 +358,7 @@ class MerchantsFragment : Fragment(), OnMapReadyCallback, SearchView.OnSuggestio
mMap = googleMap
// Add padding to move the controls out of the toolbar/status bar and navigation bar.
// TODO might be necessary to convert dp to pixels
mMap.setPadding(0, 200, 0, 100)
mMap.setPadding(0, toolbar.height + statusBarSize, 0, navigationBarSize)
applyMapTheme()

View file

@ -8,12 +8,10 @@
android:orientation="vertical"
tools:context=".activities.MainActivity">
<!-- TODO get the height of the status bar dynamically instead of hardcoding the 24dp-->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="24dp"
android:background="@color/colorMapToolbar"
android:elevation="4dp"
app:title="@string/title_merchants_and_tellers"