Soft keyboard not closing when dialog is cancelled - android-studio

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)

Related

Toast Message will not display if placed at the beggining of the try catch

I am having an issue with displaying a toast message. I want it to display when a button is initially clicked, so that the user is aware the app is working( in my case, generating a QR code). If I place the toast at the beginning of the try catch it does not display right away when the button is clicked. It displays the toast only after the QR code is generated and displayed. Furthermore, the toast is displayed(after QR code is displayed) on my Android emulator but not at all on my physical android device(I have allowed app permissions) if I place it at the beginning of the try catch. It will only display on my physical device if the toast is at the end of the try catch. This has me puzzled. I attempted to use a ProgressBar that would run on the button click and terminate after the QR code was displayed but found this difficult to implement. I thought the toast would be an easy alternative as I have used them through out my project without any issues until now. If anyone can help me understand how I can get the toast to display right away when the button is clicked instead of displaying after the QR code is generated and displayed, it would much appreciated. Alternatively, if someone could assist me with including a progress bar(circle) to run once the button is clicked and stop once the QR code is displayed that would also be very much appreciated, as I believe this would be the most ideal solution.
Please see code below:
public class Generate extends AppCompatActivity {
public final static int QRcodeWidth = 500 ;
private static final String IMAGE_DIRECTORY = "/QR_Code_Generated";
Bitmap bitmap;
private EditText etQr;
private ImageView ivGenerated;
private Button btn;
private Button clrBtn;
private Button shareBtn;
private ProgressBar progressBar;
private int progressStatus = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_generate);
ivGenerated = (ImageView) findViewById(R.id.iv_generated);
etQr = (EditText) findViewById(R.id.generate_input);
btn = (Button) findViewById(R.id.button);
clrBtn = (Button) findViewById(R.id.clearButton);
shareBtn = (Button) findViewById(R.id.shareButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (etQr.getText().toString().trim().length() == 0) {
} else {
try {
Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
toast1.show();
bitmap = TextToImageEncode(etQr.getText().toString());
ivGenerated.setImageBitmap(bitmap);
String path = saveImage(bitmap); //give read write permission
// Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
} catch (WriterException e) {
e.printStackTrace();
}
}
}
});
I dont find any issue with your code for toast pop up, you can try adding a delay of 250ms after toast.show(), maybe it . would be a work around for you.
And for progress bar, its very simple. Create a progress bar in your .xlm file n place it at the center of your View.Layout you willing to show it & make its visibility as FALSE.
You can try this,
progressbar = = (ProgressBar) findViewById(R.id.progressbar);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
progressbar.setVisibility(VISIBLE); // or try View.VISIBLE
if (etQr.getText().toString().trim().length() == 0) {
} else {
try {
Toast toast1 = Toast.makeText(Generate.this, "Please Wait, Your QR Code is Generating & will be Saved to your Device... ", Toast.LENGTH_SHORT);
toast1.show();
bitmap = TextToImageEncode(etQr.getText().toString());
ivGenerated.setImageBitmap(bitmap);
String path = saveImage(bitmap); //give read write permission
// Toast.makeText(Generate.this, "QR Code saved to -> " + path, Toast.LENGTH_SHORT).show(); (if I include this second toast it does not display the first toast1)
} catch (WriterException e) {
e.printStackTrace();
}
finally {
progressbar.setVisibility(INVISIBLE); // or try View.INVISIBLE
}
}
}
});

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.

QDialogButtonBox button Ok not woking after connecting

I have a mainWindow class that calls a function mainWIndow::ShowDialogBox() when double clicked on the QTabBar. The dialog box shows up, but it isn't connecting the buttons. I have the connect calls in ShowDialogBox. It gives me a red underline on connect saying
no instance of overloaded function "MainWindow::connect" matches the argument list"
This is my code
bool MainWindow::eventFilter(QObject *object, QEvent *event)
{
if (object == mTabWidget->getTabBar() && event->type() == QEvent::MouseButtonDblClick)
{
qDebug()<<"dblclk";
ShowDialogBox();
}
return QObject::eventFilter(object, event);
}
//Show dialog box when double clicked on QTabBar
void MainWindow::ShowDialogBox(){
QDialog dialog;
QVBoxLayout layout(&dialog);
QLineEdit editLine;
layout.addWidget(&editLine);
QDialogButtonBox *dialogButton = new QDialogButtonBox(QDialogButtonBox::Ok );
connect(dialogButton, SIGNAL(accepted()), dialog, SLOT(accept())); //this 'connect' is underlined
layout.addWidget(dialogButton);
dialog.setLayout(&layout);
if(dialog.exec() == QDialog::Accepted)
{
mTabWidget->setTabText(0, editLine.text());
}
}
I have added the signals and slot in mainWindow.h as
private slots:
void accept();
signals:
void accepted();
I have spend hours on this but no luck. I am new to Qt.
Line:
connect(dialogButton, SIGNAL(accepted()), dialog, SLOT(accept()));
should be:
connect(dialogButton, SIGNAL(accepted()), &dialog, SLOT(accept()));
Because the third parameter has to be a memory address(pointer).

Android - Back button behavior

I have a project with 2 activities, the first one is the "SplashActivity" - where I load some network data - the second one, the MainActivity.
Inside of my MainActivity I have a fragment and inside of this fragment a webview. My first point is, when the user clicks on back button, the SplashScreen is open again.
The back button should behave like:
When the user doesn't navigate inside of my webview, close the app.
When the user navigates in webview, use the back history of the browswer.
I read about back stack here: http://developer.android.com/training/implementing-navigation/temporal.html#back-webviews
I didn't understand at all how it should work, because I have all cases "mixed". Anyone knows what should I do to fix this problem?
Any idea or sample code will be appreciate!
Define Webview wb as a global variable. Then try this;
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(wb.canGoBack() == true){
wb.goBack();
}else{
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Application will be closed")
.setMessage("Close app?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
System.exit(0);
}
}).setNegativeButton("No", null).show();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}

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

Resources