-2

fragment_maps.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MapsFragment">
    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <Button
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:focusable="true"
        android:text="Pickup Location">
    </Button>
</FrameLayout>

MapsFragment.java

i think due to improper implementaion of xml of SupportMapFragment or Framelayout ,onclick of button working sometimes and sometimes not

       @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_maps, container,false);
            button = (Button) view.findViewById(R.id.hello);

            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();
                }
            });

            location_permissions = new location_permissions(getContext());
            locationCallback = new LocationCallback();
            locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
            fusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
            locationRequest = LocationRequest.create();
            locationRequest.setInterval(10000);
            locationRequest.setFastestInterval(5000);
            locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
            SettingsClient client = LocationServices.getSettingsClient(getContext());
            mtask = client.checkLocationSettings(builder.build());
            supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
            requestpermission();

            // google maps
            supportMapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
               // request GPS turn on if Gps is off
                    requestGPSCheck();
               // gets device current location & animate to current Latlong
                    callback();

                }
            });

            return view;
        }  

I have set Toast message on Button OnClick event,it is not working always may be due to focus?

1 Answers1

0

Instead of using

Toast.makeText(getContext(),"Clicked",Toast.LENGTH_SHORT).show();

try using

Toast.makeText(getActivity().getApplicationContext(),"Clicked",Toast.LENGTH_SHORT).show();

getContext() uses the current view as context, so if the map isn't loaded yet it may cause the context to be not available yet. getApplicationContext() instead uses the application's context which is already available by the time this fragment is loaded.

Try and let me know.

GauthamK
  • 174
  • 2
  • 12