Android EditText inserting - android

How do I insert characters into the middle of an EditText field?
I'm making a calculator that can take a string expression like "3*(10^2-8)". I'm using an EditText field to make the string using XML like so:
EditText
android:id="#+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/label"
android:text="#string/testString1"
android:background="#android:drawable/editbox_background"
and then in my activity I have, say:
entry = (EditText)findViewById(R.id.entry);
entry.setText("blablahblah");
entry.setSelection(3);
Now I have an EditText field with the cursor blinking after the third character in the string. How do I insert a character there, so it correctly says "blahblahblah"?

The method getText() of the EditText widget returns an object that implements the Editable interface. On this object you can call the insert() method to insert text at a certain position.
I found this out by reading the documentation, but never used this myself. But for your needs, to insert a character at the selected position in the EditText, the following should work:
Editable text = entry.getText();
text.insert(entry.getSelectionStart(), "h");

Let's say you have a String called str and it contains "blablahblah" and you want to make it "blahblahblah" you can do the following:
String newString = str.substring(0, 3) + "h" + str.substring(3);
Take the first 3, add the new letter, place everything else. So you could take the String from the EditText, change it like this and then put the new String as the new value of the EditText.

Related

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

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());
}

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 validate edittext with integers in android?

I want to have an edittext that only accepts numbers as input and when a button is clicked I want to check that the edittext has got a number inside and is not empty. Thanks.
try this in layout.xml
<EditText android:numeric="integer" ..../>
in Code
EditText mNumber = (EditText)findViewById(R.id....);
onButtonClick
if(mNumber.getText().toString().length()>0)
//logic
else
//empty Editext
Call EditText.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
This will make sure that numeric virtual keypad will appear, and special filtering will be applied to only allow only numbers to be entered.
To check empty text, call
EditText.getText().length() != 0
Instead of validating you can add a (inpuType = "number") attribute in your xml file (Under the editText) after which you'll be able to add only numbers in your editText.

Editable text to string

How can I convert editable text into string in Android ? Any solution?
If I understand correctly, you want to get the String of an Editable object, right? If yes, try using toString().
Based on this code (which you provided in response to Alex's answer):
Editable newTxt=(Editable)userName1.getText();
String newString = newTxt.toString();
It looks like you're trying to get the text out of a TextView or EditText. If that's the case then this should work:
String newString = userName1.getText().toString();
This code work correctly only when u put into button click because at that time user put values into editable text and then when user clicks button it fetch the data and convert into string
EditText dob=(EditText)findviewbyid(R.id.edit_id);
String str=dob.getText().toString();

Categories

Resources