Reputation: 133
I want to open a snackBar after that I clicked on marker I tr do this :
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
for(Point p: points){
if(p.getFull().equalsIgnoreCase(marker.getTitle())){
view = getCurrentFocus();
mySnackbar(p,view);
}
}
return false;
}
});
public void mySnackbar(Point p, View view) {
LinearLayout.LayoutParams objLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Snackbar snackbar = Snackbar.make(view, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setPadding(0, 0, 0, 0);
LayoutInflater mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View snackView = getLayoutInflater().inflate(R.layout.my_snackbar, null);
Button textViewOne = (Button) snackView.findViewById(R.id.txtOne);
TextView tvName = (TextView) snackView.findViewById(R.id.name);
tvName.setText(p.getFull());
textViewOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("One", "First one is clicked");
snackbar.dismiss();
}
});
Button textViewTwo = (Button) snackView.findViewById(R.id.txtTwo);
textViewTwo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i("Two", "Second one is clicked");
}
});
layout.addView(snackView, objLayoutParams);
snackbar.show();
}
But my application is crashed and in the log I see :
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
Ok I do this and it works:
getWindow().getDecorView(),
But now when show Snackbar
I want to see the whole map, this Snackbar
cover my map
Upvotes: 0
Views: 369
Reputation: 1206
I created a custom snackbar , where the views are wrapped within a CoordinatorLayout for a Fragment.
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getTag() != null) {
marker.showInfoWindow();
int tag = (int) marker.getTag();
showCustomSnackBar(marker, 10000, tag);
}
return false;
private void showCustomSnackBar(Marker marker, int duration, int tag) {
if (getActivity() != null && !(getActivity()).isFinishing()) {
CoordinatorLayout rootCoordinatorLayout = view.findViewById(R.id.parent_coordinator_layout);
Snackbar snackbar = Snackbar.make(rootCoordinatorLayout, "", duration);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
layout.setBackgroundColor(getResources().getColor(R.color.white));
LayoutInflater objLayoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View snackView = objLayoutInflater.inflate(R.layout.snackbar_layout_view, null);
ImageView someImgView = snackView.findViewById(R.id.someImageView);
TextView sometxt = snackView.findViewById(R.id.snackbar_msg);
someImgView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
snackbar.dismiss();
}
});
layout.addView(snackView, 0);
snackbar.show();
}
}
}
Upvotes: 0
Reputation: 4087
try this
Use this function to show the message.
public void showMessage(Activity context, String message) {
Snackbar snackbar = Snackbar.make(context.getWindow().getDecorView().findViewById(android.R.id.content), message, Snackbar.LENGTH_LONG);
View snackBarView = snackbar.getView();
snackBarView.setBackgroundColor(context.getResources().getColor(R.color.colorPrimary));
TextView textView = (TextView) snackBarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(context.getResources().getColor(R.color.white));
snackbar.show();
}
For your UseCase
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
for(PGO pgo : pgoList.pgos){
if(pgo.getFull().equalsIgnoreCase(marker.getTitle())){
currentPGO = pgo;
showMessage(MainActivity.this,pgo.getFull());
}
}
return false;
}
});
Upvotes: 0
Reputation: 3766
If you don't have a view on focus you can use the default android one as follows:
view = findViewById(android.R.id.content);
Upvotes: 0