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);
}
}
Related
This question already has an answer here:
automatically update my activity to show which was written
(1 answer)
Closed 8 years ago.
Using a text field, how would I set the text of a TextView while constantly updating the text? For example: The user begins to type information into the text field, this changes some text in in the activity they're in, however, the user does not need to manually update the text, instead the text automatically refreshes.
I have tried doing this myself and searched for other dilemmas, yet nothing appears to work. Additionally, I'm working with fragments that could possibly cause the problem. The code is below, partitioned into areas, before onCreateView(), in onCreateView() and after onCreateView().
Before onCreateView():
// Edit Text
EditText exerciseOneTitle;
// String value, contains value of exerciseOneTitle
String value;
// titleOne, what I want to the user to be able to change
TextView titleOne;
During onCreateView():
// Edit text
exerciseOneTitle = (EditText) rootView.findViewById(R.id.exercise_text);
// Get Edit Text value
value = exerciseOneTitle.getText().toString();
// Set title so there is always an initial value
titleOne.setText(R.string.exercise_one);
// Checks if edit text is focused
exerciseOneTitle.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
// While edit text is focused, update titleOne via refresh()
while (hasFocus == true) {
refresh();
}
}
});
After onCreateView():
// Get the updated "value" and set it as titleOne text
// Additionaly, will be using it in other situations
public void refresh() {
value = exerciseOneTitle.getText().toString();
titleOne.setText(value);
}
Any thoughts or ideas? Thanks!
You can use TextWatcher.. See the link
According to the docs
onTextChanged() is called to notify you that, within s, the count
characters beginning at start have just replaced old text that had
length before. It is an error to attempt to make changes to s from
this callback.
Try this..
tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
tv.setText(textMessage.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
I want to create EditTextFields dynamically on depending the condition. Condition is that if I start typing on first EditTextField it will create one more EditTextField in the bottom and will create the third EditTextField when i start typing on second one. Similarly i want to delete the bottom text if there is no text in the upper EditTextField. Thanks.
Use a parent view, like a ScrollView that you know you can add a flexible about of content to. Then use a TextWatcher a/k/a a text change listener. You could then create a new text view which you would add to the ScrollView if text was typed into the EditText field.
For neatness I'd probably create a custom TextView class that housed this text change listener and replication check. Here's example of how you could add a TextView
//instance variable
private LinearLayout containerLayout;
private newTextViewCreated = false;
//initialize your conatinerLayout before you use it
//and create your first edit text field
public void onCreate(Bundle savedInstaceState){
containerLayout = (LinearLayout)findViewById(R.id.conatinerLinearLayout);
createEditText();
}
private void createEditText(){
EditText editText = new editText(this);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(count > 0 && !newTextViewCreated){
createEditText();
newTextViewCreated = true;
}
}
#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
}
});
containerLayout.addView(editText);
}
I didn't test this out, I'm writing it now but here's what I'm thinking. Read the description of how a TextWatcher works so you understand the inner methods. You're going to have to play with the conditionals but what you're doing is listening for a change in the number of characters entered and then making a recursive call to create an additional view when chars are added to each text view. I use a boolean flag to show when a view has been created so we don't add one each time the char is changed. I moved outside the createEditText method based on your comment. If you made your own EditText class you could just add a method that would set/get the status of whether this TextView had spanwed another. To remove you would just add a delete condition that would remove the view from the linear layout.
User TextWatcher
Implement your Activity with TextWatcher and override method
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
Show / Hide them in your layout if you know the total amount of editText fields needed or add them programatically like so:
EditText myET = new EditText(MyActivity.this);
myET.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
LayoutContentView.addView(myET);
Then check:
if (myET.getText().toString().trim().equals(""))
{
//Don't Show
}else{
//SHOW
}
SO question could help:https://stackoverflow.com/a/6792359/350421
EditText toAdd = new EditText(this);
list.add(toAdd);
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.
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.
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.