3

I am using an Intent to launch an Autocomplete Activity from the Places SDK for Android, as described here, along the following lines:

Intent intent = new Autocomplete.IntentBuilder(
        AutocompleteActivityMode.FULLSCREEN, fields)
        .build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);

But I cannot see a way of styling the resulting Autocomplete Activity, to make it match the theme of my app as closely as possible, and in particular to follow the day/night theme being used in the app. At present the background of the places list always appears to be white, with dark text on top, i.e. suited only to a light theme.

For example the following is what it looks like, when launched from within an app running on a system set to dark theme:

enter image description here

How is this Activity meant to be styled?

drmrbrewer
  • 11,491
  • 21
  • 85
  • 181

2 Answers2

3

In colors.xml file make sure you have these defined:

<resources>
   <color name="colorPrimary">#ffffff</color>
   <color name="colorPrimaryDark">#03DAC5</color>
   <color name="colorAccent">#03d</color>
</resources>

In styles.xml you should have this defined:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
   <item name="colorPrimary">@color/colorPrimary</item>
   <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
   <item name="colorAccent">@color/colorAccent</item>
</style>

With that I was able to change the colors of the Places SDK.

colorPrimary is used for the background surrounding the EditText

colorPrimaryDark is used for the status bar

Styles & Screenshot

apmartin1991
  • 3,064
  • 1
  • 23
  • 44
  • What about the places list that appears below the search box? The screenshot I included in my OP already showed limited styling feeding through from the main activity, including `colorPrimary` for the search box itself (as you have noted), but in my OP I state: "At present the background of the places list always appears to be white, with dark text on top, i.e. suited only to a light theme". How on earth is that meant to be styled? Thanks! – drmrbrewer Apr 01 '20 at 10:06
1

According to the documentation there is something called AutoCompleteActivity that extends AppCompatActivity as well as AutocompleteSupportFragment that extends Fragment.

instead of using the IntentBuilder to create what i assume is just a generic version of AutocompleteActivity you should create your own AutocompleteActivity using the provided constructor and then override onCreate to set the Theme to dark. and then you just open the activity like you would with a regular intent.

Important note: in the documentation you will find that for some reason AutocompleteSupportFragment includes a lot more usefull methods that could be interesting for you (like setLocationRestriction(), setPlaceFields(), setTypeFilter etc) but for some reason these dont appear in the docs of AutoCompleteActivity. im not sure if they just forgot to put them in the doc or if the fragment version is actually more powerful than the Activity. this just means you might have to use the fragment (and plant it into an empty activity) instead of the activity.

quealegriamasalegre
  • 2,887
  • 1
  • 13
  • 35
  • I tried using an autocomplete fragment within my main activity, based on this: https://developers.google.com/places/android-sdk/client-migration#embed_an_autocompletefragment. But the autocomplete fragment doesn't seem to take on the theme of the main activity... (still light themed despite dark themed activity)... I thought it was meant to, without having to set the theme for the fragment explicitly in onCreate? See https://stackoverflow.com/a/15677267/4070848 – drmrbrewer Apr 01 '20 at 09:24
  • Still can't see what actual styling elements can be used to style the places list that appears... please could you specify which they are? And according to this: https://developers.google.com/places/android-sdk/autocomplete#option_2_use_an_intent_to_launch_the_autocomplete_activity it says "Caution: Do NOT launch the AutcompleteActivity directly. Instead, use an intent to launch Autocomplete (PlaceAutocomplete in the compatibility library)." – drmrbrewer Apr 01 '20 at 12:20
  • hmm, you are right, I hadnt read the warning about not calling AutocompleteActivity directly. but the fragment should still be an option. – quealegriamasalegre Apr 01 '20 at 16:08
  • look, the docs say that: By default, the fragment has no border or background. To provide a consistent visual appearance, nest the fragment within another layout element such as a CardView https://developers.google.com/places/android-sdk/autocomplete#add_autocompletesupportfragment_to_an_activity have you tried this? – quealegriamasalegre Apr 01 '20 at 16:10
  • Doing that only seems to affect the background of the search box that appears within the activity in which the fragment is embedded. But click in that search box merely opens up another search box (right at the top of the screen), with the dynamically-populated places list below it... that secondary search box and list are both styled in a light theme regardless of anything else. Looks a bit like the first image at: https://developers.google.com/places/android-sdk/autocomplete – drmrbrewer Apr 01 '20 at 16:37
  • hmm. well I cant think of an easy way to do it. looking at this https://stackoverflow.com/questions/36999647/how-to-customize-placeautocomplete-widget-dialog-design-to-list-places i get the impression that AutocompleteSupportFragment contains an Adapter that you might be able to override. it seems like the relevant theme is that of the adapter and not the fragment itself, which makes sense when you think about it. not sure if you want to Ctrl+Click on AutocompleteSupportFragment and do a bit of digging – quealegriamasalegre Apr 01 '20 at 17:12
  • maybe have look at thi https://stackoverflow.com/a/2119625/10637400 – quealegriamasalegre Apr 01 '20 at 17:31
  • Crikey, they sure don't make it easy. – drmrbrewer Apr 01 '20 at 18:31
  • i couldnt find a way to override the text color when a place is selected. in dark mode, the text can hardly be seen – chitgoks Nov 04 '21 at 13:14