Examples

Change a layer's color

activity_style_color_switcher
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".examples.styles.ColorSwitcherActivity">
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="45.4385"
mapbox:mapbox_cameraTargetLng="12.338"
mapbox:mapbox_cameraZoom="17.4"/>
<LinearLayout
android:id="@+id/color_picker_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#ffffff"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingEnd="24dp"
android:paddingLeft="24dp"
android:paddingRight="24dp"
android:paddingStart="24dp"
android:paddingTop="24dp">
<Spinner
android:id="@+id/spinner_layer_picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/layer_spinner_array"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red"/>
<SeekBar
android:id="@+id/red_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="202"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Green"/>
<SeekBar
android:id="@+id/green_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="210"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Blue"/>
<SeekBar
android:id="@+id/blue_seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="211"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
ColorSwitcherActivity
package com.mapbox.mapboxandroiddemo.examples.styles;
import android.graphics.Color;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SeekBar;
import android.widget.Spinner;
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 com.mapbox.mapboxsdk.style.layers.FillLayer;
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor;
/**
* Using setPaintProperty to change a layer's fill color.
*/
public class ColorSwitcherActivity extends AppCompatActivity {
private MapView mapView;
FillLayer water;
FillLayer building;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 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));
// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_style_color_switcher);
final SeekBar redSeekBar = findViewById(R.id.red_seek_bar);
final SeekBar greenSeekBar = findViewById(R.id.green_seek_bar);
final SeekBar blueSeekBar = findViewById(R.id.blue_seek_bar);
final Spinner layerPicker = findViewById(R.id.spinner_layer_picker);
layerPicker.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if (layerPicker.getSelectedItem().toString().equals("Building")) {
if (building != null) {
redSeekBar.setProgress(Color.red(building.getFillColorAsInt()));
greenSeekBar.setProgress(Color.green(building.getFillColorAsInt()));
blueSeekBar.setProgress(Color.blue(building.getFillColorAsInt()));
}
} else if (layerPicker.getSelectedItem().toString().equals("Water")) {
if (water != null) {
redSeekBar.setProgress(Color.red(water.getFillColorAsInt()));
greenSeekBar.setProgress(Color.green(water.getFillColorAsInt()));
blueSeekBar.setProgress(Color.blue(water.getFillColorAsInt()));
}
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
redSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (water != null && layerPicker.getSelectedItem().equals("Water") && fromUser) {
water.setProperties(
fillColor(Color.rgb(progress, greenSeekBar.getProgress(), blueSeekBar.getProgress()))
);
} else if (building != null && layerPicker.getSelectedItem().equals("Building") && fromUser) {
building.setProperties(
fillColor(Color.rgb(progress, greenSeekBar.getProgress(), blueSeekBar.getProgress()))
);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
greenSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (water != null && layerPicker.getSelectedItem().equals("Water") && fromUser) {
water.setProperties(
fillColor(Color.rgb(redSeekBar.getProgress(), progress, blueSeekBar.getProgress()))
);
} else if (building != null && layerPicker.getSelectedItem().equals("Building") && fromUser) {
building.setProperties(
fillColor(Color.rgb(progress, greenSeekBar.getProgress(), blueSeekBar.getProgress()))
);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
blueSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (water != null && layerPicker.getSelectedItem().equals("Water") && fromUser) {
water.setProperties(
fillColor(Color.rgb(redSeekBar.getProgress(), greenSeekBar.getProgress(), progress))
);
} else if (building != null && layerPicker.getSelectedItem().equals("Building") && fromUser) {
building.setProperties(
fillColor(Color.rgb(progress, greenSeekBar.getProgress(), blueSeekBar.getProgress()))
);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyle(Style.LIGHT, new Style.OnStyleLoaded() {
@Override
public void onStyleLoaded(@NonNull Style style) {
water = (FillLayer) style.getLayer("water");
building = (FillLayer) style.getLayer("building");
}
});
}
});
}
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}
@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Was this page helpful?