Place an Edittext in Dialogfragment

Asked

Viewed 222 times

0

Need to put a Edittext to fill the name... ie when the user click the button will appear a Alertdialog, which should put the name of the player, I did up to a step, here in Sopt has a question of this type only that can not solve.

Could someone help me?

Code:

package com.gif.popupsair;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class MyCaixa extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());

        builderr.setMessage("Nome do jogador")
                .setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builderr.setTitle("Golll!");
        AlertDialog dialog = builderr.create();

        return dialog;
    }
}

Thank you.

2 answers

1

As a suggestion, you could use a Dialog and set Edittext in XML.

 public class MyCaixa extends Dialog {

    public MyCaixa(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.your_layout);
     }
}

And to call the Dialog just enough:

new MyCaixa(context).show();

1


Nathan,

I’ll give you a slightly better suggestion. What was done in the above @Eduarda reply can be done without necessarily inheriting Dialog. The AlertDialog has a method called setView(), where you can enter a custom view by you.

We will do this within the method itself onCreateDialog thus:

View view = LayoutInflater.from(getContext()).inflate(R.layout.seulayoutcustomizado, null);
builderr.setView(view);

Follow the full code:

package com.gif.popupsair;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;

public class MyCaixa extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderr = new AlertDialog.Builder(getActivity());

        builderr.setMessage("Nome do jogador")
                .setPositiveButton("Salvar", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        builderr.setTitle("Golll!");

        View view = LayoutInflater.from(getContext()).inflate(R.layout.seulayoutcustomizado, null);
        builderr.setView(view);

        AlertDialog dialog = builderr.create();

        return dialog;
    }
}

Not advisable to use Dialog out of a DialogFragment, how @Eduarda’s response does. That’s because if your Dialog is displayed through a DialogFragment, it will be better connected to the life cycle of a Activity. Try in debug mode to display a Dialog without a DialogFragment and rotate the screen. You will see in the logs an error like this :

Activity x has leaked window com.android.... Decorview

That’s because there was a leak because you didn’t close the Dialog when the Activity was destroyed during the rotation. What I mean is that if you display a Dialog on its own, it has to tie it to the life cycle of Activity so that no leakage occurs. When you use the DialogFragment, this kind of concern is not necessary as it is automatic.

  • Thanks for the tip...

Browser other questions tagged

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