How to Capitalize the First Letter of Text in a Textview in an Android Application

How to capitalize the first letter of text in a TextView in an Android Application

I should be able to accomplish this through standard java string manipulation, nothing Android or TextView specific.

Something like:

String upperString = myString.substring(0, 1).toUpperCase() + myString.substring(1).toLowerCase();

Although there are probably a million ways to accomplish this. See String documentation.

EDITED
I added the .toLowerCase()

Capitalize the first letter in Android TextView using XML

I'm pretty sure that you can't achieve that behaviour using only XML files, but if you extend the TextView class to a custom one that capitalises the first letter, then you should be able to use only XML from this point on.

public class CapitalizeTextView extends TextView {
// Create here constructors matching super

@Override
public void setText(CharSequence text, BufferType type) {
StringBuilder builder = new StringBuilder(text);
builder.setCharAt(0, Character.toUpperCase(builder.charAt(0)));
super.setText(builder.toString(), type);
}
}

From this point on, you only have to use CapitalizeTextView instead of TextView and the first letter will be capitalised. It works from XML or Java/Kotlin.

<mobile.com.capitalize.CapitalizeTextView
......
android:text="not capital letter" />


textView.setText("not capital letter");

First letter capitalization for EditText

Statically (i.e. in your layout XML file): set android:inputType="textCapSentences" on your EditText.

Programmatically: you have to include InputType.TYPE_CLASS_TEXT in the InputType of the EditText, e.g.

EditText editor = new EditText(this); 
editor.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);

Can be combined with text and its variations to request capitalization of the first character of every sentence.

- Google Docs

Capitalize and enlarge the first letter of TextView in android

Sample Image

100% work

Steps!

1) Use this library

             compile 'com.novoda:drop-cap:1.1.0'

2) Define it in your layout

    <com.novoda.dropcap.DropCapView
android:id="@+id/view_drop_cap"
style="@style/DropCap"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/button1_detail" />

3) Define it into your styles.xml

<style name="DropCap">
<item name="android:paddingLeft">@dimen/drop_cap_padding_left</item>
<item name="android:paddingTop">@dimen/drop_cap_padding_top</item>
<item name="android:paddingRight">@dimen/drop_cap_padding_right</item>
<item name="android:paddingBottom">@dimen/drop_cap_padding_bottom</item>
<item name="dropCapTextSize">@dimen/drop_cap_text</item>
<item name="numberOfDropCaps">1</item>
<item name="dropCapFontPath">fonts/SANS-SERIF_Cabin-Regular.otf</item>
<item name="copyTextSize">@dimen/copy_text</item>
<item name="copyFontPath">fonts/neuropolitical_rg.ttf</item>
<item name="lineSpacingExtra">@dimen/drop_cap_linespacing_extra</item>
</style>

4)Define it into your dimens.xml

<dimen name="drop_cap_padding_left">10dp</dimen>
<dimen name="drop_cap_padding_top">10dp</dimen>
<dimen name="drop_cap_padding_right">10dp</dimen>
<dimen name="drop_cap_padding_bottom">10dp</dimen>
<dimen name="drop_cap_text">64sp</dimen>
<dimen name="copy_text">21sp</dimen>
<dimen name="scroll_view_height">200dp</dimen>
<dimen name="divider_height">1dp</dimen>
<dimen name="drop_cap_linespacing_extra">0sp</dimen>

4) In your java code just findItById and set Text.

Reference-> https://github.com/novoda/drop-cap

How to capitalize first letter of edit text in android

Check these solution:

edittext.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable et) {
String s=et.toString();
if((!s.equals(s.toUpperCase()) && s.length()==1)) {
s=s.toUpperCase();
edittext.setText(s);
}
}
});


Related Topics



Leave a reply



Submit