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

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);

Related

Overlay toolbar with other toolbar when item is selected in RecyclerView which is inside a fragment

To illustrate what I mean with this, it is similar to WhatsApp, where various options are displayed in the toolbar when a chat is selected.
I have a similar layout, so a MainActivity with Fragments containing RecyclerViews. Now when an item in a RecyclerView is selected I would like to get a similar behaviour as in WhatsApp. The RecyclerViews have an Adapter that implements an OnClickListener.
However, from this Adapter I do not seem to have access to Views from the MainActivity. I tried the following (inside the OnClick method in the Adapter), but it did not work since the view could not be found.
View view = getActivity().findViewById(R.id.toolbar_main_activity);
if( view instanceof Toolbar) {
Toolbar toolbar = (Toolbar) view;
toolbar.setTitle("TestTitle");
}
Does anyone know how to get the intended behavior or have a reference to a tutorial?
UPDATE: for who is also stuck with this and this is still quite confusing, here is how I solved it in my own words
My Fragment contains the Interface by adding the following code to it;
OnItemsSelected mCallBack;
public interface OnItemsSelected {
void onToolbarOptions(String title);
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
mCallback = (OnItemsSelected) getActivity();
}
Also I passed 'mCallback' to the adapter like this;
MyAdapter adapter = new MyAdapter(myList, mCallback);
The RecyclerView adapter implements OnClickListener. In the OnClick method I called; 'mCallBack.onToolbarOptions("someTitle");'. And finally I made my MainActivity implement the method; 'implements myFragment.onItemsSelected' and I added the following code to it also;
#Override
public void onToolbarOptions(String title) {
toolbar.setTitle(title);
}
With this, only the title is changed, but from this it is quite easy to make other changes to the toolbar, such as changing the menu items.
Inside your Fragment you make an Interface and a global variable like this:
OnItemsSelected mCallBack;
public interface OnItemsSelected {
public void onToolbarOptions();
}
Then when in your RecyclerView items are selected or clicked you call:
mCallBack.onToolbarOptions();
In your Activity implement the Interface like this plus the method onToolbarOptions():
public static class YourActivityName extends AppCompatActivity
implements YourFragmentName.OnItemsSelected {
public void onToolbarOptions(){
// CHANGE YOUR TOOLBAR HERE
}
//.....OTHER STUFFS IN YOUR ACTIVITY
}

Keyboard is not shown up after came from android default contact page in my app

I have one button called "Invite Paricipants". When i tap on it, my app will goes to android default contact page. After selecting contact, i am calculating the emails in it. If i found no email then, showing edit text to enter email. There i am using following code
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
but keyboard is not showing up. How can i resolve my problem?
Yes he is right, after coming back the focus to EditText has been lost and by using requestFocus for EditText allows you to have focus and obviously pops up the Keyboard
You have to request input focus for editText.
To do this go to your interface definition xml and add:
<requestFocus />
Inside the definition for editText.

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.

Switching between LWUIT Form and LCDUI Form

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

Resources