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

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.

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

How to display the user entered text on the same screen

I'm a begineer in android. I learned from the developer.android.com, how to display a text by calling another activity. I want to display the user entered text in the same window. i.e., belolow the text field(center). please anyone help me. I'm a beginner in android and I have just started to learn android.
Use EditText to get user input text
EditText text = (EditText)findViewById(R.id.edittext);
For use of the TextWatcher listen text field
text .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
}
});
use onTextChanged method to use listen to edittext . Then inside onTextChanged start another activity passing that test value
you can display value in to label using .setText() or you can use Toast
write this inside your button OnClick Event Listner
Toast.makeText( MainActivity.this, "Plain Text Input: " + editTextPlainTextInput.getText().toString(), Toast.LENGTH_SHORT).show();
-> MainActivity.this is your context
->editTextPlainTextInput.getText().toString() is value entered by user gettext() gets value and .toString() convert this to string because Toast not accept Object.
-> Toast.LENGTH_SHORT is time that how long toast message appears

Spinner Android (SetPrompt)

I am running into an issue with a spinner that I defined and bound to an array resource. The problem is that it "ONLY" defaults to the first item of the array when it is first constructed. I am using the setPrompt and it looks like it is being ignore totally. I wrote to the log and I can see in the log that I am setting it to the right value but it instead keeps defaulting to the first element in the array.
_spnCountDown.setPrompt(setting);
Log.d("SETTING_SPINNER", setting);
_spnCountDown.setOnItemSelectedListener(new OnItemSelectedListener()
{
boolean _firstTime = true;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3)
{
if (_firstTime == false)
{
String value = _spnCountDown.getSelectedItem().toString();
MobileAppManager.getInstance().storeSetting("CountDown",
value);
Log.d("SETTING_SPINNER onItemSelected", value);
}
else
{
Log.d("SETTING_SPINNER onItemSelected", "Ignore");
_spnCountDown.setPrompt(Settings.this.getInitialCountDown());
_firstTime = false;
}
}
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
I have followed few answers that recommend using a flag to overcome the fact that onSetItemSelected will first the first time the spinner is constructed. So, rightfully so, I am ignoring the first call. However as I mentioned, It is defaulting to first entry.
So, If this line will not do anything _spnCountDown.setPrompt("5 seconds")
I'm not sure but if I understand your question correctly this suggests perhaps you should use setSelection?
Setting default values in spinner in android

How to overwrite value in edittext

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.

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