How to prevent custom DialogFragment from hiding keyboard when being shown The Next CEO of Stack OverflowViewPager in DialogFragment - IllegalStateException: Fragment does not have a viewClose/hide the Android Soft KeyboardHow to prevent a dialog from closing when a button is clickedAndroid: NullPointerException while replacing FrameLayoutAndroid facebook event requestHow to retrieve image in a gridview from fragment?null pointer exception with listview.setadapter() in fragment in method oncreateviewAndroid studio showing error in graph.addSeries(series). The android studio is showing red underline in series of graph.add(series)Weird OutOfMemoryError when loading view pagerFragments very slowing work while it loadHow to save CHOICE MODE MULTIPLE
0 rank tensor vs 1D vector
I want to delete every two lines after 3rd lines in file contain very large number of lines :
Rotate a column
Is it possible to use a NPN BJT as switch, from single power source?
Is there a way to save my career from absolute disaster?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
How to get from Geneva Airport to Metabief, Doubs, France by public transport?
Domestic-to-international connection at Orlando (MCO)
How to invert MapIndexed on a ragged structure? How to construct a tree from rules?
Why didn't Khan get resurrected in the Genesis Explosion?
What connection does MS Office have to Netscape Navigator?
Easy to read palindrome checker
Legal workarounds for testamentary trust perceived as unfair
How to prove a simple equation?
Calculator final project in Python
Prepend last line of stdin to entire stdin
Why, when going from special to general relativity, do we just replace partial derivatives with covariant derivatives?
Does increasing your ability score affect your main stat?
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
Make solar eclipses exceedingly rare, but still have new moons
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Is the D&D universe the same as the Forgotten Realms universe?
How to avoid supervisors with prejudiced views?
RigExpert AA-35 - Interpreting The Information
How to prevent custom DialogFragment from hiding keyboard when being shown
The Next CEO of Stack OverflowViewPager in DialogFragment - IllegalStateException: Fragment does not have a viewClose/hide the Android Soft KeyboardHow to prevent a dialog from closing when a button is clickedAndroid: NullPointerException while replacing FrameLayoutAndroid facebook event requestHow to retrieve image in a gridview from fragment?null pointer exception with listview.setadapter() in fragment in method oncreateviewAndroid studio showing error in graph.addSeries(series). The android studio is showing red underline in series of graph.add(series)Weird OutOfMemoryError when loading view pagerFragments very slowing work while it loadHow to save CHOICE MODE MULTIPLE
There are 2 ways to create custom dialog via DialogFragment
.
- Overwrite
onCreateDialog
and return a dialog usingAlertDialog.Builder
. - Overwrite
onCreateView
.
We notice that, if we overwrite onCreateDialog
, the previous shown soft keyboard will not be hidden.
However, if we overwrite onCreateView
, the previous shown soft keyboard will be hidden.
onCreateDialog will not hide soft keyboard
onCreateView will hide soft keyboard
Hiding soft keyboard is not our desired behavior. We want the soft keyboard to remain same as it is.
However, we are not able to use onCreateDialog
way, due to the limitation mentioned ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view . In nutshell, if you need ViewPager
to work well in dialog, you cannot implement custom dialog using onCreateDialog
.
If we use onCreateView
, we can achieve everything desired, except "not hiding soft keyboard".
Do you have any idea why overwrite onCreateView
to create custom dialog, will hide the keyboard? How can we prevent such behavior?
Code
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
public class ColorDialogFragment extends DialogFragment
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
// We provide custom style, because we need title.
colorDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
return view;
// We overwrite onCreateView because ViewPager in DialogFragment, can hardly play well with
// onCreateDialog + AlertDialog.Builder.
//
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return onCreateView(inflater, container);
// We overwrite onCreateDialog, because we need title.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.select_a_color);
return dialog;
android android-softkeyboard android-dialogfragment
add a comment |
There are 2 ways to create custom dialog via DialogFragment
.
- Overwrite
onCreateDialog
and return a dialog usingAlertDialog.Builder
. - Overwrite
onCreateView
.
We notice that, if we overwrite onCreateDialog
, the previous shown soft keyboard will not be hidden.
However, if we overwrite onCreateView
, the previous shown soft keyboard will be hidden.
onCreateDialog will not hide soft keyboard
onCreateView will hide soft keyboard
Hiding soft keyboard is not our desired behavior. We want the soft keyboard to remain same as it is.
However, we are not able to use onCreateDialog
way, due to the limitation mentioned ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view . In nutshell, if you need ViewPager
to work well in dialog, you cannot implement custom dialog using onCreateDialog
.
If we use onCreateView
, we can achieve everything desired, except "not hiding soft keyboard".
Do you have any idea why overwrite onCreateView
to create custom dialog, will hide the keyboard? How can we prevent such behavior?
Code
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
public class ColorDialogFragment extends DialogFragment
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
// We provide custom style, because we need title.
colorDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
return view;
// We overwrite onCreateView because ViewPager in DialogFragment, can hardly play well with
// onCreateDialog + AlertDialog.Builder.
//
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return onCreateView(inflater, container);
// We overwrite onCreateDialog, because we need title.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.select_a_color);
return dialog;
android android-softkeyboard android-dialogfragment
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49
add a comment |
There are 2 ways to create custom dialog via DialogFragment
.
- Overwrite
onCreateDialog
and return a dialog usingAlertDialog.Builder
. - Overwrite
onCreateView
.
We notice that, if we overwrite onCreateDialog
, the previous shown soft keyboard will not be hidden.
However, if we overwrite onCreateView
, the previous shown soft keyboard will be hidden.
onCreateDialog will not hide soft keyboard
onCreateView will hide soft keyboard
Hiding soft keyboard is not our desired behavior. We want the soft keyboard to remain same as it is.
However, we are not able to use onCreateDialog
way, due to the limitation mentioned ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view . In nutshell, if you need ViewPager
to work well in dialog, you cannot implement custom dialog using onCreateDialog
.
If we use onCreateView
, we can achieve everything desired, except "not hiding soft keyboard".
Do you have any idea why overwrite onCreateView
to create custom dialog, will hide the keyboard? How can we prevent such behavior?
Code
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
public class ColorDialogFragment extends DialogFragment
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
// We provide custom style, because we need title.
colorDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
return view;
// We overwrite onCreateView because ViewPager in DialogFragment, can hardly play well with
// onCreateDialog + AlertDialog.Builder.
//
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return onCreateView(inflater, container);
// We overwrite onCreateDialog, because we need title.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.select_a_color);
return dialog;
android android-softkeyboard android-dialogfragment
There are 2 ways to create custom dialog via DialogFragment
.
- Overwrite
onCreateDialog
and return a dialog usingAlertDialog.Builder
. - Overwrite
onCreateView
.
We notice that, if we overwrite onCreateDialog
, the previous shown soft keyboard will not be hidden.
However, if we overwrite onCreateView
, the previous shown soft keyboard will be hidden.
onCreateDialog will not hide soft keyboard
onCreateView will hide soft keyboard
Hiding soft keyboard is not our desired behavior. We want the soft keyboard to remain same as it is.
However, we are not able to use onCreateDialog
way, due to the limitation mentioned ViewPager in DialogFragment - IllegalStateException: Fragment does not have a view . In nutshell, if you need ViewPager
to work well in dialog, you cannot implement custom dialog using onCreateDialog
.
If we use onCreateView
, we can achieve everything desired, except "not hiding soft keyboard".
Do you have any idea why overwrite onCreateView
to create custom dialog, will hide the keyboard? How can we prevent such behavior?
Code
<style name="CustomDialog" parent="@style/Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
public class ColorDialogFragment extends DialogFragment
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
// We provide custom style, because we need title.
colorDialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.CustomDialog);
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
return view;
// We overwrite onCreateView because ViewPager in DialogFragment, can hardly play well with
// onCreateDialog + AlertDialog.Builder.
//
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return onCreateView(inflater, container);
// We overwrite onCreateDialog, because we need title.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setTitle(R.string.select_a_color);
return dialog;
android android-softkeyboard android-dialogfragment
android android-softkeyboard android-dialogfragment
edited Mar 8 at 16:21
VikaS
1
1
asked Mar 8 at 16:12
Cheok Yan ChengCheok Yan Cheng
18.4k95346646
18.4k95346646
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49
add a comment |
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49
add a comment |
1 Answer
1
active
oldest
votes
After day of experiment, I found out a solution!
Overwrite both onCreateDialog
and onCreateView
. Store view created at onCreateDialog
in a member variable, and let onCreateView
return that member variable.
Reference: https://stackoverflow.com/a/51530917/72437
Here's the complete code.
public class ColorDialogFragment extends DialogFragment
private View view;
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// TODO: Read from WeNoteOptions.
final int position = 0;
this.viewPager.setCurrentItem(position);
updateButtonVisibility(position);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
updateButtonVisibility(position);
@Override
public void onPageScrollStateChanged(int state)
);
return view;
private void updateButtonVisibility(int position)
if (position == 1)
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
else
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return this.view;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
this.view = onCreateView(layoutInflater, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.select_a_color)
.setView(this.view)
.setPositiveButton(R.string.select_color, (dialogInterface, i) ->
);
return alertDialogBuilder.create();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066977%2fhow-to-prevent-custom-dialogfragment-from-hiding-keyboard-when-being-shown%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
After day of experiment, I found out a solution!
Overwrite both onCreateDialog
and onCreateView
. Store view created at onCreateDialog
in a member variable, and let onCreateView
return that member variable.
Reference: https://stackoverflow.com/a/51530917/72437
Here's the complete code.
public class ColorDialogFragment extends DialogFragment
private View view;
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// TODO: Read from WeNoteOptions.
final int position = 0;
this.viewPager.setCurrentItem(position);
updateButtonVisibility(position);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
updateButtonVisibility(position);
@Override
public void onPageScrollStateChanged(int state)
);
return view;
private void updateButtonVisibility(int position)
if (position == 1)
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
else
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return this.view;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
this.view = onCreateView(layoutInflater, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.select_a_color)
.setView(this.view)
.setPositiveButton(R.string.select_color, (dialogInterface, i) ->
);
return alertDialogBuilder.create();
add a comment |
After day of experiment, I found out a solution!
Overwrite both onCreateDialog
and onCreateView
. Store view created at onCreateDialog
in a member variable, and let onCreateView
return that member variable.
Reference: https://stackoverflow.com/a/51530917/72437
Here's the complete code.
public class ColorDialogFragment extends DialogFragment
private View view;
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// TODO: Read from WeNoteOptions.
final int position = 0;
this.viewPager.setCurrentItem(position);
updateButtonVisibility(position);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
updateButtonVisibility(position);
@Override
public void onPageScrollStateChanged(int state)
);
return view;
private void updateButtonVisibility(int position)
if (position == 1)
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
else
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return this.view;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
this.view = onCreateView(layoutInflater, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.select_a_color)
.setView(this.view)
.setPositiveButton(R.string.select_color, (dialogInterface, i) ->
);
return alertDialogBuilder.create();
add a comment |
After day of experiment, I found out a solution!
Overwrite both onCreateDialog
and onCreateView
. Store view created at onCreateDialog
in a member variable, and let onCreateView
return that member variable.
Reference: https://stackoverflow.com/a/51530917/72437
Here's the complete code.
public class ColorDialogFragment extends DialogFragment
private View view;
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// TODO: Read from WeNoteOptions.
final int position = 0;
this.viewPager.setCurrentItem(position);
updateButtonVisibility(position);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
updateButtonVisibility(position);
@Override
public void onPageScrollStateChanged(int state)
);
return view;
private void updateButtonVisibility(int position)
if (position == 1)
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
else
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return this.view;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
this.view = onCreateView(layoutInflater, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.select_a_color)
.setView(this.view)
.setPositiveButton(R.string.select_color, (dialogInterface, i) ->
);
return alertDialogBuilder.create();
After day of experiment, I found out a solution!
Overwrite both onCreateDialog
and onCreateView
. Store view created at onCreateDialog
in a member variable, and let onCreateView
return that member variable.
Reference: https://stackoverflow.com/a/51530917/72437
Here's the complete code.
public class ColorDialogFragment extends DialogFragment
private View view;
private TabLayout tabLayout;
private ViewPager viewPager;
private ColorFragmentPagerAdapter colorFragmentPagerAdapter;
public static ColorDialogFragment newInstance()
ColorDialogFragment colorDialogFragment = new ColorDialogFragment();
return colorDialogFragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
@Override
public void onResume()
super.onResume();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
// TODO: Read from WeNoteOptions.
final int position = 0;
this.viewPager.setCurrentItem(position);
updateButtonVisibility(position);
private View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container)
View view = inflater.inflate(R.layout.color_dialog_fragment, container, false);
this.tabLayout = view.findViewById(R.id.tab_layout);
this.viewPager = view.findViewById(R.id.view_pager);
this.colorFragmentPagerAdapter = new ColorFragmentPagerAdapter(this.getChildFragmentManager());
this.viewPager.setAdapter(this.colorFragmentPagerAdapter);
this.tabLayout.setupWithViewPager(this.viewPager);
this.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
@Override
public void onPageSelected(int position)
updateButtonVisibility(position);
@Override
public void onPageScrollStateChanged(int state)
);
return view;
private void updateButtonVisibility(int position)
if (position == 1)
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.VISIBLE);
else
((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE).setVisibility(View.INVISIBLE);
// https://stackoverflow.com/questions/20303865/viewpager-in-dialogfragment-illegalstateexception-fragment-does-not-have-a-vi
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
return this.view;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
LayoutInflater layoutInflater = getActivity().getLayoutInflater();
this.view = onCreateView(layoutInflater, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.select_a_color)
.setView(this.view)
.setPositiveButton(R.string.select_color, (dialogInterface, i) ->
);
return alertDialogBuilder.create();
answered Mar 9 at 4:34
Cheok Yan ChengCheok Yan Cheng
18.4k95346646
18.4k95346646
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55066977%2fhow-to-prevent-custom-dialogfragment-from-hiding-keyboard-when-being-shown%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Oh this is a mess, what if we hide the keyboard and turn it on again when this popup appears? :-/
– Manoj Perumarath
Mar 8 at 16:42
@ManojPerumarath We want to avoid that because (1) It doesn't yield a good user experience. (2) It is difficult to achieve. Need to remember the focus location/ component of parent, and then restore back when dialog is closed.
– Cheok Yan Cheng
Mar 8 at 16:49