Android EditText - Capitalize first letter of sentences using setText() - android

Folks,
I need to capitalize first letter of every sentence. I followed the solution posted here
First letter capitalization for EditText
It works if I use the keyboard. However, if I use setText() to programatically add text to my EditText, first letter of sentences are not capitalized.
What am I missing? Is there a easy way to fix or do I need to write code to capitalize first letters in my string before setting it to EditText.

The only thing the inputType flag does is suggest to the input method (e.g. keyboard) what the user is attempting to enter. It has nothing to do with the internals of text editing in the EditText view itself, and input methods are not required to support this flag.
If you need to enforce sentence case, you'll need to write a method which does this for you, and run your text through this method before applying it.

You can use substring to make this
private String capSentences( final String text ) {
return text.substring( 0, 1 ).toUpperCase() + text.substring( 1 ).toLowerCase();
}

Setting inputType doesn't affect anything put into the field programmatically. Thankfully, programmatically capitalizing the first letter is pretty easy anyway.
public static String capFirstLetter(String input) {
return input.substring(0,1).toUpperCase() + input.substring(1,input.length());
}

Related

The char "\u200B" is automatically added to the EditText

I am working on an Android Application in which one I would like to compare some string values in EditText.
For example, in a first EditText, I start to entry "dav" and then select "David" from the keyboard suggestions. In a second EditText, I start to entry "dav", then select "David" from the keyboard suggestions and then correct the content to "Dav".
Every seems to be OK. If I retrieve the content of the EditText (with getEditableText().toString().trim()) the debugger tells me that "David" is a word composed by 5 characters and "Dav" a word composed by 3 characters.
If now I click on the EditText that contains "Dav" and I select "David" from the keyboard suggestions, the debugger tells me that the word "David" is composed by 6 characters. The last character is "\u200B".
Why this character is automatically add and how can I remove it in a generic way ?
Thank you for your help.
\u200B is a unicode character zero width space. It seems to me it's being added by the keyboard you are using. I assume if you change your keyboard it's possible you won't see that behavior.
One way to handle that is replacing that character and dealing with the actual String:
#Test
public void zero_space_character() {
String David = "David\u200B";
String theRealDavid = David.replace("\u200B", "");
assertNotEquals(David, theRealDavid);
assertEquals("David", theRealDavid);
}
It should be getText(). toString(). trim().

Android EditText, show both comma and dot as possible separator in case of numberDecimal input type

I need to let user choose between two variants when he inputs decimal number:
use comma(,) as separator
use dot(.) as separator
By default if I use inputType="numberDecimal" in the EditText xml config - EditText shows only digits and comma (,) as possible separator.
I've tried to use android:digits="0123456789, in my EditText config, but without result - EditText widget shows just digits and comma.
I want to have both variants (. and ,) available for user on on-screen keyboard when he tries to input decimal number.
Could you please advise?
Specifying both android:inputType="numberDecimal" and android:digits="0123456789,." will display a keyboard with mostly just numbers and punctuation (depending on the user's chosen keyboard). It will also limit the characters accepted as input to those in your digits attribute.
If you'd like to further limit the keyboard to display only certain characters, you'll need to define a custom one. Here's one of the better tutorials on doing that.
Use proper validation. Let the user see full keyboard but he remain aloof of using it. Means user should not be able to use or input anything using keyboard.
etlocation = (EditText) findViewById(R.id.etlocation);
and used this
etlocation.getText().toString();
if (!isValidLocation(etlocation.getText().toString().trim()))
{
etlocation.setError("Invalid location");
}
validate this
public static boolean isValidLocation(String str) {
boolean isValid = false;
String expression = "^[0-9,.]*$";
CharSequence inputStr = str;
Pattern pattern = Pattern.compile(expression);
Matcher matcher = pattern.matcher(inputStr);
if (matcher.matches()) {
isValid = true;
}
return isValid;
}
You can read this link and also read allow only number and period(.) in edit text in android. Hopefully are helpful from those links. Best of Luck!
You can create a custom keyboard.
The below link shows a nice example of custom keyboard
http://www.mediafire.com/download/39q7of884goa818/myKeyborad2.zip
and check this link also
How to develop a soft keyboard for Android?

The use of input type in EditText

Even if I set the input type to numberdecimal or number, I have to cast the number to get the number. Then what is the use of input type in EditText views.
e.g.
int a = Integer.valueOf(editText.getText().toString());
One more thing, why do I need to use toString() with almost every views to get Text? In java, we could just getText anything from controls.
You have to parse Text to Integer because it doesn't return int. It returns Editable formatted by input type. So if you set input type to numbers you get Editable which contains only numbers.
And you have to add toString() because EditText return Editables not Strings.
InputType is used for various purposes. For example, in a password field, it can hide the characters.
Here's the official description of every single property it can take:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
As for the second part of your question, EditText.getText() returns Editable
This defines a common interface for all text whose content and markup can be changed (as opposed to immutable text like Strings).
So you need to use toString() to get a string out of it.

How To capitalize the letters in AutoCompleteTextView dynamically?

I am developing the application which consists of AutoCompleteTextView,here is my problem,How I can upper case the letters entering in AutoCompleteTextView.
I Don't want in xml: android:capitalize="characters"
I want to declare in Java code.
You can try like this..In your text watcher in ontextchanged change the text to upper case..and check if the new string in edittext is the old string which you converted to upper case...in order to avoid stackoverflow error..
String upper = mytextview.getText().toString().toUpperCase()
mytextview.setText(upper);
Try this in your code there are some other flags also which you can check and try.
tv.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);

How to search and Highlight Text within an EditText

I've searched high and low for something that seems to be a simple task. Forgive me, I am coming to Android from other programming languages and am new to this platform and Java.
What I want to do is create a dialog pop-up where a user enters text to search for and the code would take that text and search for it within all the text in an EditText control and if it's found, highlight it.
I've done this before, for example in VB and it went something similar to this pseudo code:
grab the text from the (EditText) assign it to a string
search the length of that string (character by character) for the substring, if it's found return the position (index) of the substring within the string.
if found, start the (EditText).setSelection highlight beginning on the returned position for the length of
Does this make sense?
I just want to search a EditText for and when found, scroll to it and it'll be highlighted. Maybe there's something in Android/Java equivalent to what I need here?
Any help / pointers would be greatly appreciated
grab the text from the (EditText) assign it to a string
Try the code sample below:
EditText inputUsername = (EditText) findViewById(R.id.manageAccountInputUsername);
inputUsername.getText().toString()
^^ Replace the IDs with the IDs you are using.
After this, you have the standard string methods available. You could also use Regex for a complex search query:
http://developer.android.com/reference/java/lang/String.html
http://developer.android.com/reference/java/util/regex/package-summary.html

Categories

Resources