can i put a dialog in a spinner item? - dialog

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

Related

Soft keyboard not closing when dialog is cancelled

In my app i have a dialog where the user should enter some text in an edittext. But when I tap outside of the dialog to close the dialog, the dialog closes but the soft keyboard which popped up because i clicked on the edittext stays. It's very weird: when I set windowsoftinputmode to stateAlwaysHidden,the keyboard gets a bit transparent but it doesn't close. I only have this problem in portrait, In landscape it doesn't happen but that could be because the softkeyboard fills the whole screen. I also cant click on the keys of the keyboard it doesn't react. I already tried to set the windowsoftinputmode to different values and I set a oncancellistener on my dialo g which should close the softkeyboard but it doesn't. It's seems to me as a bug.
The code of my dialog
public void create(View view) {
final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_SWIPE_TO_DISMISS);
dialog.setContentView(R.layout.dialoglayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
dialog.setOnCancelListener(new DialogInterface.OnCancelListener(){
#Override
public void onCancel(DialogInterface dialog) {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
View view = getCurrentFocus();
if (view == null) {
view = new View(getBaseContext());
}
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
editText = dialog.findViewById(R.id.levelname);
editText.setOnEditorActionListener(new EditText.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
name = editText.getText().toString();
callables.totaloverwriteFile(name,getApplicationContext(),"newlevelname");
getApplicationContext().startActivity(new Intent(getApplicationContext(), CreatorActivity.class));
return true;
}
return true;
}
});
}```
Try building InputMethodManager from context, something like this (kotlin)
val inputMethodManager = context?.getSystemService<InputMethodManager>()
inputMethodManager?.hideSoftInputFromWindow(view.windowToken,0)

When click action bar icon i want display customized dialog box.(i.e)xml our dialog

My click action bar icon I want display dialog box.
(i.e)i am using custom dialog .
for dialog box i create xml layout.
The program i tried given below...
Actually i want that when click icon in action it display the custom dialog box (custom ).
switch (item.getItemId()) {
case R.id.icon:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dailog);
dialog.setTitle("Custom Dialog Example");
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok);
// Click cancel to dismiss android custom dialog box
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
return true;
}
When run app there no output
You have to call dialog.show() to display the dialog;
Also you can't use a return in a switch you need to use break.
Edit:
switch (item.getItemId()) {
case R.id.icon:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dailog);
dialog.setTitle("Custom Dialog Example");
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel);
Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok);
// Click cancel to dismiss android custom dialog box
dialogButtonCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
break;
}
Edit 2: Alternatively you could use the library Material Dialogs, it's really easy to use.

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

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

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