Home » Android » android – PlacePicker doesn't pick up material theme

android – PlacePicker doesn't pick up material theme

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using a PlacePicker library from Google Play Services which starts up a new activity. The new activity/picker has a toolbar (actionbar) which is not styled by default.

PlacePicker documentation states that

If you set custom colors in your application using the material theme,
the place picker inherits the colorPrimary and colorPrimaryDark
attributes from the theme.

I have a theme in my style.xml file:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">#5665bb</item>
    <item name="android:colorPrimary">#5665bb</item>
    <item name="colorPrimaryDark">#41456b</item>
    <item name="android:colorPrimaryDark">#41456b</item>
</style>

and I have set the theme to be used in my Android Manifesto file

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

The placepicker is created by the following code:

try {
    PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
    Intent intent = intentBuilder.build(Main.this);
    // Start the intent by requesting a result,
    // identified by a request code.
    startActivityForResult(intent, REQUEST_PLACE_PICKER);
} catch (GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e) {
    Log.e("", "Error with Google Play lib.");
}

However, the toolbar doesn’t get styled. As before it has a white background and black text.
It’s interesting to note that my own toolbar (actionbar) does get styled.

How do I force the placepicker activity to adopt my theme?

How to&Answers:

This is an acknowledged issue by the development team.

There is currently a known issue with setting custom theme colors on
the PlacePicker. For now you can work around this by defining two
colors with the names “primary” and “primary_dark” – these will be
applied to the PlacePicker.

Unfortunately setting the primary/primaryDark property on a theme will
not affect the PlacePicker styling at the moment.

EDIT:
Seems to be fixed now.

This should be fixed in Google Play Services 10.0. Place Picker and
Autocomplete Widget will use the colorPrimary and colorPrimaryDark
colors from your app.

Answer:

As is mentioned in the documentation that you provided in your question:

If you set custom colors in your application using the material theme,
the place picker inherits the colorPrimary and colorPrimaryDark
attributes from the theme.

However, you have not specified the actual Material theme (Theme.Material.Light) in your style.xml file; rather, you have utilized Theme.AppCompat.Light which, while it is designed to look and operate the same as the Material theme, is a support library designed to provide backward compatability for the Material theme all the way to Android 2.1 (API 7). My guess would be that the PlacePicker library only inherits the colorPrimary and colorPrimaryDark attributes from the true Material theme and not the support library.

An easy way to test this would be to modify your style.xml file to use the real Material theme and see if this works:

<style name="AppTheme" parent="android:Theme.Material">
    <item name="android:colorPrimary">#5665bb</item>
    <item name="android:colorPrimaryDark">#41456b</item>
</style>

The only drawback of course is that the Material theme is only available in API ≥ 21, so, if this works, then you would be restricted to devices that utilize Lollipop only.

Lastly, as a sidenote, the Theme.AppCompat support libraries do not use the android: namespace, so your original style.xml file can be shortened as follows:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">#5665bb</item>
    <item name="colorPrimaryDark">#41456b</item>
</style>

Answer:

To fix issues toolbar theme android Place Picker on OS under Lollipop

PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder(); 
Intent intent = intentBuilder.build(getActivity()); 
    // if Build version sdk is under Lollipop, add intent extra
    (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        intent.putExtra("primary_color", getResources().getColor(R.color.colorPrimary));
        intent.putExtra("primary_color_dark", getResources().getColor(R.color.colorPrimaryDark));
    }

//start intent
startActivityForResult(intent, REQUEST_PLACE_PICKER_CODE);