Switching between LWUIT Form and LCDUI Form - java-me

I have built a LWUIT UI class which contains the Midlet. I am basically using a theme from this midlet. But I need to jump to another LCDUI form which contains some LCDUI controls and I need to set display that LCDUI form. So is it possible to jump from LWUIT form to LCDUI form and set display the LCDUI form ? If possible how ?

I used following code to show the both LWUIT Form and LCDUI Form. See the sample code.
com.sun.lwuit.Form lwuitForm;
protected void startApp() throws MIDletStateChangeException {
Display.init(this);
lwuitForm = new com.sun.lwuit.Form("LWUIT Form");
lwuitForm.addComponent(new TextField(""));
final MIDlet midlet = this;
final Command abtUsCmd = new Command("Next") {
public void actionPerformed(ActionEvent evt) {
javax.microedition.lcdui.Form frm = new javax.microedition.lcdui.Form("LCDUI Form");
StringItem item = new StringItem("Text", "Sample text");
frm.append(item);
final javax.microedition.lcdui.Command cmd = new javax.microedition.lcdui.Command("Back", javax.microedition.lcdui.Command.BACK, 0);
CommandListener cmdLis = new CommandListener() {
public void commandAction(javax.microedition.lcdui.Command c, Displayable d) {
if(c == cmd) {
Display.init(midlet);
lwuitForm.show(); // Show the LWUIT form again
}
}
};
frm.setCommandListener(cmdLis);
frm.addCommand(cmd);
javax.microedition.lcdui.Display.getDisplay(midlet).setCurrent(frm); // show the LCDUI Form
}
};
lwuitForm.addCommand(abtUsCmd);
lwuitForm.show(); // Show the LWUIT Form
}

This looks tricky, but yeah, we can switch between both.
The trick is when u show the LWUIT form, after it has been successfully painted on the screen, make a call to
javax.microedition.lcdui.Display.getDisplay(midlet).getCurrent();
this gives you the Displayable holding all the LWUIT views, so with this, you can always switch to LCDUI, and back to LWUIT with the LCDUI's
display.setCurrent
Let me know if this works for you.
Thanks

Related

can i put a dialog in a spinner item?

I'm new in the android world but I got experience in java.
Im trying to show a dialog when I select an especific item in my spinner but, when I do it, my application has stopped.
I have 1 fragment and 1 class for the listener, instantiate an object from the fragment in the listener and try to call the dialog.
The code is somthing like this:
//Instantiate of class Guardar extends SherlockFragment.
Guardar controlador = new Guardar();
public void onItemSelected(final AdapterView parent, View view, int pos,long id) {
String addSM = parent.getItemAtPosition(pos).toString();
if (addSM == “Anyadir”){
// custom dialog
final Dialog dialog = new Dialog(controlador.context);
dialog.setContentView(R.layout.dialog_afegirsuper);
dialog.setTitle(“Title…”);
// set the custom dialog components – text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText(“Android custom dialog example!”);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
Is this possible to implement? another similar idea?
thanks a lot for any solution

Lwuit touch screen strange behaviour

I am making an application using LWUIT.
There is a form
There is a list embedded on the form.
The list has 5 elements.
Initially, when I first load the app, if I choose the 1st element, 2nd gets chosen; when I choose the second the 3rd gets chose and and so on (Weird!)
I am not able to click any button on the screen either
next what I do is, shift to a different from using arrow keys (of the keyboard... I am running the app on a simulator btw)
Then I come back to the first form and now everything works as expected(no weird behaviour).
What could be the issue?
I am using Sun Java Micro Edition SDK 3.0 (default touch screen for testing)
My code is:
List dummy = new List();
dummy.addItem("wewerwer");
dummy.addItem("wewerdswer");
dummy.addItem("wewqweerwer");
dummy.addItem("dscxwewerwer");
dummy.addItem("jhgwewerwer");
mainListForm.setLayout(new BorderLayout());
mainListForm.addComponent(BorderLayout.CENTER,dummy);
mainListForm.show();
What could possible be going wrong here?
UPDATE 1
I think there is a bug here. I have attached the complete code below along with the screen shot
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class Demo extends MIDlet implements ActionListener {
private Form mForm;
List abc;
public void startApp() {
Display.init(this);
try {
Resources r = Resources.open("/Test.res");
UIManager.getInstance().setThemeProps(r.getTheme(
r.getThemeResourceNames()[0])
);
} catch (Exception e){
System.out.println(e.toString());
}
if (mForm == null) {
Button click = new Button("Press me!");
click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("I have been pressed");
}
});
abc = new List();
abc.addItem("Str1");
abc.addItem("Str2");
abc.addItem("Str3");
abc.addItem("Str4");
abc.addItem("Str5");
abc.addItem("Str6");
Form f = new Form("Hello, LWUIT!");
abc.addActionListener(this);
f.addComponent(abc);
Command exitCommand = new Command("Exit");
f.addCommand(exitCommand);
f.addCommandListener(this);
f.addComponent(click);
f.show();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void actionPerformed(ActionEvent ae) {
System.out.println(abc.getSelectedIndex());
}
}
So now when I click on 'Str1' of the list Str2 gets selected and so on.
IDE: Netbeans
Emulator: Default Touch screen phone
On the action event set the list to active again after the event by invoking setHandlesInput(true)
OK....so this is how you resolve it.
After the form is displayed remove the list from the form and again add it to the form and then repaint the form.
Earlier Code
1) form.addComponenet(BorderLayout.center,list);
2) form.show();
Word Around for the problem
1)form.addComponenet(BorderLayout.center,list);
2)form.show();
3)form.setScrollable(false);
I know its kind of strange, but this way the list index selection works smooth for touch screen phones.

How to remove the Title-bar of a LWUIT Form?

In the actionPerformed of a Button I want to remove the Title-bar of the actual LWUIT Form. How to achieve that? And how to redisplay it again after a certain action has been complete?
Use below code for hide/show the title of the Form in the Button action event,
final Form form = new Form("Sample");
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
final Container titleContainer = form.getTitleArea();
titleContainer.setVisible(false);
Button b = new Button("button");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!titleContainer.isVisible()) {
titleContainer.setVisible(true);
} else {
titleContainer.setVisible(false);
}
form.revalidate();
}
});
form.addComponent(b);
form.show();
You could also just do
form.getTitleArea().setVisible(false);

How to open the VirtualKeyboard associated to a TextField when the field is edited?

I initialized the VKBImplementationFactory in startApp() :
public void startApp() {
VKBImplementationFactory.init();
Display.init(this);
new MenuPrincipalForm(this).show();
}
I created also a VirtualKeyboard in a Form :
...
private VirtualKeyboard vkNombre = new VirtualKeyboard();
...
vkNombre.setInputModeOrder(new String[]{VirtualKeyboard.NUMBERS_SYMBOLS_MODE});
And I bound this VirtualKeyboard to a TextField :
cintxt=new TextField();
VirtualKeyboard.bindVirtualKeyboard(cintxt, vkNombre);
I registered dataChangeListener to this TextField :
public class ModifierFicheClient extends Form implements ActionListener, DataChangedListener
{
...
cintxt.addDataChangeListener(this);
...
}
In the dataChanged(int type, int index) method I want to open the vkNombre VirtualKeyBoard. I know that when clicking the TextField then the VirtualKeyboard is shown automatically. But there is a case when navigating to the TextField through the phone mobile scroll softbuttons then I can navigate to the TextField without clicking it and I can type any letters ! So how to call the VirtualKeyboard when typing a letter on the phone mobile ?
NB : I wrote System.out.println("zzzz"); in the dataChanged(int type, int index) method and the output writes two lines "zzzz" when I type one character ! So why the dataChanged method is called two times when I type only one letter ?
No need to use VKBImplementationFactory.init(); in startApp(). Because LWUIT automatically detect whether that mobile is touch screen or not. And numeric constraint not working on VKB when you use LWUIT 1.5 or before versions. It is bug on that versions. But it will be fixed on current repository version of LWUIT (Revision: 1605). So you can checkout from repository and use the latest LWUIT jar.
Update:
See the sample code for showing VKB while focusing on TextField,
TextField textField = new TextField();
final VirtualKeyboard keyboard = new VirtualKeyboard();
textField.addFocusListener(new FocusListener() {
public void focusGained(Component cmp)
keyboard.show();
}
public void focusLost(Component cmp) {
keyboard.dispose();
}
});
keyboard.setTextField(textField);

Extend View and Custom Dialogs

I am working on a game where I am extending the view and performing operations in the class. I need to have a popup in the game which will have 3 buttons inside it. I have managed to have the popup show-up using custom dialogs but when I configure the onClick as follows:
private void popUp() {
Context mContext = getContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_fullimage_dialog);
dialog.setTitle("Cheese Market");
Button one = (Button)findViewById(R.id.firstpack);
one.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cheeseLeft = cheeseLeft + 10;
masterMoveLock = false;
return;
}
});
}
It force closes giving a nullpointerexeption even though it is defined in the custom_fullimage_dialog layout.
Can someone help me figure out how to have the button click detected in this scenario?
Thank you.
Try calling dialog.findViewById instead.
You're setting the contentView for the dialog, but by calling findViewById you're looking for it under your activity's content view.

Resources