Search code examples
androidandroid-dialogfragmentandroid-dialogandroid-textwatcher

Unable to maintain the state of a custom dialog button when screen orientation changes


I have a customized dialog which i have created using DialogFragment. it uses a view which got one button and an edittext. in my edittext, i have implemented a textwatcher where if there are values in the edittext, the button becomes visible else it remains invisible.

public  class ShowDialog extends DialogFragment {

private Button btnShowBalance;
private EditText balanceInquiryPinInput;

public ShowDialog(){

}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View myView = inflater.inflate(R.layout.fragment_balance_inquiry,null);

    btnShowBalance= (Button) myView.findViewById(R.id.btnShowBalance);

    balanceInquiryPinInput = (EditText) myView.findViewById(R.id.et_balance_inquiry_pin);

    balanceInquiryPinInput.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if(s.length() > 0){
                btnShowBalance.setVisibility(View.VISIBLE);
            }else{
                btnShowBalance.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

     btnShowBalance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showBalance(balanceInquiryPinInput,nameview,ShowDialog.this);
        }
    });


    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setView(myView);

    // Create the AlertDialog object and return it
    return builder.create();
}

}

Problem

When i change my screen orientation to (let say) landscape, if i had typed anything to my edittext before changing my orientation, whatever i had typed is still visible in landscape, my button is visible But it becomes unclickable. i cannot click it. i have to remove the dialog by pressing somewhere outside the dialog window or back button and create it again.

How can i make my button remain clickable even when one change the screen orientation?

Note: it is clickable before changing orientation.

EDIT

My dialog is activated by a button which is in a fragment and not activity. this question is not a duplicate of This one because the latter is an implentation on an activity and it's implementation is unreliable to my view and it's state in question(button)

EDIT xml for my custom dialog layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#044848"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/check_balance"
    >

    <EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_balance_inquiry_pin"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:background="@drawable/enter_pin_textview"
        android:inputType="numberPassword"/>

    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="OK"
        android:id="@+id/btnShowBalance"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:background="@drawable/show_balance_ok_button"
        android:visibility="invisible" />

</LinearLayout>


Solution

  • The problem of the button being disabled may be due to the dialog no longer being functional due to the calling view being destroyed in the orientation change. So it may still be visible, but is really an artifact of the previous view before it was destroyed.

    You need to use methods that saves the state of your dialog fragment instance when it is created. for example you can call

    setRetainInstanceState(true);
    

    in your onCreateDialog method to retain the state of your DialogFragment instance when screen orientation changes.

    You may need to overide onDestroyView() to prevent dialog from been destroyed when screen orientation changes. like this

    @Override
    public void onDestroyView() {
        if (getDialog() != null && getRetainInstance())
            getDialog().setDismissMessage(null);
        super.onDestroyView();
    }
    

    Information about this and other fragment methods can been found here.