How to overwrite value in edittext - android

I have an edittext in my app, which accepts only numbers. It is set to 0 by default. But when i enters some umbers, i want to overwrite the initial 0 in the left. Now i am having some problem with edittext. For example, when i inserts a 1, i want it to how 1, not 01. my code is like,
value = (EditText)findViewById(R.id.value);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
if(arg0.length()==0)
{
value.setText("0");
int i = value.getText().length();
value.setSelection(i);
}
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
How can i achieve it? or is it possible?

You can set android:hint in your xml file.
For example,
<EditText
android:id="#+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="0"/>

There are lots of options to accomplish what you're looking for. Besides the ones already mentioned, I'd say the EditText's attribute android:selectAllOnFocus="true" might be worth looking at. It's convenient for a default value, and at the same quite user friendly as changing the value does not require any 'backspacing'.

You can change string to empty in your beforeTextChanged Function Call and let the onTextChanged as it is.

Instead of using value.setText("0");
use value.setHint("0");

Use setHint(int) method to display 0 initially. So that when the user types something it is automatically cleared and you can get the text as desired.

Related

Applying auto-calculation to edit text

If the user enters any value like 1234 in the edit text box, then all the values of the edit text box are Addition in each other and the answer is shown in a text box.
For example if user enter values such as 1234 in edit text and after addition (1+2+3+4=10) their answer that is 10 show in text box.
You could probably use data binding, either one or two way depending on how you want to do things.
Make sure to read the documentation around data-binding before starting to successfully implement!
Using one-way data binding, you can set a value on an attribute and set a listener that reacts to a change in that attribute - android docs
First set your binding adapters on your new SumNumberTextView,
Then in your parent view's layout file, if you want to use the more verbose one way data binding, you'd reference something like:
android:numbers="#{viewmodel.numbers}"
and
android:onNumbersChanged="#{() => viewmodel.onNumbersChanged()}"
Then you can define a function on your view model that does your numeric operations whenever you input numbers.
You can just use textwatcher on edittext to get update when text change and do your operation of addition just like this
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// call function from here if you want to perform operation as user provide input
add(cs.toString());
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
Toast.makeText(getApplicationContext(),"before text change",Toast.LENGTH_LONG).show();
}
#Override
public void afterTextChanged(Editable arg0) {
// call function from here if you want to perform operation only user stop input
add(arg0.getText().toString());
}
});
private void add(String input){
if(input != null && input != ""){
String[] numbers = input.split("")
int total = 0;
for(int counter = 0 ; counter < numbers.length ; counter++){
total += Integer.parseInt(numbers[counter]);
}
Log.e("total",total);
}
}

Specify addTextChangedListener in XML

Is there any property for EditText I can specify TextChangedListener? I know it has 3 methods, so I want just specify value of property like below
<EditText
android:afterTextChanged="myAfterHandler"
However I am getting compilation errors. I can add on click listener in same way so I am wondering what is wrong on text changed?
I know I can obtain view at certain time in Java code and call add listener, but this solution looks really ugly.
Android's Data Binding Library now supports generating xml attributes for setters that exist in the view's class.
If you have data binding enabled, all you need to do is use the app namespace (don't forget to declare the namespace), so something like app:addTextChangedListener="#{someobject.textwatcherfield}"
For use of the TextWatcher..
edittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
There is no such property of TextChangedListener that you can specify in xml file.
You have to add textchangedListener to your edittext in Java class as described in another answer to this post.
You can just give onclick property to your views in xml file.
You can ask if you have any further queries :)

EditText in multi-color

I am working on an Android application.
Now I want take input from user into an EditText.
I want display the text after the last fullstop in black color and text before last full stop in red color.
For example, If user types below sentence in EditText:
'my name is john.I am from India.I Love Android'
I want to show the 'I love Android ' in black and first parts of the sentence in red.
Is there any way to do that?
Use SpannableString to apply attributes to your text. Here blog entry you may want to read: http://www.chrisumbel.com/article/android_textview_rich_text_spannablestring.
Listen to changes in text by:
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (!colorHasSet) {
makeColorText();
}
colorHasSet = false;
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {}
});
then declare a function to colorize text by using the tutorial which WebnetMobile linked to.
public void makeColorText() {
SpannableString ss = new SpannableString(textEdit.getText());
// customize ss here
// ...
colorHasSet = true;
editText.setText(ss);
}
flag boolean variable colorHasSet should be defined to prevent stackOverflowException.
this is not a complete WYSIWYG editor with instant colored text, and you should do some hacks to make it complete and suitable to your needs, that is left to be done by yourself.

how to set a default value to EditField when it is cleared?

I am using an EditText field in my application. also i had added addTextChangedListener for that. On the onTextChanged method, i had called my squareFunction. The code is like..
value = (EditText)findViewById(R.id.amount);
value.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
{
squareFunction();
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
Now my problem:= i am getting the value of input numbers correctly. But when i clear the EditField, an error comes and ask me to "Force Close". I want to make my editField value to 0 if all the characters in it are cleared. Is it possible? If yes, how it can be done. Plz explain with code if possible...
inside onTextChanged()
if( arg0.length()==0)
{
value.setText("");
}
i hope you tried this basic thing , so what errorlog says at the time of force close ??
You should try adding condition like
if(value.getText().toString().equalsIgnoreCase(""))
{
Log.i(TAG,"EditText Clear");
//set value to 0
}
else
{
squareFunction();
}
also use this in afterTextChanged
The error is due to the function trying to perform square when you r clearing the values. Move the square function to afterTextChanged, and in onTextChange, check the length of the edittext, if it is zero, set text to 0.

Android: Limiting EditText to alphanumerics in any language

I want to allow users to create a username using only alphanumeric characters but where the alpha characters can be any of the characters in the user's native language. It must be possible to restrict the input to only those characters that are part of the alphabet of the native language or if there is no alphabet in a language (like Chinese), then limited to those characters that would normally be considered non-symbolic (symbolic characters being a question mark, colon, etc).
Using inputType seems to pose a problem because setting it to "text" actually allows the keyboard to display symbols as well.
EditText editText = (EditText)findViewById(R.id.your_edit_text);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
for(int i=0;i<arg0.toString().length();i++){
if(arg0.toString().charAt(i)=='a char you hate')
//Show an error and change the contents of arg0
}
}
});
EDITED
I have edited the answer a little bit because In the previous version assumed that he changed the last character. However this is not true onTextChanged is called even if he inserted a character in the middle of the String.

Categories

Resources