How to avoid automatic closing in DialogFragment? [duplicate]

1

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

TheonlyproblemIamfacingisthatdialogclosesregardlessoftheoptionIclickon.HowdoIpreventautocompletionfromoccurringwhenIclicktheACCESSbutton?

Dialogcall:

publicclassMainActivityextendsFragmentActivity{//...publicvoidabrirDialogLogin(){DialogFragmentdialogLogin=newLoginDialog();dialogLogin.show(getSupportFragmentManager(),"login");
    }
}

Building the dialog:

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();
    }
}
    
asked by anonymous 07.09.2015 / 22:39

2 answers

1

To solve the reported problem I had to make small adjustments to the LoginDialog class. The main change occurred in the onCreateDialog() method, 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 onCreateView() method. In this I do 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 fine. But the layout appears small.

Toworkaroundtheproblem,thefollowingcodemustbewrittenintheonStart()method:

@OverridepublicvoidonStart(){super.onStart();Dialogdialog=getDialog();if(dialog!=null){dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);}}

Ready!Everythingworkingperfectly.

LoginDialogclasscompletecode

publicclassLoginDialogextendsDialogFragment{@OverridepublicDialogonCreateDialog(BundlesavedInstanceState){Dialogdialog=super.onCreateDialog(savedInstanceState);dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);returndialog;}@OverridepublicvoidonStart(){super.onStart();Dialogdialog=getDialog();if(dialog!=null){dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);}}@Nullable@OverridepublicViewonCreateView(LayoutInflaterinflater,ViewGroupcontainer,BundlesavedInstanceState){Viewview=inflater.inflate(R.layout.login,container,false);ButtonbotaoCancelar=(Button)view.findViewById(R.id.botao_cancelar);ButtonbotaoLogin=(Button)view.findViewById(R.id.botao_login);botaoCancelar.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOrealizandoocancelamentododialog}});botaoLogin.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOrealizarologin}});returnview;}}

LoginLayout

<?xmlversion="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 height and width declared with the maximum value ( match_parent ), however these values are neglected and need to be overwritten during the DialogFragment lifecycle, as can be seen in the onStart() of class LoginDialog .

    
08.09.2015 / 05:58
3

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

getDialog().setCanceledOnTouchOutside(false);
    
08.09.2015 / 05:13