Search code examples
javaandroidandroid-studiofingerprint

How to set visibility gone from another class to textview and imageview that belong to activity?


I am trying to set a visibility value to certainviews in my activity. The problem is that I don't see any changes.

In my Login activity I have the option for the user to Login with fingerprint. When the user has the option to login with fingerprint my activity hiddes the EditText and shows an imageview when the actyvity is created. What I want to do is in case the user fails the fingerprint login 3 times in a row the activity should hide an image view and show an Edittext so that he can login with password.

Inside my main activity I have this method:

public void ChangeViews(Context context){
    LinearLayout passwoordInsert;
    ImageView enterBtnMasked;
    ImageView imgFinger;
    TextView txtFinger;

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.activity_masked_login, null);
    passwoordInsert = vi.findViewById(R.id.password_insert_f);
    enterBtnMasked = vi.findViewById(R.id.enter_btn_masked_f);
    imgFinger=vi.findViewById(R.id.fingerprintImage_f);
    txtFinger=vi.findViewById(R.id.text_fnger_f);

    passwoordInsert.setVisibility(View.VISIBLE);
    enterBtnMasked.setVisibility(View.VISIBLE);
    txtFinger.setVisibility(View.GONE);
    imgFinger.setVisibility(View.GONE);

    Log.e("IsDataChanged", "Data is changed");


}

The method above is called inside my FingerPrintHandler.class after showing an alertDialog:

 @Override
public void onAuthenticationFailed() {

CancelAuth();
setAlert("Retry writting your password");
}

AlertDialog called when the user fails for 3 times in a row, this method is inside my fingerprinthandler class:

public void setAlert(String message) {
    alertDialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE);
    alertDialog.setTitleText("Error");
    alertDialog.setContentText(message);
    alertDialog.setConfirmText("Aceptar");
    alertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
        @Override
        public void onClick(SweetAlertDialog sweetAlertDialog) {

            alertDialog.dismissWithAnimation();

            MaskedLogin maskedLogin = new MaskedLogin();
            maskedLogin.ChangeViews(context);


        }
    });
    alertDialog.show();
}

I've been trying to achieve this for hours but no success yet. Any suggestion would be apreciated.


Solution

  • Make a callback to communicate between YourActivity and YourHandler class,

    example:

    In YourHandler, create an interface

    public interface ChangeViewListener {
        void onViewChanged();
    }
    
    private ChangeViewListener listener;
    
    
    public void setAlert(String message) {
        alertDialog = new SweetAlertDialog(context, SweetAlertDialog.WARNING_TYPE);
        alertDialog.setTitleText("Error");
        alertDialog.setContentText(message);
        alertDialog.setConfirmText("Aceptar");
        alertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
            @Override
            public void onClick(SweetAlertDialog sweetAlertDialog) {
    
                if(listener != null) {
                   listener.onViewChanged();
                }
    
    
            }
        });
        alertDialog.show();
    }
    
    public void setListener(ChangeViewListener listener) {
       this.listener = listener;
    }
    

    and finally, call setListener in yourActivity

    yourHandler.setlistener(YourActivity.this)
    

    of course, implement this listener in your activity

    public class YourActivity extends Activity implements YourHandler.ViewChangeListener {
    ...
    override void onViewChanged() {
      // call method ChangeViews here
    }
    ...
    }
    

    that is a right approach in your case! hope it helps