dudi
dudi

Reputation: 5732

Check first Char on String from EditText

I want to read a String from an EditText. This String is a hashtag and should be start with #. So I want to check the first char of the String after the user input. If the string fails the check, I want to give a simple Alert and put the focus again on the input, so the user can try input again.

How can I implement this?

Upvotes: 0

Views: 540

Answers (2)

dudi
dudi

Reputation: 5732

I found this solution for my problem. At first I wrote a method, that checked the first char of the String, makes an Alert, if the first char not equals, and return a boolean.

public boolean checkFirstChar (String s, char c) {
    boolean isConform = false;
    if (s.charAt(0) != c ) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SettingsActivity.this);
        // set title
        alertDialogBuilder.setTitle("Input not conform!");
        // set dialog message
        alertDialogBuilder
                .setMessage("Your hashtag should start with " + c)
                .setCancelable(false)
                .setNegativeButton("OK",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    } else {
        isConform = true;
    }
    return isConform;
}

Then I make an OK Button. On the Button I implement an OnClicklistener. OnClick I get the String from the EditText. Then I put the String into the Method to check the first char. If the Method returns true, I saved the Settings and start the next Activity. If not, I returned to the EditText Focus.

//set onClickListener on OK Button
    btn_ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //get selected Item from Spinner HashTag Item
            strgHashtag = spinner_hashtag.getSelectedItem().toString();

            //get String form EditText addHashTag
            strgAddHashtag = edit_addHashTag.getText().toString();

            //check if the first char == #
            if (checkFirstChar(strgAddHashtag, '#') == true ) {

                //if true, add String to HashTagItem List
                spinner_HashTagItems.add(strgAddHashtag);

                //save settings into a JSON File on SD-Card
                saveSettings();

                //and put the hashTagString into an IntenExtra for the HomeActivity
                Intent intent = new Intent(SettingsActivity.this, HomeActivity.class);
                intent.putExtra("hashTag", strgHashtag);
                startActivity(intent);
                finish();
                //if the char != '#'
            } else {
                //return to the user input
                edit_addHashTag.requestFocus();
            }
        }
    });

I hope this code can help other community member to solve the same problem.

Upvotes: 0

you can check the Edit text by getting ist string context... this can be done invoking the method getTExt and from the string resulting verify the char at begin (index 0)

EditText myInput =....
if(myInput.getText().charAt(0) !='#'){
    //modal dialog and/ or toast!
} else {
    // ok
}

Upvotes: 1

Related Questions