I was trying to close the default infowindow in the android map.
I used .hideInfoWindow() but nothing appends.
Thanks.
Change the return statement
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
(to)
#Override
public boolean onMarkerClick(Marker marker) {
return true;
}
Use
mapa.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
marker.hideInfoWindow();
}
});
I hope that helps.
I assume you want to close the infowindow when you click the marker for the second time. It seems you need to keep track of the last clicked marker. I can't get it to work by simply checking if the clicked marker is currently shown and then close it, but this works nicely.
private Marker lastClicked;
private class MyMarkerClickListener implements OnMarkerClickListener {
#Override
public boolean onMarkerClick(Marker marker) {
if (lastClicked != null && lastClicked.equals(marker)) {
lastClicked = null;
marker.hideInfoWindow();
return true;
} else {
lastClicked = marker;
return false;
}
}
}
Setting the pin title and snippet to null, will result in no info window being displayed. This shall cover both cases when selecting and clicking a pin.
Related
I want to display some toast when my button is clicked once and other toast when my button is clicked twice. Is there any built in library or do I need to implement custom logic?
You can Gesture Detector for detecting double taps.
final GestureDetector mDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener {
#Override
public boolean onDown(MotionEvent e) {
return true;
}
#Override
public boolean onDoubleTap(MotionEvent e) {
return true;
}
});
For more details you can check this documentation.
Answer given by #TaranmeetSingh is absolutely right but there is a better solution. You can use this library to do it.
How to implement
Add this library -> implementation 'com.github.pedromassango:doubleClick:CURRENT-VERSION'(Version is given above).
Instead of using an onClickListenerObject for onClickListener, you can use DoubleClickListener Check the example below
Button btn = new Button(this);
btn.setOnClickListener( new DoubleClick(new DoubleClickListener() {
#Override
public void onSingleClick(View view) {
// Single tap here.
}
#Override
public void onDoubleClick(View view) {
// Double tap here.
}
});
This is the sample code from Here API. But the problem I have is that the initial location where the map is loaded is wrong. What I need is for the map to be displayed where you are.
What is the method that modifies the position of the map?
private MapViewLite mapView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a MapViewLite instance from the layout.
mapView = findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
}
private void loadMapScene() {
// Load a scene from the SDK to render the map with a map style.
mapView.getMapScene().loadScene(MapStyle.NORMAL_DAY, new MapScene.LoadSceneCallback() {
#Override
public void onLoadScene(#Nullable MapScene.ErrorCode errorCode) {
if (errorCode == null) {
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915));
mapView.getCamera().setZoomLevel(14);
} else {
Log.d(TAG, "onLoadScene failed: " + errorCode.toString());
}
}
});
}
This is the piece of code that hard codes the position:
mapView.getCamera().setTarget(new GeoCoordinates(52.530932, 13.384915))
Instead, set the coordinates to be the user's click. In order to do so, you will need to surround that piece of code with an onTouch() listener and check the action of the user. I'll use an ImageView as an example. So something like this:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN){
//whatever you want to do
//to fetch the coordinates of the user's click use event.getX() and event.getY()
}
return true;
}
});
I can set the state of the qs tile using the onClick() method but I would also like to toggle its state using a switch in my Main Activity. However, I can't change the state of the tile using a switch because getQsTile returns null outside the onClick() method.
public class AwesomeTileService extends TileService{
Tile tile;
public Activity activity;
public AwesomeTileService(Activity activity){
this.activity=activity;
Switch switch1 = (Switch)this.activity.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
tile=getQsTile(); //tile is null
SwitchState(); //SwitchState does not work with onCheckedChanged
Log.v("Switch State=", ""+isChecked);
}
});
}
public AwesomeTileService(){
//zero arg constructor
}
#Override
public void onTileAdded() {
tile=getQsTile();
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
}
#Override
public void onClick() {
tile=getQsTile(); //tile is not null
SwitchState(); //SwitchState method works with onClick
}
public void SwitchState(){
if(tile!=null){
if (tile.getState() == Tile.STATE_INACTIVE) {
tile.setState(Tile.STATE_ACTIVE);
}
else{
tile.setState(Tile.STATE_INACTIVE);
}
tile.updateTile();
}
}
}
I can simply remove the switch from the app but I'm curious to know what the issue is here and how I can overcome it.
If you want to update the tile programmatically from another activity, use sharedPreferences and store boolean value and write code in onStartListening method in tile service class to update tile. (onStartListening is called every time you swipe down the panel).
Right now I have markers that when clicked, begin autorefreshing the contents of their info windows in the background.
The thing is, once the user closes the info window (like clicking elsewhere on the map), the background task still goes on.
Is there a way to detect when a marker InfoWindow is closed so I can close the task then?
You can only have a single InfoWindow open at a time, which means you can track the one that's open:
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
Now on each map click you can check if the task on a specific marker is still running:
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
if (mLastMarker != null) {
mLastMarker = null;
// Stop task
}
}
});
If you want, you can also stop the tasks when changing the InfoWindow:
#Override
public View getInfoWindow(Marker marker) {
if (mLastMarker != null) {
// Stop task for mLastMarker
}
Log.e(TAG, "Info window requested for " + marker);
mLastMarker = marker;
return null; // Returning null will load the default InfoWindow
}
You could use setOnMapClickListener on the map and within the onMapClick method you could check whether the task is still running and if yes, end the task.
I have ImageViews inside of a GridView, I had been using an OnItemClickListener along with an OnItemLongClickListener set on the GridView to open the image on a larger page and to delete the item respectively. Now, I have to implement rearranging of the ImageViews in the GridView, so I plan to move the deletion function to a double tap gesture, (please do not lecture me on android style guidelines (including the possibility of contextual actionbars, which I suggested), as this is what my boss asks for to emulate functions inside our ios app) in order to reserve long click for the drag and drop. I set an OnTouchListener on each view in the getView of my custom adapter, feeding a GestureDetecter with a listener extending SimpleOnGestureListener the given MotionEvent with onTouchEvent. I know what to do up to that point, but when I included (onDown of course, to get other callbacks) onDoubleTap, onSingleTapConfirmed, and onLongPressed all taps were interpreted as long clicks. And when I removed the both callback methods to be replaced with their listener counterparts once again (ie OnItemClickListeners) I received those two gestures but not the double tap, which makes sense, as double taps start out as a single tap unless you wait for a bit less than a second to confirm them as singles rather than potential doubles. I also tried placing the OnItemClickListener, but not the OnItemLongClickListener, with the callback in the extended SimpleOnGestureListener. In this case, only long presses were ever interpreted, but other gestures caused no response. Here is my code as it stands now, and do note that I returned false in the onTouchEvent in order to allow others (itemclicklisteners) to consume the events following the attempts made in the GestureDetector.
public class MainBoardGridAdapter extends GenericBoardGridAdapter implements OnItemLongClickListener {
private class Ges extends GestureDetector.SimpleOnGestureListener {
int pos;
public Ges(View v) {
pos = (Integer) v.getTag();
}
#Override
public boolean onDown(MotionEvent me) {
//this does get called but none of these methods below
return true;
}
#Override
public boolean onDoubleTap(MotionEvent me) {
new DeleteConfirmationPrompt(c, "board") {
#Override
protected boolean onDeleteConfirmed() {
// delete the visionboard
return deleteBoard(pos);
}
}; // Constructor shows dialog
return false;
}
#Override
public boolean onSingleTapConfirmed(MotionEvent e) {
MainBoardGridAdapter.super.flagForUpdate(pos);
if (listener != null) {
listener.onBoardClick(pos, getName(pos));
} else {
Intent intent = new Intent(c, VisionBoardActivity.class);
intent.putExtra(VisionBoardActivity.EXTRA_VISION_BOARD_NAME, getName(pos));
frag.startActivityForResult(intent, MyBoardsFragment.REQUEST_EDIT);
}
return false;
}
}
#Override
public boolean onItemLongClick(AdapterView<?> parent, View v,
final int pos, long id) {
Toast.makeText(c, "Long", Toast.LENGTH_LONG).show();
return false;
}
// called by getView of extended adapter
#Override
public void onImageLoaded(ImageView iv, String data, View root) {
iv.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
(new GestureDetector(c, (new Ges(v)))).onTouchEvent(event);
return false;
}
});
}
}
And in the Activity, gv is my GridView:
gv.setOnItemLongClickListener(gridAdapter);
Also note that I had been using true in the return value in the GestureDetector methods, until trying the current configuration.There was no difference to be seen.
Thank you for your valuable time and help, I hope that someone will be able to point out what I am doing incorrectly.
-Jackson