get a value from a CheckBox in Android










0















I would like to get the booleanvalue of my checkbox that is in my Fragment and get that value in MainActivity . The Fragment is related to an Activity.
Fragment:



@Override
public void onCreate (@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);



}


Activity



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new MyPreferenceFragment()).commit();


Can someone please explain to me how i make it. I just tried with an Interface but i got always a null error like the checkbox isnt initialize



resroot










share


Activity



@Override
protected void onCreate(Bundle savedInstanceState)  










0












0








0








I would like to get the booleanvalue of my checkbox that is in my Fragment and get that value in MainActivity . The Fragment is related to an Activity.
Fragment:



@Override
public void onCreate (@Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);






Activity



@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new MyPreferenceFragment()).commit();


Can someone please explain to me how i make it. I just tried with an Interface but i got always a null error like the checkbox isnt initialize



resroot










share


Activity



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new MyPreferenceFragment()).commit();


Can someone please explain to me how i make it. I just tried with an Interface but i got always a null error like the checkbox isnt initialize



resroot







java android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 18:43









Mat

1,31032030




1,31032030










asked Nov 13 '18 at 16:07









Marco SchwarzMarco Schwarz

114




114












  • You should look into how to define and use callbacks Look here

    – Haroun Hajem
    Nov 13 '18 at 16:32

















  • You should look into how to define and use callbacks Look here

    – Haroun Hajem
    Nov 13 '18 at 16:32
















You should look into how to define and use callbacks Look here

– Haroun Hajem
Nov 13 '18 at 16:32





You should look into how to define and use callbacks Look here

– Haroun Hajem
Nov 13 '18 at 16:32












2 Answers
2






active

oldest

votes


















0














I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence).
findPreference returns a Preference object that would give you values.






share|improve this answer






























    0














    This is how you bind and get the value of a checkbox :



    Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
    boolean checkboxState = cb.isChecked();


    now in order to pass that information to your activity with callbacks, you do the following:



    in your fragment create an interface:



    public interface MyInterface 
    public void onMyInterface(boolean b);



    in the onAttach() method include the following:



    MyInterface myInterface;

    @Override
    public void onAttach(Context context)
    super.onAttach(context);
    myInterface = (MyInterface) context;



    when you need to send the data to the activity, within your fragment add the following:



    sendBooleanCheckBox(checkBoxState);


    where:



    public void sendBooleanCheckBox(boolean b) 
    myInterface.onMyInterface(b);



    and in your activity:



    @Override
    public void onMyInterface(boolean b)
    Log.d("TAG","check box value is: " + b);



    make sure that your activity implements the interface:



    public MyActivity implements OnMyInterface {...





    share|improve this answer























    • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

      – Marco Schwarz
      Nov 13 '18 at 17:59











    • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

      – Nikos Hidalgo
      Nov 14 '18 at 9:40










    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%2f53285008%2fget-a-value-from-a-checkbox-in-android%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














    I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence).
    findPreference returns a Preference object that would give you values.






    share|improve this answer



























      0














      I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence).
      findPreference returns a Preference object that would give you values.






      share|improve this answer

























        0












        0








        0







        I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence).
        findPreference returns a Preference object that would give you values.






        share|improve this answer













        I believe you are using PreferenceFragmentCompat. In the documentation I see that one can findPreference by using the method findPreference(key) findPreference(java.lang.CharSequence).
        findPreference returns a Preference object that would give you values.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 16:27









        Gaurav VashisthGaurav Vashisth

        3,95052650




        3,95052650























            0














            This is how you bind and get the value of a checkbox :



            Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
            boolean checkboxState = cb.isChecked();


            now in order to pass that information to your activity with callbacks, you do the following:



            in your fragment create an interface:



            public interface MyInterface 
            public void onMyInterface(boolean b);



            in the onAttach() method include the following:



            MyInterface myInterface;

            @Override
            public void onAttach(Context context)
            super.onAttach(context);
            myInterface = (MyInterface) context;



            when you need to send the data to the activity, within your fragment add the following:



            sendBooleanCheckBox(checkBoxState);


            where:



            public void sendBooleanCheckBox(boolean b) 
            myInterface.onMyInterface(b);



            and in your activity:



            @Override
            public void onMyInterface(boolean b)
            Log.d("TAG","check box value is: " + b);



            make sure that your activity implements the interface:



            public MyActivity implements OnMyInterface {...





            share|improve this answer























            • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

              – Marco Schwarz
              Nov 13 '18 at 17:59











            • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

              – Nikos Hidalgo
              Nov 14 '18 at 9:40















            0














            This is how you bind and get the value of a checkbox :



            Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
            boolean checkboxState = cb.isChecked();


            now in order to pass that information to your activity with callbacks, you do the following:



            in your fragment create an interface:



            public interface MyInterface 
            public void onMyInterface(boolean b);



            in the onAttach() method include the following:



            MyInterface myInterface;

            @Override
            public void onAttach(Context context)
            super.onAttach(context);
            myInterface = (MyInterface) context;



            when you need to send the data to the activity, within your fragment add the following:



            sendBooleanCheckBox(checkBoxState);


            where:



            public void sendBooleanCheckBox(boolean b) 
            myInterface.onMyInterface(b);



            and in your activity:



            @Override
            public void onMyInterface(boolean b)
            Log.d("TAG","check box value is: " + b);



            make sure that your activity implements the interface:



            public MyActivity implements OnMyInterface {...





            share|improve this answer























            • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

              – Marco Schwarz
              Nov 13 '18 at 17:59











            • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

              – Nikos Hidalgo
              Nov 14 '18 at 9:40













            0












            0








            0







            This is how you bind and get the value of a checkbox :



            Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
            boolean checkboxState = cb.isChecked();


            now in order to pass that information to your activity with callbacks, you do the following:



            in your fragment create an interface:



            public interface MyInterface 
            public void onMyInterface(boolean b);



            in the onAttach() method include the following:



            MyInterface myInterface;

            @Override
            public void onAttach(Context context)
            super.onAttach(context);
            myInterface = (MyInterface) context;



            when you need to send the data to the activity, within your fragment add the following:



            sendBooleanCheckBox(checkBoxState);


            where:



            public void sendBooleanCheckBox(boolean b) 
            myInterface.onMyInterface(b);



            and in your activity:



            @Override
            public void onMyInterface(boolean b)
            Log.d("TAG","check box value is: " + b);



            make sure that your activity implements the interface:



            public MyActivity implements OnMyInterface {...





            share|improve this answer













            This is how you bind and get the value of a checkbox :



            Checkbox cb = (CheckBox)findViewById(R.id.yourCheckBox);
            boolean checkboxState = cb.isChecked();


            now in order to pass that information to your activity with callbacks, you do the following:



            in your fragment create an interface:



            public interface MyInterface 
            public void onMyInterface(boolean b);



            in the onAttach() method include the following:



            MyInterface myInterface;

            @Override
            public void onAttach(Context context)
            super.onAttach(context);
            myInterface = (MyInterface) context;



            when you need to send the data to the activity, within your fragment add the following:



            sendBooleanCheckBox(checkBoxState);


            where:



            public void sendBooleanCheckBox(boolean b) 
            myInterface.onMyInterface(b);



            and in your activity:



            @Override
            public void onMyInterface(boolean b)
            Log.d("TAG","check box value is: " + b);



            make sure that your activity implements the interface:



            public MyActivity implements OnMyInterface {...






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 13 '18 at 16:34









            Nikos HidalgoNikos Hidalgo

            918115




            918115












            • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

              – Marco Schwarz
              Nov 13 '18 at 17:59











            • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

              – Nikos Hidalgo
              Nov 14 '18 at 9:40

















            • Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

              – Marco Schwarz
              Nov 13 '18 at 17:59











            • that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

              – Nikos Hidalgo
              Nov 14 '18 at 9:40
















            Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

            – Marco Schwarz
            Nov 13 '18 at 17:59





            Thanks a lot, so i did but now can you say me how i can use the method onMyInterface? Because when i try with it i get nothing

            – Marco Schwarz
            Nov 13 '18 at 17:59













            that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

            – Nikos Hidalgo
            Nov 14 '18 at 9:40





            that method is your callback method, so when you use it in the fragment, the activity listens and fires the same method. Right now the method on the activity only prints out the value of your boolean variable to the log, but you can use that value to do whatever you want with it. For example, define a variable in your Activity class boolean checkBoxValue; then within the @override public void onMyInterface(boolean b) checkBoxValue = b; and use that value from there.

            – Nikos Hidalgo
            Nov 14 '18 at 9:40

















            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%2f53285008%2fget-a-value-from-a-checkbox-in-android%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







            這個網誌中的熱門文章

            What does pagestruct do in Eviews?

            Dutch intervention in Lombok and Karangasem

            Channel Islands