1

Hi I am using recycler view in alterbox with an edit text.
In edittext keyboard not showing.
And try this solution:

In the mainfest XML I am add this

android:windowSoftInputMode="stateHidden|adjustPan"

In adapter Class:

editText.requestFocusFromTouch();

In recyclerview i tried this line:

android:descendantFocusability="beforeDescendants"

In above i tried it not working:

My code:

Alter Box:

      final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view2 = inflater.inflate(R.layout.alter_box, null);
    alertDialogBuilder.setView(view2);
    String[] subjects =
            {
                    "ANDROID",
                    "PHP",
                    "BLOGGER",
                    "WORDPRESS",
                    "JOOMLA",
                    "ASP.NET",
                    "JAVA",
                    "C++",
                    "MATHS",
                    "HINDI",
                    "ENGLISH"};
    relativeLayout = (RelativeLayout) view2.findViewById(R.id.relativelayout1);
    recyclerView = (RecyclerView)view2. findViewById(R.id.recyclerview1);
    recylerViewLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(recylerViewLayoutManager);
    recyclerViewAdapter = new RecyclerViewAdapter(context, subjects);
    recyclerView.setAdapter(recyclerViewAdapter);



    final AlertDialog dialog2 = alertDialogBuilder.create();
    alertDialogBuilder.setCancelable(true);
    dialog2.show();

RecyclerViewAdapter:

                  public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{

String[] SubjectValues;
Context context;
View view1;
ViewHolder viewHolder1;
TextView textView;

public RecyclerViewAdapter(Context context1,String[] SubjectValues1){

    SubjectValues = SubjectValues1;
    context = context1;
}

public static class ViewHolder extends RecyclerView.ViewHolder{
    public EditText textView;
    public ViewHolder(View v){
        super(v);
        textView = (EditText)v.findViewById(R.id.subject_textview);

    }
}

@Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){

    view1 = LayoutInflater.from(context).inflate(R.layout.recyclerview_items,parent,false);

    viewHolder1 = new ViewHolder(view1);

    return viewHolder1;
}

@Override
public void onBindViewHolder(ViewHolder holder, int position){

    holder.textView.setText(SubjectValues[position]);
    holder. textView.requestFocusFromTouch();
}

@Override
public int getItemCount(){

    return SubjectValues.length;
}
}

// Passing dailog on recycleview in alterbox not working for me.

ALTERDIALOG final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view2 = inflater.inflate(R.layout.alter_box, null);
    AlertDialog dialog = alertDialogBuilder.create();
    alertDialogBuilder.setView(view2);


    String[] subjects =
            {
                    "ANDROID",
                    "PHP",
                    "BLOGGER",
                    "WORDPRESS",
                    "JOOMLA",
                    "ASP.NET",
                    "JAVA",
                    "C++",
                    "MATHS",
                    "HINDI",
                    "ENGLISH"};
    relativeLayout = (RelativeLayout) view2.findViewById(R.id.relativelayout1);
    recyclerView = (RecyclerView)view2. findViewById(R.id.recyclerview1);
    recylerViewLayoutManager = new LinearLayoutManager(MainActivity.this);
    recyclerView.setLayoutManager(recylerViewLayoutManager);
    recyclerViewAdapter = new RecyclerViewAdapter(context, subjects,dialog);
    recyclerView.setAdapter(recyclerViewAdapter);
    final AlertDialog dialog2 = alertDialogBuilder.create();
    alertDialogBuilder.setCancelable(true);
    dialog2.show();

cONSTROUCTOR

TextView textView; Dialog dialogs;

public RecyclerViewAdapter(Context context1, String[] SubjectValues1, Dialog dialog){

    SubjectValues = SubjectValues1;
    context = context1;
    dialogs=dialog;

} OnBindViewHolder

  holder.textView.setText(SubjectValues[position]);


    holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                dialogs.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                dialogs.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });
}
jaya oriya
  • 83
  • 1
  • 17

2 Answers2

4

In onBindViewHolder adding this code worked for me, refered from this

pass dialog to RecyclerViewAdapter

alertDialogBuilder.setView(view2);
AlertDialog dialog = alertDialogBuilder.create();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new RecyclerViewAdapter(this, subjects, dialog));

used the dialog to open the keyboard.

holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

Edit solution code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view2 = inflater.inflate(R.layout.alter_box, null);
        String[] subjects = {"ANDROID", "PHP", "BLOGGER", "WORDPRESS", "JOOMLA", "ASP.NET"};
        RecyclerView recyclerView = view2.findViewById(R.id.rv);
        alertDialogBuilder.setView(view2);
        AlertDialog dialog = alertDialogBuilder.create();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecyclerViewAdapter(this, subjects, dialog));

        alertDialogBuilder.setCancelable(false);
        dialog.show();
    }

    public static class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

        private String[] subjects;
        private Context context;
        private AlertDialog dialog;

        RecyclerViewAdapter(Context context, String[] subjects, AlertDialog dialog) {

            this.subjects = subjects;
            this.context = context;
            this.dialog = dialog;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            TextView textView;

            ViewHolder(View v) {
                super(v);
                textView = v.findViewById(R.id.et);
            }
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.recyclerview_items, parent, false));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(subjects[position]);
            holder.textView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
                        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            return subjects.length;
        }
    }
}
Gautam Chibde
  • 934
  • 3
  • 9
  • 26
  • i have post my question with ur solution pls suggest what i want do ? – jaya oriya Oct 27 '17 at 06:27
  • @jayaoriya create dialog object first before passing it to the adapter alertDialogBuilder.setView(view2); AlertDialog dialog = alertDialogBuilder.create() recyclerView.setAdapter(new RecyclerViewAdapter(this, subjects, dialog)); – Gautam Chibde Oct 27 '17 at 06:37
  • hi i have create like that only.could pls check my answer – jaya oriya Oct 27 '17 at 07:03
  • in your solution, you have created two AlertDialog **dialog** and **dialog2** you don't need to use **dialog2** use show method on dialog. also use alertDialogBuilder.setView(view2) before AlertDialog dialog = alertDialogBuilder.create(); – Gautam Chibde Oct 27 '17 at 07:16
1

Try this

  • I think you have not taken any edittext in your customAlert
  • analyze your alter_box.xml
  • use this layout and namedas dialog_group_name.xml

<?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">
    <EditText
        android:id="@+id/edt_groupName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top"
        android:paddingLeft="@dimen/_10dp"
        android:paddingRight="@dimen/_10dp"
        android:textSize="@dimen/textSize"
        android:textColor="@color/colorPrimaryDark"
        android:textColorHint="@color/colorText"
        android:hint="@string/enter_groupName"
        android:inputType="textCapWords"
        android:singleLine="true"
        android:layout_marginTop="@dimen/_10dp"
        android:background="@null"
        android:maxLines="1"
        android:scrollbars="vertical" />
</LinearLayout>

  • populate you alert here

public void inflateDialogue() {
        LayoutInflater layoutInflater=SelectUsers.this.getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.dialog_group_name, null);
        AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        alertDialog.setTitle(getResources().getString(R.string.Group_name));

        alertDialog.setCancelable(false);

        edt_groupName = (EditText) view.findViewById(R.id.edt_groupName);

        alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                str_getTextFrom=edt_groupName.getText().toString();
                

                if(edt_groupName.getText().toString().trim().isEmpty()){
                    Toast.makeText(SelectUsers.this, "Empty Group Name", Toast.LENGTH_SHORT).show();
                }else {
                  
                    }
                }

                dialog.dismiss();

            }
        });
        alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.setView(view);
        alertDialog.show();
    }

happy to help you

Community
  • 1
  • 1
Vishal Yadav
  • 993
  • 3
  • 13
  • 28