Search code examples
javaandroidandroid-alertdialogandroid-softkeyboard

soft keyboard not appearing


I have a following dialog:

import android.app.AlertDialog;

public class IpDiscoveryDialog extends AlertDialog implements View.OnClickListener {

    private View view;
    private Context context;
    private Activity activity;

    private EditText ipEditText;


    protected IpDiscoveryDialog(Context context) {
        super(context);
        this.context = context;
    }

    public IpDiscoveryDialog(Context context, Activity activity) {
        this(context);
        this.activity = activity;
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(layoutResID, null);
        this.view = view;
        this.setContentView(view);
    }

    @Override
    public void setContentView(@NonNull View view) {
        super.setContentView(view);
        this.buildView(view);
        this.setCancelable(false);
    }

    private void buildView(View view) {
        view.findViewById(R.id.ipConfirmButton).setOnClickListener(this);
        this.ipEditText = view.findViewById(R.id.ipEditText);
    }
}

I open it from a different activity like this:

IpDiscoveryDialog ipDiscoveryDialog = new IpDiscoveryDialog(ScanDevicesActivity.this, ScanDevicesActivity.this);
ipDiscoveryDialog.create();
ipDiscoveryDialog.setContentView(R.layout.ip_discovery_dialog);
ipDiscoveryDialog.show();

And the layout of the dialog is the following:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:id="@+id/textView8"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/set_ip_address"
        android:textAlignment="center"
        android:textColor="@android:color/black"
        android:textSize="24sp" />

    <EditText
        android:id="@+id/ipEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/ip_address"
        android:importantForAutofill="no"
        android:inputType="text" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/ipConfirmButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/confirm" />
</LinearLayout>

The problem I have is that no matter what I do, I cannot open soft keyboard to edit that EditText. I tried open it manually in code, I tried different settings, nothing works.

Code like this does not work:

InputMethodManager mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
mImm.showSoftInput(SearchEdit, InputMethodManager.SHOW_IMPLICIT);

What else can I try? What is the issue here?


Solution

  • Try this class

    public class IpDiscoveryDialog extends AlertDialog implements View.OnClickListener {
    
    private View view;
    private Context context;
    private Activity activity;
    
    private EditText ipEditText;
    
    
    protected IpDiscoveryDialog(Context context) {
        super(context);
        this.context = context;
    }
    
    public IpDiscoveryDialog(Context context, Activity activity) {
        this(context);
        this.activity = activity;
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.ip_discovery_dialog);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        buildView();
        this.setCancelable(false);
    }
    
    private void buildView() {
        findViewById(R.id.ipConfirmButton).setOnClickListener(this);
        this.ipEditText = findViewById(R.id.ipEditText);
    }
    
    @Override
    public void onClick(View v) {
    
    }
    }