How to force EditText to start text with capital letter? - android

I have an EditText and I need the text in it (when user types in) to be started with capital letter.

Be careful if you add both android:capitalize="sentences" and android:inputType="text", as the latter seems to have priority over the first and the input will not be capitalized.
There's a specific inputType for automatically capitalizing the first letter:
android:inputType="textCapSentences"
See http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

The options for android:capitalize are
android:inputType="none", which won't automatically capitalize anything.
android:inputType="sentences", Which will capitalize the first word of each sentence.
android:inputType="words", Which Will Capitalize The First Letter Of Every Word.
android:inputType="characters", WHICH WILL CAPITALIZE EVERY CHARACTER.
Apparently it has been changed to inputType instead of capitalize

Use
android:inputType="textPersonName|textCapWords"
as using only "textPersonName" is not enough so name's first letters would be capitalized.
Similarly with postal addresses:
android:inputType="textPostalAddress|textCapSentences"

Try this way,
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
or android:inputType="textCapSentences" will only work If your device keyboard Auto Capitalize Setting enabled.

Add this in your XML
android:inputType="textCapWords"
android:inputType="textCapSentences" will work for sentences. However, I needed to capitalize every word in a full name field.

In the layout xml, add android:inputType=textCapSentences

You used the word "Enforce". So try this. Just pass your edittext as argument.
public static void setCapitalizeTextWatcher(final EditText editText) {
final TextWatcher textWatcher = new TextWatcher() {
int mStart = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
#Override
public void afterTextChanged(Editable s) {
String input = s.toString();
String capitalizedText;
if (input.length() < 1)
capitalizedText = input;
else
capitalizedText = input.substring(0, 1).toUpperCase() + input.substring(1);
if (!capitalizedText.equals(editText.getText().toString())) {
editText.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) {
}
#Override
public void afterTextChanged(Editable s) {
editText.setSelection(mStart);
editText.removeTextChangedListener(this);
}
});
editText.setText(capitalizedText);
}
}
};
editText.addTextChangedListener(textWatcher);
}

In the layout xml, add android:capitalize="sentences"

In case of password which starts from upper case it will be:
android:inputType="textPassword|textCapSentences"

in case you ran into the annoying case of setting android:inputType=textCapSentences then ending up with one-liner EditText, here's the solution:
android:inputType="textCapSentences|textMultiLine"

Paste this in your edittext (xml):
android:capitalize="sentences"

edittext.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

Adding input type in edit text as textCapSentences which will make first letter of the sentence capital
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:padding="10dp"
android:gravity="center"
android:inputType="textCapSentences"
android:hint="Name"/>

Related

android gravity right cursor shows left of the input text EditText

I am using Kotlin. I have used gravity right so that input number start from right. But EditText cursor keep stay always left of the input text in EditText. I want to keep gravity right & text right to left.Here is the image
Here is my EditText xml code..
<EditText
android:id="#+id/et_amount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="numberDecimal"
android:ellipsize="end"
android:gravity="right|center_horizontal|center_vertical"
android:textStyle="bold"
android:maxLength="6" >
<requestFocus />
</EditText>
Please help me so that input text start from left to right & cursor show always right.
Try to add this to your edittext:
android:textDirection="rtl"
try this:
EditText text = new EditText(context);
text.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) {
}
#Override
public void afterTextChanged(Editable s) {
String text1 = text.getText().toString();
text.setText(text1);
text.setSelection(text1.length());
}
});
it will be work;
hope to help you

Space button click puts Auto suggestion to the EditText Android

Please, help me to solve this strange problem. I try to implement EditText, where user can add links to user profiles, hashtags and so on. I have OnTextChanged method where I get current text from EditText, process it and put it to EditText backward.
#OnTextChanged(R.id.share_article_say_something)
public void onTextChanged() {
if (mIsTextChangedManually) {
mIsTextChangedManually = false;
return;
}
mIsTextChangedManually = true;
SpannableStringBuilder builder = mCommentsSpannable.format(mSaySomethingTv.getText().toString());
mSaySomethingTv.setTextKeepState(builder);
}
EditText looks like that
<com.slangwho.views.SWEditText
android:id="#+id/share_article_say_something"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left"
android:textColor="#color/black"
android:textColorHint="#color/my_friend_pressed_color"
android:background="#android:color/transparent"
android:textSize="#dimen/splash_screen_button_text_sp"
android:hint="#string/say_something"
android:layout_margin="10dp"
android:imeOptions="actionDone"
android:textCursorDrawable="#drawable/ed_cursor_drawable_black"
app:fontName="ProximaNova-Regular"/>
This EditText extends regular EditText just to add 1 additional parameter, which is fontName.
When I type some text I receive wrong suggestions(cursor is after single standing "s")
When I typing "Space", I receive:(The First suggestion adds after single standing "s")
It turned out that it was a problem with Samsung Swipe Keyboard. When I set text to the edit text using EditText.setText() or EditText.setTextKeepState() the keyboard suggestions behave strange. The solution was not to set text to the EditText, but set TextWatcher to the EditText and transform Editable in the method afterTextChanged(Editable s).
public class SocialTextWatcher implements TextWatcher {
CommentsSpannable mCommentSpannable;
public SocialTextWatcher(CommentsSpannable commentsSpannable) {
mCommentSpannable = commentsSpannable;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
mCommentSpannable.formatEditable(s);
}
}

Fixed-size multi-line EditText?

I have an EditText that I want to use so people can input a short bio.
So I am trying to make it so it's fixed at, for example, a box that is 4 lines high. I don't want it to "scale" or "shift" with the input -- I'm trying to make it a fixed box of a fixed number of lines with word-wrap.
I did try adding lines="4" and maxLines="4" and inputType="textCapSentences|textMultiLine" but it doesn't quite seem to be right. When I set text, it appears in the middle of the EditText (and not the upper left), and it seems to let me hit enter a whole lot so I can have a word in the first row and then a character like 20 rows down.
Current XML:
<EditText
android:background="#00ff00"
android:id="#+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="4"
android:maxLines="4"
android:inputType="textCapSentences|textMultiLine"
android:gravity="left|top"/>
I'm using a background of green so I can more easily see it for now. Right now this lets you type as much as you want, but I want to limit it to the space as given.
There is in no built code to achieve what you need. But here is a workaround -
private String enteredText;
edtText.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) {
}
#Override
public void afterTextChanged(Editable s) {
if (edtText.getLineCount() > 4) {
edtText.setText(enteredText);
edtText.setSelection(edtText.getText().length()); //This statement is to move the cursor at the end of the text otherwise it'll be moved to the start of the text.
}
else {
enteredText = edtText.getText().toString();
}
}
});
Hope this helps !!
to fix the centered text issue add:
android:gravity="top|left"
To prevent the user from inputting more then 4 lines, you'll need to do it by code.
Add a TextWatcher to the EditText, and check the number of lines after each text-change:
TextWatcher textWatcher = new TextWatcher() {
onTextChanged(CharSequence s, int start, int before, int count) {
// Check number of lines here
}
};
editText.addTextChangedListener(textWatcher);
Just add this attribute to your edittext
android:inputType="textMultiLine"

Android EditText("Textbox") : Auto capitalizing first letter of each word while user typing

I know this question has asked several times but non of the answers worked in my case.
I found some of the same sort of questions and answers in the following links , which didn't work for me at all.
LINK1
LINK2
In my layout file, I have defined my EditText as follows.
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:inputType="textCapWords"
android:ems="10" >
Also in the Activity class in the onCreate method I have added the following code.
EditText testEditText = (EditText) findViewById(R.id.test_editText);
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
If someone have experienced the same issue and solved it would like to hear what I have done wrong in my case. Thanks.
Edits...
Following above explained steps I am unable to make this get it done(Auto capitalizing the first letter of each word while user typing).
According to my code, When user typing it displays first charater in all the words (including first word) in lower case.
android:inputType="textCapWords"
Works for me
I used the same xml code you have in the questions:
<EditText
android:id="#+id/test_editText"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="90dp"
android:ems="10"
android:inputType="textCapWords" >
Just check if you are overriding the inputType attribute in your Activity.
Just try without changing anything in the Activity.
testEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
or android:inputType="textCapSentences"
will only work If your device keyboard Auto Capitalize Setting enabled.
This is what I have done. android:inputType="textCapWords" is not working for me, either.
public static void setCapitalizeTextWatcher(final EditText editText) {
final TextWatcher textWatcher = new TextWatcher() {
int mStart = 0;
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStart = start + count;
}
#Override
public void afterTextChanged(Editable s) {
//Use WordUtils.capitalizeFully if you only want the first letter of each word to be capitalized
String capitalizedText = WordUtils.capitalize(editText.getText().toString());
if (!capitalizedText.equals(editText.getText().toString())) {
editText.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) {
}
#Override
public void afterTextChanged(Editable s) {
editText.setSelection(mStart);
editText.removeTextChangedListener(this);
}
});
editText.setText(capitalizedText);
}
}
};
editText.addTextChangedListener(textWatcher);
}
Earlier it used to be android:capitalize="words", which is now deprecated. The recommended alternative is to use android:inputType="textCapWords"
Please note that this will only work if your device keyboard Auto Capitalize Setting enabled.
To do this programatically, use the following method:
setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
Have you tried android:inputType="textPersonName"
should you try with android:inputType="textCapSentences" ?
Are you trying Emulator or android device?
this works for me in emulator
android:inputType="textCapWords"
Simply add this code to your EditText android:capitalize="words" it must work.
To capitalize first letter of every word use
android:inputType="textCapWords"
Output:
This Is Simple Text.
To capitalize first letter of every sentence then use
android:inputType="textCapSentences"
Output:
This is first statement. This is second statetment.
To capitalize all letter
android:inputType="textCapCharacters"
Output:
THIS IS SIMPLE TEXT
Note: In output the bold letter used just for formatting answer. Above mentioned properties does not force these capitalization. User can enter small case letter.
TRY this on your layout...
android:inputType="textCapWords|textCapSentences"
works fine on me.. hope it works also on you...

How to set the hint properly in a text Box means EditText in android

I am using EditText and providing some Hint there. I am putting it like this:
android:hint="#string/user_name_hint"
android:textColorHint="#ffffff"
android:gravity="left"
But here hint is coming in the left side. But i want the hint is in the middle of the box and when i am clicking the text should start from the left.
How it is possible??
Use this EditText
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="user_name_hint"
android:textColorHint="#fffff"
android:gravity="center_horizontal"
android:id="#+id/editText1"/>
Then you need to add addTextChangedListener to solve your problem.
final EditText hintText = (EditText) findViewById(R.id.editText1);
hintText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
if (s.length() == 0) {
hintText.setGravity(Gravity.CENTER);
} else {
hintText.setGravity(Gravity.LEFT);
}
}
});
set Gravity to center.. android:gravity="center_horizontal"
Not sure you can do it in a "straight forward way". Probably some workaround - either suggested by nagesh or by placing a button on top of the edit text and switching to edit text when it gets clicked or focused

Categories

Resources