How to hide form elements - codenameone

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)

Related

Map does not fire Pointer Pressed/Release events

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.

RecyclerView: how to start Contextual Action Bar CAB with a Checkbox?

I have a RecyclerView list of CardViews and am using AppCompatActivity. Each CardView has a checkbox. When I click on the checkbox I would like to start a Contextual Action Bar. I would like to use an OnCheckedChangeListener and am having no luck. The checkmark correctly becomes visible when the "chkSelected" Checkbox is clicked on and it becomes invisible when the "chkSelected is clicked on again. What am I missing here?
public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...
private static ActionMode.Callback actionModeCallback = new ActionMode.Callback() {
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return false;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
}
};
...
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
final ItemHolder itemHolder = new ItemHolder(view);
return itemHolder;
}
private static class ItemHolder extends RecyclerView.ViewHolder {
private CheckBox chkSelected;
private ItemHolder(View itemView) {
super(itemView);
chkSelected = (CheckBox) itemView.findViewById(R.id.chkSelected);
chkSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
mActionMode = ((AppCompatActivity) buttonView.getContext()).startSupportActionMode(actionModeCallback);
}
});
}
I also tried an OnClickListener() in the ItemHolder() with no luck. Code is below. The Toast in the onClick() is showing properly so there must be something wrong with the startSupportActionMode().
chkSelected.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Checkbox was clicked",Toast.LENGTH_LONG).show();
if(mActionMode == null) {
// Start the Contextual Action Bar (CAB) using the ActionMode.Callback defined above
mActionMode = ((AppCompatActivity) view.getContext()).startSupportActionMode(mActionModeCallback);
}
}
});
Solution was to set up a method in onBindViewHolder() that would update the item views for the OnClickListeners in Itemholder().

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 pass control from GUI created form to code created form in codenameONe

I have created a form using GUI builder and another form via code which loads the google map. There is a button in the form which was created by using GUI builder, I need to go to the map while clicking the button. How to do it ? please help.
Add an actionListener to your button and call the new form in the method generated in StateMachine.
Your form should be created in StateMachine.
For example:
protected void onPage2_ButtonAction(Component c, ActionEvent event) {
Form hi = new GmapForm("Native Maps Test");
hi.show();
}
Gmap Form Class:
public class GmapForm extends Form {
public GmapForm(String title) {
t.setTitle(title);
setLayout(new BorderLayout());
final MapContainer cnt = new MapContainer();
addComponent(BorderLayout.CENTER, cnt);
addCommand(new Command("Move Camera") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-33.867, 151.206));
}
});
addCommand(new Command("Add Marker") {
public void actionPerformed(ActionEvent ev) {
System.out.println("Marker");
try {
cnt.setCameraPosition(new Coord(41.889, -87.622));
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
addCommand(new Command("Add Path") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-18.142, 178.431));
cnt.addPath(new Coord(-33.866, 151.195), // Sydney
new Coord(-18.142, 178.431), // Fiji
new Coord(21.291, -157.821), // Hawaii
new Coord(37.423, -122.091) // Mountain View
);
}
});
addCommand(new Command("Clear All") {
public void actionPerformed(ActionEvent ev) {
cnt.clearMapLayers();
}
});
revalidate();
}
Remove the above code from your Gmap class and Uncomment // new StateMachine("/theme");
// Map loading class
import com.codename1.io.Log;
import com.codename1.ui.events.ActionEvent;
import com.codename1.ui.events.ActionListener;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import userclasses.StateMachine;
import com.codename1.maps.Coord;
import com.codename1.ui.Command;
import com.codename1.ui.Dialog;
import com.codename1.ui.EncodedImage;
import com.codename1.ui.layouts.BorderLayout;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import java.io.IOException;
public class Gmap {
private Form current;
public void init(Object context) {
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
try {
Resources theme = Resources.openLayered("/theme");
UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
} catch(IOException e){
e.printStackTrace();
}
}
public void start() {
if(current != null){
current.show();
return;
}
// new StateMachine("/theme");
Form hi = new Form("Native Maps Test");
hi.setLayout(new BorderLayout());
final MapContainer cnt = new MapContainer();
hi.addComponent(BorderLayout.CENTER, cnt);
hi.addCommand(new Command("Move Camera") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-33.867, 151.206));
}
});
hi.addCommand(new Command("Add Marker") {
public void actionPerformed(ActionEvent ev) {
System.out.println("Marker");
try {
cnt.setCameraPosition(new Coord(41.889, -87.622));
cnt.addMarker(EncodedImage.create("/maps-pin.png"), new Coord(41.889, -87.622), "Hi marker", "Optional long description", new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Dialog.show("Marker Clicked!", "You clicked the marker", "OK", null);
}
});
} catch(IOException err) {
// since the image is iin the jar this is unlikely
err.printStackTrace();
}
}
});
hi.addCommand(new Command("Add Path") {
public void actionPerformed(ActionEvent ev) {
cnt.setCameraPosition(new Coord(-18.142, 178.431));
cnt.addPath(new Coord(-33.866, 151.195), // Sydney
new Coord(-18.142, 178.431), // Fiji
new Coord(21.291, -157.821), // Hawaii
new Coord(37.423, -122.091) // Mountain View
);
}
});
hi.addCommand(new Command("Clear All") {
public void actionPerformed(ActionEvent ev) {
cnt.clearMapLayers();
}
});
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
}
public void destroy() {
}
}
// action for the button which I have created by using GUI builder
protected void onPage2_ButtonAction(Component c, ActionEvent event) {
// Here I want to redirect to the map page
}

how to display a image on click of a button using LWUIT

I am trying to write a application using LWUIT where I want a image to be displayed on click of a button.
I have the following code. But I get an exception if the button is clicked twice.
Please help me display the image without giving any exception.
final Form f = new Form("Static TAF");
Button TrackMe = new Button("TrackMe");
Image TrackMeicon = null;
TrackMeicon = Image.createImage("/hello/follow.jpeg");
final Label TrackMeLabel = new Label(TrackMeicon);
TrackMe.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
System.out.println("Removing the previous Images");
f.addComponent(TrackMeLabel);
}
});
Please Help
When you are clicking the button for the first time, image is added to the form. When you are clicking for the second time, that image already exists in the form. So, it will throw "Component already exists" exception.
Your action listener should be
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
System.out.println("Removing the previous Images");
f.removeComponent(TrackMeLabel);
f.addComponent(TrackMeLabel);
}
});
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
System.out.println("Removing the previous Images");
final Label TrackMeLabel = new Label(TrackMeicon);
f.removeAll();
f.addComponent(TrackMeLabel);
}
});
If you want yo add only one image you can use this:
....
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
if(!f.containes(TrackMeLabel))
f.addComponent(TrackMeLabel);
}
if you want some imges you need something like that:
....
TrackMe.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae) {
Image TrackMeicon = null;
TrackMeicon = Image.createImage("/hello/follow.jpeg");
Label TrackMeLabel = new Label(TrackMeicon);
f.addComponent(TrackMeLabel);
}

Resources