How to avoid automatic closing in Dialogfragment?

Asked

Viewed 561 times

1

I developed a login screen, as can be seen in the figure below, following the section "Creating a Custom Layout" of official documentation.

inserir a descrição da imagem aqui

The only problem I’m facing is that the dialog closes independent of the option I click. How to prevent automatic closing when I click the button ACCESS?

Dialog call:

public class MainActivity extends FragmentActivity {

    // ...

    public void abrirDialogLogin() {
        DialogFragment dialogLogin = new LoginDialog();
        dialogLogin.show(getSupportFragmentManager(), "login");
    }
}

Dialog building:

public class LoginDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        final LayoutInflater inflater = getActivity().getLayoutInflater();

        builder
            .setView(inflater.inflate(R.layout.login, null))
            .setPositiveButton(R.string.acessar, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //TODO bloquear fechamento automático AQUI
                }
            })
            .setNegativeButton(R.string.cancelar, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //TODO pode fechar automaticamente AQUI
                }
            });

        return builder.create();
    }
}
  • 1

    See if this reply helping.

2 answers

3

Put the following code in the method onCreateView() of your DialogFragment

getDialog().setCanceledOnTouchOutside(false);

1


To solve the reported problem I had to make small adjustments in the Logindialog class. The main change occurred in the method onCreateDialog() which now returns its value from super.onCreateDialog().

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

Another important change was the implementation of the method onCreateView(). In this I make the Binding of my buttons and return a view containing my layout.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.login, container, false);

    Button botaoCancelar = (Button) view.findViewById(R.id.botao_cancelar);
    Button botaoLogin = (Button) view.findViewById(R.id.botao_login);

    botaoCancelar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO realizando o cancelamento do dialog
        }
    });

    botaoLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //TODO realizar o login
        }
    });

    return view;
}

So far everything works well. Only that the layout appears with reduced dimensions. inserir a descrição da imagem aqui

To get around the problem the following code should be written in the method onStart():

@Override
public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }        
}

Ready! Everything working perfectly.

inserir a descrição da imagem aqui

Full code of the Logindialog class

public class LoginDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null) {
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        }        
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.login, container, false);

        Button botaoCancelar = (Button) view.findViewById(R.id.botao_cancelar);
        Button botaoLogin = (Button) view.findViewById(R.id.botao_login);

        botaoCancelar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO realizando o cancelamento do dialog
            }
        });

        botaoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO realizar o login
            }
        });

        return view;
    }
}

Login 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">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:layout_marginTop="12dp"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/fazer_login"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="@string/email"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginBottom="12dp"
        android:id="@+id/email"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/senha"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:layout_marginBottom="12dp"
        android:id="@+id/senha"/>

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingTop="40dp"
        android:layout_marginLeft="18dp"
        android:layout_marginRight="18dp"
        android:gravity="right">

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="@color/colorPrimary"
            android:text="@string/cancelar"
            android:id="@+id/botao_cancelar"/>
        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textColor="@color/colorPrimary"
            android:text="@string/acessar"
            android:id="@+id/botao_login"/>
    </LinearLayout>

</LinearLayout>

In the layout you can notice the declared height and width with the maximum value (match_parent), however these values are disregarded and need to be overwritten during the life cycle of DialogFragment, as can be seen in the method onStart() class LoginDialog.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.