Style attribution
A newer version of the Maps SDK is available
This page uses v9.7.1 of the Mapbox Maps SDK. A newer version of the SDK is available. Learn about the latest version, v11.7.0, in the Maps SDK documentation.
Note
This example is a part of the Mapbox Android Demo app. You can find the values for all referenced resources in the res
directory. For example, see res/values/activity_strings.xml
for R.string.*
references used in this example. The dependencies can be found here.The examples use View binding.See setup documention if necessary.
<?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:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/switch_attribution_color_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_swap_horiz_white_24dp"
mapbox:fabSize="normal" />
</FrameLayout>
package com.mapbox.mapboxandroiddemo.examples.labs;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.Style;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
/**
* Adjust the Maps SDK's attribution ImageView color so that it matches the map style or app UI
*/
public class ChangeAttributionColorActivity extends AppCompatActivity implements OnMapReadyCallback {
private static final String CUSTOM_GREEN_MONOCHROME_STYLE_URI =
"mapbox://styles/appsatmapboxcom/ck0cyq1lt0bbo1cmwkpf2w7g9";
private MapView mapView;
private int index = 0;
private int[] attributionColors;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
attributionColors = new int[] {
Color.RED,
Color.BLUE,
Color.YELLOW,
ContextCompat.getColor(this, R.color.colorAccent), // a runtime app UI color
ContextCompat.getColor(this, R.color.colorPrimaryDark), // a runtime app UI color
Color.parseColor("#1e7019"), // dark green
};
// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_lab_change_attribution_info_button_color);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onMapReady(@NonNull final MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
// Change the color when the floating action button is clicked
findViewById(R.id.switch_attribution_color_fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (index == attributionColors.length) {
mapboxMap.setStyle(Style.LIGHT);
index = 0;
}
// Adjust map to the Maps SDK's Dark style
if (index == 2) {
mapboxMap.setStyle(Style.DARK);
}
// Adjust map to a custom monochrome dark green style to match the
// "i" attribution's dark green color
if (index == 5) {
mapboxMap.setStyle(new Style.Builder()
.fromUri(CUSTOM_GREEN_MONOCHROME_STYLE_URI));
}
// Change the color of the "i" attribution button
mapboxMap.getUiSettings().setAttributionTintColor(attributionColors[index]);
index++;
}
});
}
});
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
}