Skip to content

Commit

Permalink
add example to toggle info panel styles at runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek1508 committed Aug 11, 2022
1 parent 95b97f0 commit 3c36847
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app-preview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@
android:name=".dropinui.CustomInfoPanelActivity"
android:exported="true" />

<activity
android:name=".dropinui.CustomInfoPanelAttributesActivity"
android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mapbox.navigation.examples.preview
import android.content.Context
import androidx.core.content.ContextCompat
import com.mapbox.navigation.examples.preview.dropinui.CustomInfoPanelActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomInfoPanelAttributesActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomNavigationViewOptionsActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomRuntimeStylingActivity
import com.mapbox.navigation.examples.preview.dropinui.CustomSpeedLimitActivity
Expand Down Expand Up @@ -58,9 +59,18 @@ fun Context.examplesList() = listOf(
ToggleThemeActivity::class.java
),
MapboxExample(
ContextCompat.getDrawable(this, R.drawable.mapbox_screenshot_toggle_theme),
ContextCompat.getDrawable(this, R.drawable.mapbox_screenshot_custom_info_panel),
getString(R.string.title_customize_info_panel),
getString(R.string.description_customize_info_panel),
CustomInfoPanelActivity::class.java
),
MapboxExample(
ContextCompat.getDrawable(
this,
R.drawable.mapbox_screenshot_custom_info_panel_style
),
getString(R.string.title_customize_info_panel_attributes),
getString(R.string.description_customize_info_panel_attributes),
CustomInfoPanelAttributesActivity::class.java
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,33 @@ import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
import com.mapbox.navigation.dropin.ViewBinderCustomization
import com.mapbox.navigation.dropin.binder.infopanel.InfoPanelBinder
import com.mapbox.navigation.examples.preview.R
import com.mapbox.navigation.examples.preview.databinding.MapboxActivityCustomizeInfoPanelBinding

/**
* The example demonstrates how to use [ViewBinderCustomization] to replace Mapbox implementation
* of info panel with your custom info panel.
*
* Before running the example make sure you have put your access_token in the correct place
* inside [app-preview/src/main/res/values/mapbox_access_token.xml]. If not present then add
* this file at the location mentioned above and add the following content to it
*
* <?xml version="1.0" encoding="utf-8"?>
* <resources xmlns:tools="http://schemas.android.com/tools">
* <string name="mapbox_access_token"><PUT_YOUR_ACCESS_TOKEN_HERE></string>
* </resources>
*
* The example uses replay location engine to facilitate navigation without physically moving.
*
* How to use the example:
* - Start the example
* - Grant the location permissions if not already granted
* - Long press to mark the destination
* - Press route preview button to preview the route from origin to destination
* - Press start active navigation button to start navigating
*/
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
class CustomInfoPanelActivity : AppCompatActivity() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.mapbox.navigation.examples.preview.dropinui

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
import com.mapbox.navigation.dropin.ViewStyleCustomization
import com.mapbox.navigation.dropin.ViewStyleCustomization.Companion.defaultInfoPanelBackground
import com.mapbox.navigation.dropin.ViewStyleCustomization.Companion.defaultInfoPanelMarginEnd
import com.mapbox.navigation.dropin.ViewStyleCustomization.Companion.defaultInfoPanelMarginStart
import com.mapbox.navigation.dropin.ViewStyleCustomization.Companion.defaultInfoPanelPeekHeight
import com.mapbox.navigation.examples.preview.R
import com.mapbox.navigation.examples.preview.databinding.MapboxActivityCustomizeInfopanelOptionsBinding

/**
* The example demonstrates how to use [ViewStyleCustomization] to customize the peek height,
* margins and background for the default info panel supplied by Drop-In UI.
*
* Before running the example make sure you have put your access_token in the correct place
* inside [app-preview/src/main/res/values/mapbox_access_token.xml]. If not present then add
* this file at the location mentioned above and add the following content to it
*
* <?xml version="1.0" encoding="utf-8"?>
* <resources xmlns:tools="http://schemas.android.com/tools">
* <string name="mapbox_access_token"><PUT_YOUR_ACCESS_TOKEN_HERE></string>
* </resources>
*
* The example uses replay location engine to facilitate navigation without physically moving.
*
* How to use the example:
* - Start the example
* - Grant the location permissions if not already granted
* - Press on the floating button in the bottom right to apply custom styles
*/
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
class CustomInfoPanelAttributesActivity : AppCompatActivity() {

private lateinit var binding: MapboxActivityCustomizeInfopanelOptionsBinding
private var toggleInfoPanelStyles = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = MapboxActivityCustomizeInfopanelOptionsBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.navigationView.api.enableReplaySession()

binding.toggleInfoPanelStyles.setOnClickListener {
customizeInfoPanelStyles(toggleInfoPanelStyles)
toggleInfoPanelStyles = !toggleInfoPanelStyles
}
}

private fun customizeInfoPanelStyles(enabled: Boolean) {
binding.navigationView.customizeViewStyles {
if (enabled) {
infoPanelBackground = defaultInfoPanelBackground()
infoPanelPeekHeight = defaultInfoPanelPeekHeight(
this@CustomInfoPanelAttributesActivity
)
infoPanelMarginEnd = defaultInfoPanelMarginEnd()
infoPanelMarginStart = defaultInfoPanelMarginStart()
} else {
infoPanelBackground = R.drawable.mapbox_bg_custom_info_panel
infoPanelPeekHeight = this@CustomInfoPanelAttributesActivity
.resources
.getDimensionPixelSize(R.dimen.mapbox_custom_infopanel_peekheight)
infoPanelMarginEnd = this@CustomInfoPanelAttributesActivity
.resources
.getDimensionPixelSize(R.dimen.mapbox_custom_infopanel_marginEnd)
infoPanelMarginStart = this@CustomInfoPanelAttributesActivity
.resources
.getDimensionPixelSize(R.dimen.mapbox_custom_infopanel_marginStart)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/onPrimary" />
<corners android:radius="5dp" />
</shape>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.mapbox.navigation.dropin.NavigationView
android:id="@+id/navigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:accessToken="@string/mapbox_access_token" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/toggleInfoPanelStyles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="120dp"
android:src="@drawable/mapbox_ic_customize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="ContentDescription"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="mapbox_custom_infopanel_marginEnd">10dp</dimen>
<dimen name="mapbox_custom_infopanel_marginStart">10dp</dimen>
<dimen name="mapbox_custom_infopanel_peekheight">90dp</dimen>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@
<string name="title_customize_info_panel">Customize info panel using NavigationView</string>
<string name="description_customize_info_panel">The example demonstrates how to replace default info panel with a custom one</string>

<string name="title_customize_info_panel_attributes">Customize info panel attributes using NavigationView</string>
<string name="description_customize_info_panel_attributes">The example demonstrates how to change margins, background and peek height of the default info panel.</string>

</resources>

0 comments on commit 3c36847

Please sign in to comment.