-
Notifications
You must be signed in to change notification settings - Fork 493
Adding localization plugin example #593
Changes from 11 commits
0b4008f
adebcd8
be71947
7dfae81
834d3fb
d4a3ae8
acc3ed2
fd57d3e
878fc57
b7c7e04
b5e0ca1
83b8612
0c9f49f
adb9082
c28e6a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.mapbox.mapboxandroiddemo.examples.plugins; | ||
|
||
import android.os.Bundle; | ||
import android.support.design.widget.Snackbar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
|
||
import com.mapbox.mapboxandroiddemo.R; | ||
import com.mapbox.mapboxsdk.Mapbox; | ||
import com.mapbox.mapboxsdk.camera.CameraPosition; | ||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; | ||
import com.mapbox.mapboxsdk.geometry.LatLng; | ||
import com.mapbox.mapboxsdk.maps.MapView; | ||
import com.mapbox.mapboxsdk.maps.MapboxMap; | ||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | ||
import com.mapbox.mapboxsdk.plugins.localization.LocalizationPlugin; | ||
import com.mapbox.mapboxsdk.plugins.localization.MapLocale; | ||
|
||
/** | ||
* Use the localization plugin to retrieve the device's language and set all map text labels to that language. | ||
*/ | ||
public class LocalizationPluginActivity extends AppCompatActivity implements OnMapReadyCallback { | ||
|
||
private MapView mapView; | ||
private LocalizationPlugin localizationPlugin; | ||
|
||
@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_localization_plugin); | ||
mapView = findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
mapView.getMapAsync(this); | ||
} | ||
|
||
@Override | ||
public void onMapReady(final MapboxMap mapboxMap) { | ||
|
||
localizationPlugin = new LocalizationPlugin(mapView, mapboxMap); | ||
|
||
findViewById(R.id.language_one_cardview).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
localizationPlugin.setMapLanguage(MapLocale.ARABIC); | ||
} | ||
}); | ||
|
||
|
||
findViewById(R.id.language_two_cardview).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
localizationPlugin.setMapLanguage(MapLocale.RUSSIAN); | ||
} | ||
}); | ||
|
||
|
||
findViewById(R.id.language_three_cardview).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
localizationPlugin.setMapLanguage(MapLocale.SIMPLIFIED_CHINESE); | ||
} | ||
}); | ||
|
||
findViewById(R.id.match_map_to_device_language).setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
|
||
Snackbar.make(view, R.string.change_device_language_instruction, Snackbar.LENGTH_LONG).show(); | ||
|
||
try { | ||
localizationPlugin.matchMapLanguageWithDeviceDefault(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is potentially throwing the null point exception here? Isn't this a runtime exception? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed to runtime exception for when catch (RuntimeException exception) {
Snackbar.make(view, R.string.try_different_language_instruction, Snackbar.LENGTH_LONG).show();
} with <string name="try_different_language_instruction">Try setting your phone to a different language. German or French perhaps?</string>
👇 |
||
CameraPosition position = new CameraPosition.Builder() | ||
.target(new LatLng(34.032666, -80.363160)) | ||
.zoom(2.038777) | ||
.build(); | ||
|
||
mapboxMap.animateCamera(CameraUpdateFactory | ||
.newCameraPosition(position), 1000); | ||
} catch (NullPointerException exception) { | ||
Snackbar.make(view, exception.toString(), Snackbar.LENGTH_LONG).show(); | ||
} | ||
} | ||
}); | ||
|
||
} | ||
|
||
// Add the mapView lifecycle to the activity's lifecycle methods | ||
@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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportWidth="24.0" | ||
android:viewportHeight="24.0"> | ||
<path | ||
android:fillColor="#FFFFFF" | ||
android:pathData="M6.99,11L3,15l3.99,4v-3H14v-2H6.99v-3zM21,9l-3.99,-4v3H10v2h7.01v3L21,9z"/> | ||
</vector> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?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/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. frame layouts don't have orientation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:mapbox_cameraTargetLat="40.745565" | ||
app:mapbox_cameraTargetLng="-73.982058" | ||
app:mapbox_cameraZoom="11.316102" | ||
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets"> | ||
|
||
</com.mapbox.mapboxsdk.maps.MapView> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cleaner to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
|
||
<android.support.constraint.ConstraintLayout | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not just make the entire layout a constraintLayout? No need to have a FrameLayout as the parent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's what I originally had, but then decided to have the instructional cardview and buttons to be on top of the map in a frame layout, rather than the top of the map ending at the bottom of the buttons in a constraint layout... |
||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
|
||
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:id="@+id/instruction_cardview" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginTop="8dp" | ||
android:backgroundTint="@color/mapboxBlueDark" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent"> | ||
|
||
<TextView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_margin="16dp" | ||
android:text="@string/vacation_description" | ||
android:textColor="@color/mapboxWhite" /> | ||
|
||
</android.support.v7.widget.CardView> | ||
|
||
<android.support.v7.widget.CardView | ||
android:id="@+id/language_one_cardview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_marginTop="8dp" | ||
android:backgroundTint="@color/mapboxOrangeDark" | ||
app:layout_constraintEnd_toStartOf="@+id/language_two_cardview" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/instruction_cardview"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_margin="16dp" | ||
android:text="@string/arabic" | ||
android:textColor="@color/mapboxWhite" /> | ||
|
||
</android.support.v7.widget.CardView> | ||
|
||
<android.support.v7.widget.CardView | ||
android:id="@+id/language_two_cardview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:backgroundTint="@color/mapboxPurpleDark" | ||
app:layout_constraintEnd_toStartOf="@+id/language_three_cardview" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toEndOf="@+id/language_one_cardview" | ||
app:layout_constraintTop_toTopOf="@id/language_one_cardview"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_margin="16dp" | ||
android:text="@string/russian" | ||
android:textColor="@color/mapboxWhite" /> | ||
|
||
</android.support.v7.widget.CardView> | ||
|
||
|
||
<android.support.v7.widget.CardView | ||
android:id="@+id/language_three_cardview" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:backgroundTint="@color/mapboxGreenDark" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintHorizontal_bias="0.5" | ||
app:layout_constraintStart_toEndOf="@+id/language_two_cardview" | ||
app:layout_constraintTop_toTopOf="@id/language_two_cardview"> | ||
|
||
<TextView | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_margin="16dp" | ||
android:text="@string/mandarin" | ||
android:textColor="@color/mapboxWhite" /> | ||
|
||
</android.support.v7.widget.CardView> | ||
|
||
</android.support.constraint.ConstraintLayout> | ||
|
||
<android.support.design.widget.FloatingActionButton | ||
android:id="@+id/match_map_to_device_language" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="bottom|right" | ||
android:layout_marginBottom="16dp" | ||
android:layout_marginEnd="16dp" | ||
android:layout_marginRight="16dp" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:srcCompat="@drawable/ic_swap_horiz_white_24dp" | ||
mapbox:fabSize="small" /> | ||
|
||
|
||
</FrameLayout> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we get rid of the double spacings between click listeners?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed