Why is OnClickListener not working from Fragment used as a Alertdialog












0














I am using a fragment as a alertdialog. Inside the fragment I have a onclick listener, but it does not fire. What can I do?



Fragment used as alertdialog code:



        AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
View mview = getLayoutInflater().inflate(R.layout.fragment_add_opskrift, null);
mBuilder.setView((mview));
AlertDialog dialog = mBuilder.create();
dialog.show();


Onclicklistener inside the fragment code:



public class AddOpskrift extends Fragment  {

private static final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_add_opskrift, container, false);

filepath_name = (TextView) view.findViewById((R.id.billede_path)) ;


Button soeg_billede = (Button) view.findViewById(R.id.billede_button);
soeg_billede.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);


}

});

return view;


Xaml:



<Button
android:id="@+id/billede_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="@color/colorPrimary"
android:drawableRight="@drawable/ic_add_photo_white"
android:padding="10dp"
android:text="Tilføj billede "
android:textColor="@color/white" />









share|improve this question
























  • "I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
    – Mike M.
    Nov 21 '18 at 19:39










  • Thanks for your informative comment. Please let me know how it is useful for me in this case.
    – Mohammad Saad
    Nov 21 '18 at 19:46










  • Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
    – Mike M.
    Nov 21 '18 at 19:51












  • No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
    – Mohammad Saad
    Nov 21 '18 at 19:56






  • 1




    It is working now. Thanks for the help :)
    – Mohammad Saad
    Nov 21 '18 at 20:47
















0














I am using a fragment as a alertdialog. Inside the fragment I have a onclick listener, but it does not fire. What can I do?



Fragment used as alertdialog code:



        AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
View mview = getLayoutInflater().inflate(R.layout.fragment_add_opskrift, null);
mBuilder.setView((mview));
AlertDialog dialog = mBuilder.create();
dialog.show();


Onclicklistener inside the fragment code:



public class AddOpskrift extends Fragment  {

private static final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_add_opskrift, container, false);

filepath_name = (TextView) view.findViewById((R.id.billede_path)) ;


Button soeg_billede = (Button) view.findViewById(R.id.billede_button);
soeg_billede.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);


}

});

return view;


Xaml:



<Button
android:id="@+id/billede_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="@color/colorPrimary"
android:drawableRight="@drawable/ic_add_photo_white"
android:padding="10dp"
android:text="Tilføj billede "
android:textColor="@color/white" />









share|improve this question
























  • "I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
    – Mike M.
    Nov 21 '18 at 19:39










  • Thanks for your informative comment. Please let me know how it is useful for me in this case.
    – Mohammad Saad
    Nov 21 '18 at 19:46










  • Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
    – Mike M.
    Nov 21 '18 at 19:51












  • No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
    – Mohammad Saad
    Nov 21 '18 at 19:56






  • 1




    It is working now. Thanks for the help :)
    – Mohammad Saad
    Nov 21 '18 at 20:47














0












0








0







I am using a fragment as a alertdialog. Inside the fragment I have a onclick listener, but it does not fire. What can I do?



Fragment used as alertdialog code:



        AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
View mview = getLayoutInflater().inflate(R.layout.fragment_add_opskrift, null);
mBuilder.setView((mview));
AlertDialog dialog = mBuilder.create();
dialog.show();


Onclicklistener inside the fragment code:



public class AddOpskrift extends Fragment  {

private static final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_add_opskrift, container, false);

filepath_name = (TextView) view.findViewById((R.id.billede_path)) ;


Button soeg_billede = (Button) view.findViewById(R.id.billede_button);
soeg_billede.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);


}

});

return view;


Xaml:



<Button
android:id="@+id/billede_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="@color/colorPrimary"
android:drawableRight="@drawable/ic_add_photo_white"
android:padding="10dp"
android:text="Tilføj billede "
android:textColor="@color/white" />









share|improve this question















I am using a fragment as a alertdialog. Inside the fragment I have a onclick listener, but it does not fire. What can I do?



Fragment used as alertdialog code:



        AlertDialog.Builder mBuilder = new AlertDialog.Builder(v.getContext());
View mview = getLayoutInflater().inflate(R.layout.fragment_add_opskrift, null);
mBuilder.setView((mview));
AlertDialog dialog = mBuilder.create();
dialog.show();


Onclicklistener inside the fragment code:



public class AddOpskrift extends Fragment  {

private static final int RESULT_LOAD_IMAGES = 1;
TextView filepath_name;

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_add_opskrift, container, false);

filepath_name = (TextView) view.findViewById((R.id.billede_path)) ;


Button soeg_billede = (Button) view.findViewById(R.id.billede_button);
soeg_billede.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);


}

});

return view;


Xaml:



<Button
android:id="@+id/billede_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:background="@color/colorPrimary"
android:drawableRight="@drawable/ic_add_photo_white"
android:padding="10dp"
android:text="Tilføj billede "
android:textColor="@color/white" />






android fragment alertdialog






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:43







Mohammad Saad

















asked Nov 21 '18 at 19:36









Mohammad SaadMohammad Saad

154




154












  • "I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
    – Mike M.
    Nov 21 '18 at 19:39










  • Thanks for your informative comment. Please let me know how it is useful for me in this case.
    – Mohammad Saad
    Nov 21 '18 at 19:46










  • Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
    – Mike M.
    Nov 21 '18 at 19:51












  • No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
    – Mohammad Saad
    Nov 21 '18 at 19:56






  • 1




    It is working now. Thanks for the help :)
    – Mohammad Saad
    Nov 21 '18 at 20:47


















  • "I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
    – Mike M.
    Nov 21 '18 at 19:39










  • Thanks for your informative comment. Please let me know how it is useful for me in this case.
    – Mohammad Saad
    Nov 21 '18 at 19:46










  • Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
    – Mike M.
    Nov 21 '18 at 19:51












  • No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
    – Mohammad Saad
    Nov 21 '18 at 19:56






  • 1




    It is working now. Thanks for the help :)
    – Mohammad Saad
    Nov 21 '18 at 20:47
















"I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
– Mike M.
Nov 21 '18 at 19:39




"I am using a fragment as a alertdialog." – No, you're just using the same layout for both the AlertDialog and the Fragment. That does not mean that you're actually using the Fragment there.
– Mike M.
Nov 21 '18 at 19:39












Thanks for your informative comment. Please let me know how it is useful for me in this case.
– Mohammad Saad
Nov 21 '18 at 19:46




Thanks for your informative comment. Please let me know how it is useful for me in this case.
– Mohammad Saad
Nov 21 '18 at 19:46












Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
– Mike M.
Nov 21 '18 at 19:51






Sorry, that was a copy/paste fumble. There should've been a question at the end of that. You don't have a <fragment> in the fragment_add_opskrift layout, do you? Also, if you're trying to get a Dialog as a Fragment, you should be using DialogFragment, instead of a plain Fragment.
– Mike M.
Nov 21 '18 at 19:51














No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
– Mohammad Saad
Nov 21 '18 at 19:56




No problem. I have tried to change it to DialogFragment, but the onclicklistener does not work still.
– Mohammad Saad
Nov 21 '18 at 19:56




1




1




It is working now. Thanks for the help :)
– Mohammad Saad
Nov 21 '18 at 20:47




It is working now. Thanks for the help :)
– Mohammad Saad
Nov 21 '18 at 20:47












2 Answers
2






active

oldest

votes


















0














There is a difference in using fragment's layout with AlertDialog and actually using DialogFragment



DialogFragment is deprecated since API 28.




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







share|improve this answer





























    0














    This code:



    Button soeg_billede = (Button) mview.findViewById(R.id.billede_button);
    soeg_billede.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
    }
    });


    should be written just before you show the dialog with dialog.show();.

    The change needed is mview instead of view for the findViewById()






    share|improve this answer





















    • Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
      – Mohammad Saad
      Nov 21 '18 at 20:02










    • @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
      – forpas
      Nov 21 '18 at 20:06











    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%2f53419384%2fwhy-is-onclicklistener-not-working-from-fragment-used-as-a-alertdialog%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    There is a difference in using fragment's layout with AlertDialog and actually using DialogFragment



    DialogFragment is deprecated since API 28.




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







    share|improve this answer


























      0














      There is a difference in using fragment's layout with AlertDialog and actually using DialogFragment



      DialogFragment is deprecated since API 28.




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







      share|improve this answer
























        0












        0








        0






        There is a difference in using fragment's layout with AlertDialog and actually using DialogFragment



        DialogFragment is deprecated since API 28.




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







        share|improve this answer












        There is a difference in using fragment's layout with AlertDialog and actually using DialogFragment



        DialogFragment is deprecated since API 28.




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








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 19:47









        NIKHIL MAURYANIKHIL MAURYA

        1319




        1319

























            0














            This code:



            Button soeg_billede = (Button) mview.findViewById(R.id.billede_button);
            soeg_billede.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
            }
            });


            should be written just before you show the dialog with dialog.show();.

            The change needed is mview instead of view for the findViewById()






            share|improve this answer





















            • Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
              – Mohammad Saad
              Nov 21 '18 at 20:02










            • @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
              – forpas
              Nov 21 '18 at 20:06
















            0














            This code:



            Button soeg_billede = (Button) mview.findViewById(R.id.billede_button);
            soeg_billede.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
            }
            });


            should be written just before you show the dialog with dialog.show();.

            The change needed is mview instead of view for the findViewById()






            share|improve this answer





















            • Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
              – Mohammad Saad
              Nov 21 '18 at 20:02










            • @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
              – forpas
              Nov 21 '18 at 20:06














            0












            0








            0






            This code:



            Button soeg_billede = (Button) mview.findViewById(R.id.billede_button);
            soeg_billede.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
            }
            });


            should be written just before you show the dialog with dialog.show();.

            The change needed is mview instead of view for the findViewById()






            share|improve this answer












            This code:



            Button soeg_billede = (Button) mview.findViewById(R.id.billede_button);
            soeg_billede.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(galleryIntent, RESULT_LOAD_IMAGES);
            }
            });


            should be written just before you show the dialog with dialog.show();.

            The change needed is mview instead of view for the findViewById()







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 21 '18 at 19:57









            forpasforpas

            9,5071421




            9,5071421












            • Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
              – Mohammad Saad
              Nov 21 '18 at 20:02










            • @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
              – forpas
              Nov 21 '18 at 20:06


















            • Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
              – Mohammad Saad
              Nov 21 '18 at 20:02










            • @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
              – forpas
              Nov 21 '18 at 20:06
















            Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
            – Mohammad Saad
            Nov 21 '18 at 20:02




            Sorry if I wasn't clear enough. The first piece of code is a separate class opening the class mentioned in the second piece of code.
            – Mohammad Saad
            Nov 21 '18 at 20:02












            @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
            – forpas
            Nov 21 '18 at 20:06




            @MohammadSaad The listener must be set before the dialog is shown. This button belongs to the layout that is inflated in the dialog, so you must initialize it and its listener there. If you need to pass parameters like a context, pass them in the initialization of this class that creates the dialog.
            – forpas
            Nov 21 '18 at 20:06


















            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53419384%2fwhy-is-onclicklistener-not-working-from-fragment-used-as-a-alertdialog%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            Refactoring coordinates for Minecraft Pi buildings written in Python