I cannot get a toast to display in a fragment. I am using the PlacePicker, the user selects a place and the toast is supposed to show the place they selected. I have tried getContext()
, getActivity().getApplicationContext()
& getActivity()
but none are working.
The PlacePicker is loading fine it just wont display the toast. I would also like to display the place the user has chosen in a TextView but the toast is the main priority.
My code is below:
public class Tab1 extends Fragment {
int PLACE_PICKER_REQUEST = 1;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1, container, false);
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(getActivity()), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Place place = PlacePicker.getPlace(data, getActivity());
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(getContext(), toastMsg, Toast.LENGTH_LONG).show();
}
}
}
}