Problem when calling the Keyboard from an AlertDialog that is called from the fragment

2

Well, basically, I have the following problem. I have a ReciclerView that contains several EditText and will update it as the text changes and when it is required, it will return the list of items.

This in turn is done from an AlertDialog, since I need it to be called from a pop-up window and not from another fragment

The first part is simple, but my problem is that the keyboard is not called when it focuses on the EditText, Besides hint of this is not shown either, I have done tests and I have concluded that the detail is the use of AlertDialog

Adapter Code

public class AdRestos extends RecyclerView.Adapter<AdRestos.HolderPerdidas> {
    private List<Restos> mitems;
    private Runas1 runas1;
    private TextView estimatedConsequences;

    public AdRestos(List<Restos> mitems, Runas1 runas1, final TextView estimatedConsequences) {
        this.mitems = mitems;
        this.runas1 = runas1;
        this.estimatedConsequences = estimatedConsequences;
    }

    @NonNull
    @Override
    public AdRestos.HolderPerdidas onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        LayoutInflater layoutInflater=LayoutInflater.from(viewGroup.getContext());
        View view = layoutInflater.inflate(R.layout.item_caracteristicas_afectadas,viewGroup,false);
        return new HolderPerdidas(view);
    }


    @Override
    public void onBindViewHolder(@NonNull final AdRestos.HolderPerdidas holder, final int position) {

        holder.txtNombre.setText(mitems.get(position).getNombre());

        //Coloco el valor antes de crear agregar lo escuchas
        if(mitems.get(position).getCantidad() <0 || mitems.get(position).getCantidad()>0){
           holder.edtValue.setText(mitems.get(position).getCantidad()+"");
        }else{
            holder.edtValue.setText("");
        }

        holder.edtValue.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) {
                try{
                    mitems.get(position).setCantidad(Integer.parseInt(s.toString()));
                }catch (Exception e){
                    mitems.get(position).setCantidad(0);
                }

                Log.e("Posición",position+"");
                Log.e("Valor Campo",holder.edtValue.getText().toString());
                Log.e("Valor Lista",mitems.get(position).getCantidad()+"");

                try{
                    Log.e("Valor Hint",holder.edtValue.getHint().toString());
                }catch (Exception e){
                    Log.e("Error HINT",e.getMessage());
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //Prueba para ver si tiene focus

        holder.edtValue.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                    Log.e("FOCUS...","SI");
                }else{
                    Log.e("FOCUS...","NO");
                }

            }
        });


    }

    @Override
    public int getItemCount() {
        if(mitems !=null)
            return mitems.size();
        return 0;
    }

    public List<Restos> getAll(){
        return mitems;
    }


    public class HolderPerdidas extends RecyclerView.ViewHolder {
        public TextView txtNombre;
        public EditText edtValue;

        public HolderPerdidas(@NonNull View itemView) {
            super(itemView);
            txtNombre= (TextView) itemView.findViewById(R.id.txt_caracteristica);
            edtValue= (EditText) itemView.findViewById(R.id.edt_value);
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/txt_caracteristica"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="16dp"
        android:textAlignment="textEnd"
        android:textSize="14sp"
        app:layout_constraintBottom_toBottomOf="@+id/edt_value"
        app:layout_constraintEnd_toStartOf="@+id/edt_value"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/edt_value"
        tools:text="Fuerza" />

    <EditText
        android:id="@+id/edt_value"
        android:layout_width="45dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="4dp"
        android:hint="0"
        tools:text="20"
        android:inputType="number|numberSigned"
        android:textAlignment="center"
        android:textColorHint="#FF0000"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

</android.support.constraint.ConstraintLayout>

AlertDialog call

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

                LayoutInflater inflater = getActivity().getLayoutInflater();

                View dialogView= inflater.inflate(R.layout.dialog_restos, null);



                builder.setTitle(resources.getString(R.string.rest_title));
                builder.setMessage(resources.getString(R.string.rest_message));

                Switch allowRest;
                RecyclerView rvRest;
                TextView estimatedConsequences;

                allowRest = (Switch) dialogView.findViewById(R.id.rest_affected);
                estimatedConsequences = (TextView) dialogView.findViewById(R.id.txt_estimated);
                rvRest = (RecyclerView) dialogView.findViewById(R.id.rv_affected_data);
                rvRest.setLayoutManager(new LinearLayoutManager(context));

                AdRestos adRestos = new AdRestos(restosList,runas1,estimatedConsequences);
                rvRest.setAdapter(adRestos);
                rvRest.setItemViewCacheSize(100);

                builder.setView(dialogView);

                AlertDialog dialog = builder.create();
                dialog.show;

LOGCAT

2018-11-11 02:18:22.055 22629-22629/xx.xxxxxxxx.xxxxx E/FOCUS...: SI
2018-11-11 02:18:24.601 1387-1617/? E/audio_hw_generic: pcm_write failed cannot write stream data: I/O error
2018-11-11 02:18:34.609 1387-1617/? E/audio_hw_generic: pcm_write failed cannot write stream data: I/O error
2018-11-11 02:18:52.014 22629-22629/xx.xxxxxxxx.xxxxx E/Posición: 0
2018-11-11 02:18:52.014 22629-22629/xx.xxxxxxxx.xxxxx E/Valor Campo: 2
2018-11-11 02:18:52.014 22629-22629/xx.xxxxxxxx.xxxxx E/Valor Lista: 2
2018-11-11 02:18:52.014 22629-22629/xx.xxxxxxxx.xxxxx E/Error HINT: Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
2018-11-11 02:19:35.177 1776-1803/? E/memtrack: Couldn't load memtrack module

Picture of the dialogue

The funny thing is that the hint and the aliniation is omitted in the dialog

View image

I use a very similar code but much more loaded and it works perfectly but of course I execute it from a fragment and not in a dialogue

I would be very grateful if you could help me see my error and how to solve it

    
asked by Darkeniel 11.11.2018 в 06:51
source

1 answer

-1

Try changing the AlertDialog by DialogFragment

Example:

// Instantiate the fragment

FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new MyDialogFragment();
newFragment.show(ft, "dialog");

// Fragment code

public static class MyDialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater i, ViewGroup c, Bundle b) {
        View v = inflater.inflate(R.layout.my_dialog, container, false);
        return v;
    }
}
    
answered by 21.11.2018 в 14:19