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

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

Related

How to Scroll pdfView automatically with button click or volume buttons

I'm using barteksc pdf viewer library to load pdf in my application.
pdfView = findViewById(R.id.pdfView);
pdfView.fromAsset(getResources().getString(R.string.pdfname))
.enableDoubletap(true)
.enableSwipe(true)
.defaultPage(pageNumber)
.onPageChange(mainreading.this)
.pageFitPolicy(FitPolicy.WIDTH)
.pageFling(true)
.linkHandler(null)
.enableAnnotationRendering(true)
.swipeHorizontal(true)
.scrollHandle(new DefaultScrollHandlenew(mainreading.this))
.enableAntialiasing(true)
.load();
}
I want pdf to start scroll automatically when user click the button of volume up and down buttons to start stop. I tried with below code while wrapping it in the handler with handler.performClick(); but it shows blank screen while scrolling up and down.
scrollbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pdfView.scrollTo(0, pdfView.getScrollY() + 24);
}
});
Example :
https://play.google.com/store/apps/details?id=com.emptysheet.pdfreader_autoscroll&hl=en&gl=US
I want to make as like this. Can anyone help please.
Also tried with this. But it shows blank page after some scrolls.
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (action == KeyEvent.ACTION_DOWN) {
pdfView.scrollTo(0, pdfView.getScrollY() -24);
}
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (action == KeyEvent.ACTION_DOWN) {
pdfView.scrollTo(0, pdfView.getScrollY() + 24);
}
return true;
default:
return super.dispatchKeyEvent(event);
}
}
You can simply use this PDF viewer from github.
It's based on the same 'barsteksc' pdf viewer with the feature to jump to any pages.
It's MagicalPdfViewer and you can use 'jumpTo(pageNo)' method to simply jump to the specific page. It also gives you the option to animate to the specific page with the same method, just pass 'true' as the 2nd parameter.
Moreover, if you pass the values like '-1' and 'bigger than pageNo', It will automatically scroll to the 0 & last page respectively.
Give it a try & let me know if you got what you wanted.

Is there a way of displaying 2 input for QR code in android

So I’m creating an android app for queuing and it uses a QR code for reference and when scanned should display date and time of the chosen schedule but my code only displays one value when scanned
This is my code
String name;
setContentView(R.layout.deposit_gen);
dl_dep = findViewById(R.id.deposit_download);
qr_dep = findViewById(R.id.qr_deposit);
try {
bitmap = textToImageEncode(select_date.getText().toString().trim());
qr_dep.setImageBitmap(bitmap);
dl_dep.setVisibility(View.VISIBLE);
dl_dep.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "code_scanner",
null);
Toast.makeText(Deposit.this, "Saved to Gallery", Toast.LENGTH_LONG).show();
}
});
select_date is the variable for chosen dates and time is time

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)

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