Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment and force to show keyboard

I have a problem with my DialogFragment. So to create my view, I use the method described on the android blog. Here is my DialogFragment

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     final View myLayout = inflater.inflate(R.layout.dialog_connect, null);      edit = (EditText) myLayout.findViewById(R.id.password_edit);     edit.requestFocus();     getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);      return myLayout; } 

If I use onCreateView(), it works but I would like create an AlterDialog and to do this, I have the following code :

@Override public Dialog onCreateDialog(Bundle savedInstanceState) {     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());     // Inflate and set the layout for the dialog     // Pass null as the parent view because its going in the dialog layout     builder            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int id) {                     callback.onYesConnectClick(edit.getText().toString());                 }             })             .setNegativeButton(R.string.refuse, new DialogInterface.OnClickListener() {                 @Override                 public void onClick(DialogInterface dialog, int id) {                     callback.onNoConnectClick();                 }             });      return builder.create(); } 

If I comment the code from onCreateView(), the app works but I can't force the keyboard to be shown and if I uncomment onCreateView(), I get a crash. Here is the stack trace :

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.ProfileActivity_}: android.util.AndroidRuntimeException:     requestFeature() must be called before adding content AndroidRuntime at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2312) AndroidRuntime at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2362) AndroidRuntime at android.app.ActivityThread.access$600(ActivityThread.java:156) AndroidRuntime at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1250) AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:99) AndroidRuntime at android.os.Looper.loop(Looper.java:137) AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:5229) AndroidRuntime at java.lang.reflect.Method.invokeNative(Native Method) AndroidRuntime at java.lang.reflect.Method.invoke(Method.java:525) AndroidRuntime at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799) AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) AndroidRuntime at dalvik.system.NativeStart.main(Native Method) AndroidRuntime Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 

So my question ==> Can I use the AlertDialog and show the keyboard when the dialog appears ?

like image 819
mrroboaat Avatar asked Jun 21 '13 14:06

mrroboaat


People also ask

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do I dismiss my keyboard on Android?

To dismiss the keyboard, call clearFocus() on the respective element when the button is clicked.

How do I know if DialogFragment is showing?

This is gonna be disappointing to you "great" finding but just call progressDialog. showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.


1 Answers

override onActivityCreated in your dialogfragment and put getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); in there

like image 153
tyczj Avatar answered Oct 21 '22 11:10

tyczj