Map does not fire Pointer Pressed/Release events - codenameone

I have a MapContainer instance that does not respond to attached PointerRelease listeners. I'm trying to detect when the map has stopped moving once it's been dragged by the user. In the code below the first two listeners do not produce any output. Third/Fourth do.
MapContainer mc = new MapContainer("mykey");
mc.addPointerPressedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Don't see this");
}
});
mc.addPointerReleasedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Don't see this either");
}
});
mc.addTapListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("See This :-)");
}
});
mc.addMapListener(new MapListener() {
#Override
public void mapPositionUpdated(Component source, int zoom, Coord centerCoord) {
System.out.println("See this too!");
}
});

addTapListener is correct and implemented in MapContainer. The low level pointer events won't work properly for that container because the map logic is implemented natively and thus these events are consumed.
You might be able to track them by binding a listener to the parent form but that's problematic as events/gestures might have platform specific interpretations.

Related

Codename One MapContainer.addDragFinishedListener() does not receive updates

Codename One, Adding a DragFinishedListener to the MapContainer class does not receive any events. Adding MapListener works as expected.
mapContainer.addDragFinishedListener(new ActionListener<ActionEvent>() {
#Override
public void actionPerformed(ActionEvent evt) {
System.out.println("Don't see this");
}
});
mapContainer.addMapListener(new MapListener() {
#Override
public void mapPositionUpdated(Component source, int zoom, Coord center) {
System.out.println("I see this");
}
});
That's a listener for drag and drop not for pan. If you drag a component e.g. a label onto a map that might be fired but it won't be fired for standard panning.
The best way is the map listener but you can also use pointer drag events and pointer release events.

Android Database operation in Asynctask blocks UI Thread

I'm performing a Database operation in the doInBackground method inside an AsyncTask on Android.
For some reason, the UI get blocked during the 5-6 seconds that the operation takes.
It does not make any sense for me, the operacion inside the doInBackground should not be executed in the UI Thread, right?
Here is my code:
private class CountItems extends AsyncTask<String, Void, Integer> {
private ProgressDialog dialog;
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(context);
dialog.setCancelable(false);
dialog.setMessage(getString(R.string.asynTask_loading));
dialog.show();
}
#Override
protected Integer doInBackground(String... params) {
// This operation takes 5-6 seconds.
return app.databaseSession().getMyObjectDao().count(selectedProject, filter, null, false);
}
#Override
protected void onPostExecute(Integer result) {
counterTextView.setText(String.valueOf(result));
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
I've made a test. If I put a Thread.sleep() inside the doInBackground method, it is executed in a different Thread without blocking the UI.
Like this:
#Override
protected Integer doInBackground(String... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 0;
}
Any ideas?
Thanks in advance.

CodenameOne MapContainer Zoom Level

I am using the MapContainer(cn1lib). so in android devices low relsolution the zoom works fine. But in android devices high resolution the zoom not works fine. The zoom in stay far. i attach a screen with the to max zoom in, it is a bug or i'm wrong?
SCREENSHOT
GUI-DESIGN
public class StateMachine extends StateMachineBase {
MapContainer mapContainer;
public StateMachine(String resFile) {
super(resFile);
// do not modify, write code in initVars and initialize class members there,
// the constructor might be invoked too late due to race conditions that might occur
}
/**
* this method should be used to initialize variables instead of the
* constructor/class scope to avoid race conditions
*/
protected void initVars(Resources res) {
}
#Override
protected void beforeShow(Form f) {
try {
this.mapContainer.setShowMyLocation(true);
this.mapContainer.zoom(new Coord(20.640086, -103.432207), 17);
this.mapContainer.setCameraPosition(new Coord(20.640086, -103.432207));
this.mapContainer.addMarker(
EncodedImage.createFromImage(fetchResourceFile().getImage("pin.png"), false),
new Coord(20.640086, -103.432207),
"Hi marker", "Optional long description",
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
}
);
this.mapContainer.addPointerDraggedListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
mapContainer.clearMapLayers();
mapContainer.addMarker(EncodedImage.createFromImage(fetchResourceFile().getImage("pin.png"), false), mapContainer.getCameraPosition(), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
super.beforeShow(f); //To change body of generated methods, choose Tools | Templates.
}
#Override
protected Component createComponentInstance(String componentType, Class cls) {
if (cls == MapComponent.class) {
this.mapContainer = new MapContainer();
return this.mapContainer;
}
return super.createComponentInstance(componentType, cls); //To change body of generated methods, choose Tools | Templates.
}
}
That is a MapComponent not a native map, so it uses the old open street maps support and relatively simple map rendering even on the device. We have support for native google maps which isn't exposed in the GUI builder but you can add it thru code.
This will embed the actual native GUI into place which will both look and feel better on the device although it will look the same on the simulator.

Unable to display toast messages

I have download and use the code form the following URL
https://github.com/Pmovil/Toast to display toast message.
Initially I got NativeToastImpl Not implemented error. I have resolved by coping the native related code to my project. Now the System throws Runtime Exception "Toast is not supported in this platform."
Here is my code to display toast message.
public class MyApplication {
private Form current;
private static Object context;
public void init(Object context) {
MyApplication.context = context;
}
public static Object getContext() {
return context;
}
public void start() {
if (current != null) {
current.show();
return;
}
showLoginForm();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
private void showLoginForm() {
Form form = new Form("WelCome ...");
Button b = new Button(" Login ");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Log.p(" Came hgere ");
Log.p(" *** " + MyApplication.getContext());
Toast.makeText(MyApplication.getContext(), "HI", Toast.LENGTH_LONG);
}
});
form.addComponent(b);
form.show();
}}
I have used Net Beans IDE for development, OS : windows 8.1
Please let me know I am doing wrong in this code and
Is there any other way to display toast messages using codename one?.
Thanks in advance
please edit the following code and please test the toast in device . Toast is not available in emulator.
public void init(Object context) {
this.context = context;
}
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Log.p(" Came hgere ");
Toast.makeText(context, "HI", Toast.LENGTH_LONG);
}
});
You missed the show() method on Toast.
Toast.makeText(MyApplication.getContext(), "HI", Toast.LENGTH_LONG).show();

How to hide form elements

I am new to codenameone, how can i hide form element in a button click?
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
//hide element
}
}
If you are using the latest Codenameone plugin, you can do:
myButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent evt) {
myElement.setHidden(true);
myElement.getParent().animateLayout(200);
}
}
OR
If the button has an actionEvent from GUI, do:
#Override
protected void onMyForm_MyButtonAction(Component c, ActionEvent event) {
findMyElement(c).setHidden(true);
findMyElement(c).getParent().animateLayout(200);
}
And to reveal it, do:
myElement.setHidden(false);
myElement.getParent().animateLayout(200);
Use setVisible(boolean visible) (see https://www.codenameone.com/javadoc/com/codename1/ui/Component.html)

Resources