0

I have a barcode reader that works likes a keyboard when reading a barcode sends the keys and ends with enter.

In my picking app I show an alertDialog to confirm the read of quantities after reading an article barcode but if I read another barcode whit the alertDialog is in display, my app continues catching the input keys... the numbers keys focus the accept button (I don't know why) and the enter accept and close the alertDialog.

Is possible to allow only finger touch to confirm an alertDialog?

Edit: this is my alertdialog code:

public void dialogCantidad(String cantidad, String producto){                                                         
   TextInputEditText focuss = findViewById(R.id.lectura);                                                            
   focuss.clearFocus();                                                                                              

   AlertDialog.Builder builder = new AlertDialog.Builder(activity_detalle.this);                                     
   DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {                     
       @Override                                                                                                     
       public void onClick(DialogInterface dialog, int which) {                                                      
           switch (which){                                                                                           
               case DialogInterface.BUTTON_POSITIVE:                                                                 
                       focuss.requestFocus();                                                                        
                break;                                                                                               

               case DialogInterface.BUTTON_NEGATIVE:                                                                 
                   //No button clicked                                                                               
                   break;                                                                                            
           }                                                                                                         
       }                                                                                                             
   };                                                                                                                

   builder.setMessage(cantidad+" unidades. "+producto).setPositiveButton("Lo tengo, loco", dialogClickListener);     
   builder.setCancelable(false);                                                                                     
   AlertDialog theDialog = builder.create();                                                                         
   theDialog.show();                                                                                                 
   TextView textView = theDialog.findViewById(android.R.id.message);                                                 
   textView.setTextSize(60);}
fe80grau
  • 11
  • 4
  • a dialog in android is asynchronous. The code keeps running in the background after the dialog appears. You need to set some kind of variable for when the dialog is open that, when it's true, the scanner is turned off. – John Lord Apr 07 '20 at 02:18
  • I undertand, but if i turn off bluetooth or device is a waste of time. The pairing time bluetooth is too long. Is possible to allow only touch actions and deny all external device inputs? – fe80grau Apr 07 '20 at 11:14
  • ok i understand. You are using a hardware scanner. It is possible to block keypresses. Your options are limited with the stock dialog, You will want to use a custom layout in the dialog and when you attach events to the buttons, only attach ontouchlistener events. This will ignore keyboard presses. The default ones use onclick with responds to keypresses and touch both. – John Lord Apr 07 '20 at 14:04
  • Thank you John. I think that it is perfect solution, but when in my builder i declare ."setPositiveButton" only accept a OnClickListener. I edited the main post with my code. – fe80grau Apr 07 '20 at 16:44

1 Answers1

1

Thanks to you John Lord from my girlfriend and me :), you have given me the idea to create a custom dialog with view.ontouchlistener implements.

This is the code to create a Dialog that only works with onTouch events (is based in How to create a Custom Dialog box in android?)

XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@color/colorPrimary">

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            app:srcCompat="@android:drawable/ic_dialog_alert" />

    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FDFDFD"
        android:orientation="vertical"
        android:padding="16dp">

        <TextView
            android:id="@+id/cantidad"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text=""
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textSize="50sp" />

        <TextView
            android:id="@+id/producto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text=""
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textSize="36sp" />

        <Button
            android:id="@+id/buttonOk"
            android:layout_width="291dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="15dp"
            android:text="Lo tengo, loco"
            android:textColor="@color/colorPrimary"
            android:textSize="30sp" />

    </LinearLayout>

</LinearLayout>

Java Class:

**Imports

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

Class

public class CustomDialogClass extends Dialog implements android.view.View.OnTouchListener {

public Activity c;
public Dialog d;
public Button yes, no;
public String cantidad, producto;

public CustomDialogClass(Activity a, String cantidad, String producto) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
    this.cantidad = cantidad;
    this.producto = producto;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    yes = (Button) findViewById(R.id.buttonOk);
    yes.setOnTouchListener(this);
    TextView txtcantidad = findViewById(R.id.cantidad);
    TextView txtproducto = findViewById(R.id.producto);

    txtcantidad.setText(this.cantidad+" unidades");
    txtproducto.setText(this.producto);

}

@Override
public boolean onTouch(View v, MotionEvent motionEvent) {
    switch (v.getId()) {
        case R.id.buttonOk:
            dismiss();
            break;
        default:
            break;
    }
    dismiss();

    return false;
}}

And my function to invoke

 public void dialogCantidad(String cantidad, String producto){
    TextInputEditText focuss = findViewById(R.id.lectura);
    focuss.clearFocus();

    CustomDialogClass cdd = new CustomDialogClass(activity_detalle.this, cantidad, producto);
    cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    cdd.setCancelable(false);
    cdd.show();

    Window window = cdd.getWindow();
    window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT);
}
fe80grau
  • 11
  • 4