0

I want to use google place picker in my app. I have followed this link https://developers.google.com/places/android-api/placepicker and added place picker in my fragment but it dose not get the color or theme of my app. The toolbar of place picker shows white in color.

Its written in document that the picker takes colorPrimary from the material theme of app. I also have a colorPimary and colorPrimaryDark im my style.xml.

What's going wrong here?

code:

public class NameOfBusinessFragment extends Fragment {

    int PLACE_PICKER_REQUEST = 1;
    int RESULT_OK = -1;
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
    EditText category,location;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);


        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_name_of_business,
                    container, false);

            location = (EditText)view.findViewById(R.id.location);

            location.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {

                        startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
                    }
                    catch (GooglePlayServicesRepairableException e)
                    {
                        Toast.makeText(getActivity(),"ServiceRepaire Exception",Toast.LENGTH_SHORT).show();
                    }
                    catch (GooglePlayServicesNotAvailableException  e)
                    {
                        Toast.makeText(getActivity(),"SeerviceNotAvailable Exception",Toast.LENGTH_SHORT).show();
                    }
                }
            });

            return view;
        }
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {
                Place place = PlacePicker.getPlace(data, getActivity());
                String toastMsg = String.format("Place: %s", place.getName());
                Toast.makeText(getActivity(), toastMsg, Toast.LENGTH_LONG).show();
                location.setText(place.getName());
            }
        }
    }
}

Style.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBarOverlay">false</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowBackground">@android:color/white</item>
    <item name="android:windowContentOverlay">@null</item>

    <item name="android:editTextStyle">@style/EditTextStyle</item>

</style>

<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

How to get this?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Sid
  • 2,792
  • 9
  • 55
  • 111
  • You got any solution for this problem? Apparently I also facing the same issue on 4.2.2. devices. It seems to work fine on 5.1.1 devices. – Obscure Geek Jun 15 '16 at 10:04

1 Answers1

0

Try replacing

startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);

with:

Intent placePickerIntent = builder.build(getActivity());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    placePickerIntent.putExtra("primary_color", ContextCompat.getColor(this, R.color.colorPrimary));
    placePickerIntent.putExtra("primary_color_dark", ContextCompat.getColor(this, R.color.colorPrimaryDark));
}
startActivityForResult(placePickerIntent, PLACE_PICKER_REQUEST);

The place picker doesn't automatically pick color from the theme. It is a bug as discussed here. Also note that this is a hack and not an actual clean solution.

Obscure Geek
  • 749
  • 10
  • 22
  • I tried by only starting an intent for the other app, and it picked the theme color automatically on same device. No idea what's wrong for other app i have same styles with both the apps. @Obscure Geek – Sid Jun 22 '16 at 05:35
  • Interesting. Same style and same device. Are the design and app-compat library versions also same? – Obscure Geek Jun 22 '16 at 07:26
  • yes, I have just changed the package name of the app, And further changed the activities and design as needed. and when I tried to implement place picker same as previous app, it worked. Still not working for previous app. @Obscure Geek – Sid Jun 22 '16 at 08:08
  • You tried the posted solution for the previous app? – Obscure Geek Jun 22 '16 at 08:09
  • could you please help me with this question?http://stackoverflow.com/questions/37959739/how-to-get-contacts-photo-by-contacts-number @Obscure Geek – Sid Jun 22 '16 at 08:22