3

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>

Community
  • 1
  • 1
Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
  • @MrsEd how can you restore savedinstancestate of a button? i mean i find it difficult to implement the answer in your comment's link. – Edijae Crusar Dec 08 '15 at 08:03
  • @MrsEd i haven't found an answer yet bro. the one in your link is complicated and confusing to me. i can't find a good relationship with my button here. – Edijae Crusar Dec 08 '15 at 08:08
  • @MrsEd i have posted the xml for my custom dialog which has the button. and yes am getting the text from user input even when screen orientation is changed. only that the button is becoming unclickable – Edijae Crusar Dec 08 '15 at 08:16

2 Answers2

1

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.

Edijae Crusar
  • 3,473
  • 3
  • 37
  • 74
0

If you are adding click listeners in Dialog to your buttons, you need to add onclick for your new buttons objects create after onConfigurationChanged.

setting on click listener in xml is good practice for removing problem like this:

so set on click in xml like this :

<Button
android:id="@+id/addnote"
android:onClick="closeDialog"
/>

now in activity call onclick where you want

public void closeDialog(View view) {
dialogObject.dismiss();
} 

after using on click in xml no need to call setOnClickListener

santoXme
  • 802
  • 5
  • 20
  • what if my method has some parameters? and by the way this dialog is inmplemented in a fragment. am getting a warining in xml `Cannot resolve symbol xxx check if method specified in onclick xml attribute is declared in related activity` – Edijae Crusar Dec 08 '15 at 07:12
  • am getting an error! `could not find method showBal in the activity` this method is in a fragment not activity. is there any way i can use it in xml onClick attribute? – Edijae Crusar Dec 08 '15 at 07:49
  • sry but ..i cant understand what you are saying ..be little bit clear – santoXme Dec 08 '15 at 07:56
  • i don't think your answer will work for my case because You can't register the onClick callback(using android:onClick) for the Button(from the layout of the Dialog) in the Fragment class because Android will not find it as it will search only the **Activity** for a method matching that name(showBal) and it will throw that exception if it won't find the metnod – Edijae Crusar Dec 08 '15 at 08:00