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










2















There are 2 ways to create custom dialog via DialogFragment.



  1. Overwrite onCreateDialog and return a dialog using AlertDialog.Builder.

  2. 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



enter image description here




onCreateView will hide soft keyboard



enter image description here




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;











share|improve this question
























  • 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















2















There are 2 ways to create custom dialog via DialogFragment.



  1. Overwrite onCreateDialog and return a dialog using AlertDialog.Builder.

  2. 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



enter image description here




onCreateView will hide soft keyboard



enter image description here




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;











share|improve this question
























  • 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













2












2








2








There are 2 ways to create custom dialog via DialogFragment.



  1. Overwrite onCreateDialog and return a dialog using AlertDialog.Builder.

  2. 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



enter image description here




onCreateView will hide soft keyboard



enter image description here




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;











share|improve this question
















There are 2 ways to create custom dialog via DialogFragment.



  1. Overwrite onCreateDialog and return a dialog using AlertDialog.Builder.

  2. 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



enter image description here




onCreateView will hide soft keyboard



enter image description here




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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















0














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();







share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    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();







    share|improve this answer



























      0














      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();







      share|improve this answer

























        0












        0








        0







        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();







        share|improve this answer













        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();








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 9 at 4:34









        Cheok Yan ChengCheok Yan Cheng

        18.4k95346646




        18.4k95346646





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived