I am trying to toggle a heatmap on and off using Android.
I have two lists say:
List<LatLng> a = new ArrayList<>
List<LatLng> b = new ArrayList<>
a.add(new LatLng(53.457131,-1.054688);
a.add(new LatLng(53.036407,-4.570313);
a.add(new LatLng(52.182504, -1.054688);
b.add(new LatLng(45.238325,2.460938);
I combine these two lists:
finalList.addAll(a);
finalList.addAll(b);
I want to toggle these lists in the TileOverlay.
aButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (overlay.isVisible()) {
finalList.removeAll(a);
} else {
finalList.addAll(a);
}
providor.setData(a);
overlay.clearTileCache();
}
and I do the same to the second button.
But once you remove both lists (so finalList is empty) I get the following error:
java.lang.IllegalArgumentException: No input points
I would like to know if there is a easy way to implement this.
I have tried to implement this meathod:
On button click check if finalList.isEmpty() and remove the overlay if it is.
But then that brings its own problems as then I would have to create new overlay.
I also tried to set the overlay to visible but then that messes up the buttonClick Listener as it checks for overlay.isVisible()
Thanks
Related
In my application I have an ArrayList markers, exactly:
public ArrayList<Marker> listMarker = new ArrayList<Marker>();
public int p = listMarker.size();
public Marker singleMarker;
I put markers in ArrayList as follows:
MarkerOptions options = new MarkerOptions()
.position(latLng);
listMarker.add(p, singleMarker = nMap.addMarker(options));
I was looking for at site the same problem and I have not found
Now I try to do that every button click will remove the last marker from the map and list. If I have 5 markers that click is 4, the next click is 3 and so on. But not only from the list, and also from the map, the biggest problem
My attempt that does not work like this:
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
listMarker.remove(k);
}
});
Button is not removed Marker from the map. I suspect that this only removes the last marker from the list but no from the map and do not know how to deal with it.
Currently you are only removing the marker from the List, the Map is going to hold onto it until you explicitly remove it... Call remove on the Marker to remove it from the map, like so:
deleteButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (listMarker != null && !listMarker.isEmpty()) {
listMarker.get(k).remove(); // Add this line
listMarker.remove(k);
}
}
});
I've tried a lot of different ways, most of the suggestions found here, but none of them seems to work. What I'm trying to achieve is at chat area below my game area, a SurfaceView. It is supposed to scroll upwards as new lines are added to the textview.
At first, it looks like a really simple task, but having tried all kinds of suggestions, like a TextView in a ScrollView, like a TextView in a TableRow in a TableLayout in a ScrollView, and so on...I've still not made it happen. Of course this must be something easily achieved in Android, right??
The task is to display like 6 lines of text in the bottom of the screen, and as a new message is added last it should scroll the rest upwards, like a terminal window. The important thing is that it should add the latest message after the other and, when reached the bottom line, scroll the text upwards and add the new line(s) at the end.
Any kind of help or suggestions would be highly appreciated!!
I needed the same behavior in one of my apps and I achieved in just with one command:
view.setGravity(Gravity.BOTTOM);
Or, analogously, setting this attribute in your layout:
android:gravity="bottom"
Then simply add your lines using:
your_text_view.append(newLine);
Suppose, you declared your ScrollView as follows...
private ScrollView mScrollView;
you initialized it as...
mScrollView = (ScrollView) findViewById(R.id.scroll_view_chat_window);
Now, create a method to perform scroll down when you call the method. Inside the method implement a thread which will do the scroll down independently. And call the method after every chat message update thats will do the auto-srcoll functionality.
private void scrollDown() {
mScrollView.post(new Runnable() {
#Override
public void run() {
mScrollView.smoothScrollTo(mScrollView.getScrollY(), mScrollView.getScrollY()
+ mScrollView.getHeight());
}
});
}
I've achieved this (crudely!) by maintaining my own list, deleting the lowest element then adding at the end each time. Here i've just got a 3 line window:
public class MessageWindow {
private ArrayList <String> msgs;
private Activity parentActivity;
public MessageWindow(Activity act, int allMsgsMax) {
this.parentActivity = act;
msgs = new ArrayList <String> ();
// create empty list elements for initial display
for (int i = 0; i < allMsgsMax; i++){
msgs.add("");
}
}
//
public void put (String msg){
msgs.remove(0);
msgs.add(msg);
// get a handle to the textview 'messages', a 3-line box
TextView t2v = (TextView) parentActivity.findViewById(R.id.messages);
// crappy but you get the idea:
t2v.setText(msgs.get(0) + "\n" + msgs.get(1) + "\n" + msgs.get(2) );
}
then in the activity:
protected MessageWindow messageWindow;
// setup splash screen
messageWindow = new MessageWindow(this, 3);
// write some stuff - row1 will disappear off the top of the box
messageWindow.put ("row1")
messageWindow.put ("row2")
messageWindow.put ("row3")
messageWindow.put ("row4")
The question really is that simple. I have an activity with a button that randomly changes the text of a textview box. How do I add another button that gathers the previous number generated so the previous textview text comes back - for a quotes app.Is there a feature I require? I have searched, but I cannot find a feature that will 'go back' on the random number generator.
Thank you in advance.
You dont need to go back on the generator itself, just remember the previous values. Store it in a variable. Here we are using prev as an array, because we need it to be final so we can access it in the anonymous inner classes (click handlers). But we also need to change it. So we use the prev[0] value.
Here I am assuming you have a button called nextNum which generates the next random number. But that could be anything, it doesnt matter. When you generate a new random number simply update the 0th element of the prev array and you should be fine.
final String[] prev = new String[] { "" }
#Override
public void onCreate(Bundle bundle) {
// ....
Button BackQuote = (Button)findViewById(R.id.back);
final TextView display = (TextView) findViewById(R.id.textView1);
// number generator button
Button nextNum = (Button) findViewById(R.id.random);
nextNum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// generate a random number
// store it in prev
prev[0] = rndNum.toString();
}
});
BackQuote.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
display.setText(prev[0]);
}
});
}
Unless there is a specific reason you want to avoid this, then this would work perfectly fine. And there is no way to go back on a random generator. By definition it IS random! (Yes, Pseudo-random. Nonetheless)
So if you want to keep an history, you are going to have to implement those yourself. I have yet to see a random generator which has a history feature. I'd be surprised if i do too.
I am trying to allow a User to remove a point (or more specifically, an OverlayItem) from a map. I followed the developer tutorial to get started and implemented the CustomMapView in this tutorial to capture a long press on the map.
So now I have a program which allows a User to place points on the map. My next goal is to let the User remove points. Here is my code for when a User clicks an existing point on the map.
public class OurItemizedOverlay extends ItemizedOverlay {
//Create new list of points
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context mapContext;
#Override
protected boolean onTap(final int index) {
Button edit, remove;
//Get index of item tapped
OverlayItem item = mapOverlays.get(index);
//Create Dialog to show point info, allow for edit or removal.
LinearLayout layout = new LinearLayout(mapContext);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater inflater = LayoutInflater.from(mapContext);
AlertDialog.Builder builder = new AlertDialog.Builder(mapContext);
builder.setTitle(item.getTitle());
builder.setMessage(item.getSnippet());
View view = inflater.inflate(R.layout.view_or_edit_location_dialog, null);
builder.setView(view);
builder.show();
//BUTTONS
edit = (Button)view.findViewById(R.id.edit);
remove = (Button)view.findViewById(R.id.delete);
//Edit Button Listener
edit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
//Remove Button Listener
remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeOverlay(index); <--------
Log.d("View location info", "user clicked delete.");
return;
}
});
return true;
}
Here is my code for removeOverlay.
protected void removeOverlay(int index) {
mapOverlays.remove(index);
com.example.mapproject.MainActivity.mapView.invalidate();
}
After I click on an existing point a dialog is presented offering to remove the point. When I have selected to remove the point, the point still remains on the screen. If I place a new point, the 'deleted' one is removed. However, if I click the 'deleted' point or another existing point, the program crashes with this error.
If you have a clue of what to do, I'd appreciate to hear from you !!
Edit
Following a tip from Vishwa Patel, I remove a point from the map straight away using postInvalidate(). However, I still get indexoutofbounds exceptions when I click where the icon was..
You probably need to call invalidate() on your MapView, forcing it to re-draw itself. As the commenters mentioned, you may also need to re-call populate(). Your app is probably crashing because it's trying to call onTap() for an OverlayItem that doesn't exist. You may also want to try any method that could "refresh" the MapView and/or Overlay, because that is what you need to do to make the OverlayItem disappear.
Try using
com.example.mapproject.MainActivity.mapView.postInvalidate();
since you are making an invalidate call from a non-UI thread, as specified in the documentation for postInvalidate();
I believe I found the solution here. It seems to work so far, the answer was to put the following line into my removeOverlay method,
setLastFocusedIndex(-1);
The code to remove an OverylayItem from my custom Overlay is,
protected void removeOverlay(OverlayItem overlayItem) {
mapOverlays.remove(overlayItem);
MainActivity.mapOverlays.remove(this);
setLastFocusedIndex(-1);
populate();
}
Any thoughts/suggestions are welcome!
I have 2 markers on the map and i want to delete them when the user clicks on a button. This is my method:
public void deleteAllMarkers() {
if(mapView.getOverlays().size() !=0) {
//Log.d("MAPA ",Integer.toString(mapView.getOverlays().size()));
for (int i=0; i<mapView.getOverlays().size(); i++ ) {
mapView.getOverlays().remove(i);
}
mapView.postInvalidate();
}
}
The problem is that i have to press my button twice to get rid of both markers, because after the first press only 1 marker disappears.
What am i doing wrong?
.size() will get re-evaluated on each iteration, i.e. after you've removed element 0.
It would be easier to write:
mapView.getOverlays().clear();
The more fair solution is removing only Markers without any other layouts (like Compas, Copyright, etc)
mapView.overlays
.forEach { (it as? Marker)?.remove(mapView) }