Clicking a Button if a text field contains “r” / carriage return











up vote
-1
down vote

favorite












i am absolutely new to Android development, i have found one example i am learning from.



i want to perform a Button Click or in this Case: onClickDone when a r / carriage return was in the inputTitle Textbox.



the function is at the




/app/src/main/java/com/hazmirulafiq/androidsqlitedatabasedemo/AddItem.java




file



here is the source: Code



is there a uncomplicated way to accomplish this ?
or can someone help me get on the way ?



many thanks in advance










share|improve this question




























    up vote
    -1
    down vote

    favorite












    i am absolutely new to Android development, i have found one example i am learning from.



    i want to perform a Button Click or in this Case: onClickDone when a r / carriage return was in the inputTitle Textbox.



    the function is at the




    /app/src/main/java/com/hazmirulafiq/androidsqlitedatabasedemo/AddItem.java




    file



    here is the source: Code



    is there a uncomplicated way to accomplish this ?
    or can someone help me get on the way ?



    many thanks in advance










    share|improve this question


























      up vote
      -1
      down vote

      favorite









      up vote
      -1
      down vote

      favorite











      i am absolutely new to Android development, i have found one example i am learning from.



      i want to perform a Button Click or in this Case: onClickDone when a r / carriage return was in the inputTitle Textbox.



      the function is at the




      /app/src/main/java/com/hazmirulafiq/androidsqlitedatabasedemo/AddItem.java




      file



      here is the source: Code



      is there a uncomplicated way to accomplish this ?
      or can someone help me get on the way ?



      many thanks in advance










      share|improve this question















      i am absolutely new to Android development, i have found one example i am learning from.



      i want to perform a Button Click or in this Case: onClickDone when a r / carriage return was in the inputTitle Textbox.



      the function is at the




      /app/src/main/java/com/hazmirulafiq/androidsqlitedatabasedemo/AddItem.java




      file



      here is the source: Code



      is there a uncomplicated way to accomplish this ?
      or can someone help me get on the way ?



      many thanks in advance







      android button text return contains






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 2 days ago

























      asked 2 days ago









      user982998

      3417




      3417
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          I think you might be looking for a TextWatcher. You can assign it to an EditText:



          inputTitle.addTextChangedListener(new TextWatcher() {
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) {

          }

          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {

          String str = s.toString();

          if(str.length() > 0 && str.charAt(str.length() - 1) == 'n') {
          //Run the onClick code here
          }


          }

          @Override
          public void afterTextChanged(Editable s) {

          }
          });





          share|improve this answer























          • Hi! this has to be at the onCreate area or above at the public class?
            – user982998
            2 days ago










          • Inside onCreate, after dbManager.open(); line
            – Kosovir
            2 days ago










          • thank you, that has worked for me :)
            – user982998
            2 days ago


















          up vote
          0
          down vote













          I think that changing the if/else statemend can do the trick.

          Actually an empty TextBox is empty when there is no text, that's correct.

          But what if you try using the string method contains('n); after getText()?



              public void onClickDone(View view) {
          String myInputTitle = inputTitle.getText().toString();
          String myInputDesc = inputDesc.getText().toString();
          if (myInputTitle.isEmpty() || myInputDesc.isEmpty() || !myInputTitle.getText().toString().contains('n')) {
          Snackbar.make(view, "Please fill in both form!",
          Snackbar.LENGTH_SHORT).show();
          } else {
          dbManager.insert(myInputTitle, myInputDesc);
          Intent intent = new Intent(AddItem.this,
          MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(intent);
          }
          }





          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',
            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%2f53373356%2fclicking-a-button-if-a-text-field-contains-r-carriage-return%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








            up vote
            1
            down vote



            accepted










            I think you might be looking for a TextWatcher. You can assign it to an EditText:



            inputTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            String str = s.toString();

            if(str.length() > 0 && str.charAt(str.length() - 1) == 'n') {
            //Run the onClick code here
            }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
            });





            share|improve this answer























            • Hi! this has to be at the onCreate area or above at the public class?
              – user982998
              2 days ago










            • Inside onCreate, after dbManager.open(); line
              – Kosovir
              2 days ago










            • thank you, that has worked for me :)
              – user982998
              2 days ago















            up vote
            1
            down vote



            accepted










            I think you might be looking for a TextWatcher. You can assign it to an EditText:



            inputTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            String str = s.toString();

            if(str.length() > 0 && str.charAt(str.length() - 1) == 'n') {
            //Run the onClick code here
            }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
            });





            share|improve this answer























            • Hi! this has to be at the onCreate area or above at the public class?
              – user982998
              2 days ago










            • Inside onCreate, after dbManager.open(); line
              – Kosovir
              2 days ago










            • thank you, that has worked for me :)
              – user982998
              2 days ago













            up vote
            1
            down vote



            accepted







            up vote
            1
            down vote



            accepted






            I think you might be looking for a TextWatcher. You can assign it to an EditText:



            inputTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            String str = s.toString();

            if(str.length() > 0 && str.charAt(str.length() - 1) == 'n') {
            //Run the onClick code here
            }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
            });





            share|improve this answer














            I think you might be looking for a TextWatcher. You can assign it to an EditText:



            inputTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            String str = s.toString();

            if(str.length() > 0 && str.charAt(str.length() - 1) == 'n') {
            //Run the onClick code here
            }


            }

            @Override
            public void afterTextChanged(Editable s) {

            }
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 2 days ago

























            answered 2 days ago









            Kosovir

            417




            417












            • Hi! this has to be at the onCreate area or above at the public class?
              – user982998
              2 days ago










            • Inside onCreate, after dbManager.open(); line
              – Kosovir
              2 days ago










            • thank you, that has worked for me :)
              – user982998
              2 days ago


















            • Hi! this has to be at the onCreate area or above at the public class?
              – user982998
              2 days ago










            • Inside onCreate, after dbManager.open(); line
              – Kosovir
              2 days ago










            • thank you, that has worked for me :)
              – user982998
              2 days ago
















            Hi! this has to be at the onCreate area or above at the public class?
            – user982998
            2 days ago




            Hi! this has to be at the onCreate area or above at the public class?
            – user982998
            2 days ago












            Inside onCreate, after dbManager.open(); line
            – Kosovir
            2 days ago




            Inside onCreate, after dbManager.open(); line
            – Kosovir
            2 days ago












            thank you, that has worked for me :)
            – user982998
            2 days ago




            thank you, that has worked for me :)
            – user982998
            2 days ago












            up vote
            0
            down vote













            I think that changing the if/else statemend can do the trick.

            Actually an empty TextBox is empty when there is no text, that's correct.

            But what if you try using the string method contains('n); after getText()?



                public void onClickDone(View view) {
            String myInputTitle = inputTitle.getText().toString();
            String myInputDesc = inputDesc.getText().toString();
            if (myInputTitle.isEmpty() || myInputDesc.isEmpty() || !myInputTitle.getText().toString().contains('n')) {
            Snackbar.make(view, "Please fill in both form!",
            Snackbar.LENGTH_SHORT).show();
            } else {
            dbManager.insert(myInputTitle, myInputDesc);
            Intent intent = new Intent(AddItem.this,
            MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            }
            }





            share|improve this answer

























              up vote
              0
              down vote













              I think that changing the if/else statemend can do the trick.

              Actually an empty TextBox is empty when there is no text, that's correct.

              But what if you try using the string method contains('n); after getText()?



                  public void onClickDone(View view) {
              String myInputTitle = inputTitle.getText().toString();
              String myInputDesc = inputDesc.getText().toString();
              if (myInputTitle.isEmpty() || myInputDesc.isEmpty() || !myInputTitle.getText().toString().contains('n')) {
              Snackbar.make(view, "Please fill in both form!",
              Snackbar.LENGTH_SHORT).show();
              } else {
              dbManager.insert(myInputTitle, myInputDesc);
              Intent intent = new Intent(AddItem.this,
              MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
              startActivity(intent);
              }
              }





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I think that changing the if/else statemend can do the trick.

                Actually an empty TextBox is empty when there is no text, that's correct.

                But what if you try using the string method contains('n); after getText()?



                    public void onClickDone(View view) {
                String myInputTitle = inputTitle.getText().toString();
                String myInputDesc = inputDesc.getText().toString();
                if (myInputTitle.isEmpty() || myInputDesc.isEmpty() || !myInputTitle.getText().toString().contains('n')) {
                Snackbar.make(view, "Please fill in both form!",
                Snackbar.LENGTH_SHORT).show();
                } else {
                dbManager.insert(myInputTitle, myInputDesc);
                Intent intent = new Intent(AddItem.this,
                MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                }
                }





                share|improve this answer












                I think that changing the if/else statemend can do the trick.

                Actually an empty TextBox is empty when there is no text, that's correct.

                But what if you try using the string method contains('n); after getText()?



                    public void onClickDone(View view) {
                String myInputTitle = inputTitle.getText().toString();
                String myInputDesc = inputDesc.getText().toString();
                if (myInputTitle.isEmpty() || myInputDesc.isEmpty() || !myInputTitle.getText().toString().contains('n')) {
                Snackbar.make(view, "Please fill in both form!",
                Snackbar.LENGTH_SHORT).show();
                } else {
                dbManager.insert(myInputTitle, myInputDesc);
                Intent intent = new Intent(AddItem.this,
                MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 days ago









                b00leant

                468




                468






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373356%2fclicking-a-button-if-a-text-field-contains-r-carriage-return%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    Npm cannot find a required file even through it is in the searched directory