user input run time android - java

I am trying to build a simple converter code(convert feet to mtr) in Android. I am able to do this but only when user clicks some button. Now I want to modify it such that it starts to convert as and when the user gives input(Something like google converter). Is there any way to do this in Android?
Thanks in advance.

Add the listener to your edittext:
yourEditText.addTextChangedListener(addTextWatcher);
Add the TextWatcher interface:
private TextWatcher addTextWatcher = new TextWatcher() {
#Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
// here is where you could grab the contents of the edittext
// input each time a character is entered, and pass the value
// off to your unit conversion code. Careful to check for
// numerals/decimals only, or to set the proper inputType in
// your xml.
}
#Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable sequence) {
}
};

Related

Android Studio : Converting numbers in real time

For AndroidStudio, I am creating a simple conversion app that allows you to convert kilometres to miles and vice versa. I am able to convert it by clicking a button, but how would you do it in real time? For example, as I am typing the number, it converts it right away in a different textbox.
This is the code for my onClick method that I created for my button:
public void onClick(View vw){
EditText value = (EditText)findViewById(R.id.editText);
double total = Double.parseDouble(value.getText().toString()) * 0.621371192;
TextView obj = (TextView)findViewById(R.id.milesDisplay);
obj.setText(Double.toString(total));
obj.setVisibility(View.VISIBLE);
}
You can do this if you're using an EditText:
mEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Every time you hit a number, capture the number and convert it
}
#Override
public void afterTextChanged(Editable s) {
}
});
inside onTextChanged a listener is trigged every time you hit the keyboard. Force it to only accept numbers.

MultiAutocompleteTextView events

I'm trying to work with a MultiAutocompleteTextView in such way that whenever I type a character in the textview, an event will be fired OR after 2-3 elements have been typed, fire the event again.
The reason I work with multiautocomplete is because I also need the autocompletion feature .
Is there such an event that can be triggered after every character or 2-3 characters typed? Thanks!
Yes you can use add a TextWatcher and TextChangedListener to your edit Text like this:
myEditText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//check count or count in edit text and do something
}
#Override
public void afterTextChanged(Editable arg0) {
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
});
http://developer.android.com/reference/android/text/TextWatcher.html

Android TextWather object. How java anonymous class is working ?

private TextWatcher billEditTextWatcher = new TextWatcher()
{
// called when the user enters a number
#Override
public void onTextChanged(CharSequence s, int start,
int before, int count)
{
// convert billEditText's text to a double
try
{
currentBillTotal = Double.parseDouble(s.toString());
} // end try
catch (NumberFormatException e)
{
currentBillTotal = 0.0; // default if an exception occurs
} // end catch
// update the standard and custom tip EditTexts
updateStandard(); // update the 10, 15 and 20% EditTexts
updateCustom(); // update the custom tip EditTexts
} // end method onTextChanged
#Override
public void afterTextChanged(Editable s)
{
} // end method afterTextChanged
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
} // end method beforeTextChanged
}; // end billEditTextWatcher
This is a segment of a code from a tip calculator app written by a professional. Can someone explain how this works?
Usually I just write the following to create a new object.
TextWatcher billEditTextWatcher = new TextWatcher();
I understand what the private does. But how come there are methods in the creation of the new object? Is it basically doing what it says? overriding the original methods in the TextWatcher class?
I hope this question makes sense because I'm quiet confused.
Thanks in advance!
This is an example of an anonymous class in Java. You don't have any TextWatcher java file, and you declare the content of the class while initializing it.
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
As name is indicating TextWatcher will sense all event of EditText
Like when you are writing something in Editable area.
Its have following callback(Watching state.)
Key Pressed - beforeTextChanged();
Key Down - afterTextChanged();
Text Change - onTextChanged();
All the callback method contain their relative data which passed by event generator. like which is key is pressed , Unicode(ASCII) of Key etc.
Basically, a TextWatcher is used to keep watch on EditText or MultiLine EditText while entering data into it. We can perform operation and keep watch on which characters are being entered or how many characters are being entered in the EditText.
Technical Description:
If you want to use TextWatcher then you need to resister your EditText with TextWather object.
e.g.
EditText editTextPassword; // Some EditText object.
TextWatcher billEditTextWatcher = new TextWatcher(); // TextWather object creation
editTextPassword.addTextChangedListener(billEditTextWatcher ); // EditText registation with Textwather object.
By default all call back of TextWather are empty that's that why you need yo give your definition of all callback according to your requirement.
private TextWatcher billEditTextWatcher = new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// you code is here while onTextChanged.
}
#Override
public void afterTextChanged(Editable s)
{
you code is here while afterTextChanged.
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after)
{
// you code is here while beforeTextChanged.
}
};

KeyReleased equivalence in Android

On PC I can add a onKeyListener for a JTextField to listen keyReleased event. On Android I've used addTextChangedListener.
I have two EditText fields in my Android application. Editing one will affect the other. This will cause the program to fail in stack overflow error.
How can I listen for the phone's keyboard instead of changes in the EditText field? I don't want the program to invoke the listener because of the infinite loop caused by the listener.
Attach a onFocusChangedListener and add the TextChangedListener when a EditText has focus and remove it when it loses focus.
Something like this:
EditText1.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
((EditText) v).addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
//
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
//
}
public void afterTextChanged(Editable s) {
// affect EditText2
}
});
}
if(!hasFocus){
((EditText) v).removeTextChangedListener();
}
}
});
}
});
The same for EditText2
First of all, I would create one text change listener, something like SynchronizingWatcher and attach it to both EditTexts. Then, when you receive a text change event, before updating other text edits, just unregister old listeners, update text and enable listeners again:
class SynchronizingWatcher implements TextWatcher {
Set<EditText> synchronizedViews = new HashSet<EditText>();
public void watchView(EditText view) {
view.addTextChangedListener(this);
synchronizedViews.add(view);
}
public void afterTextChanged(Editable s) {
for (EditText editText : synchronizedViews) {
editText.removeTextChangeListener(this);
editText.setText(s); // Of course you can do something more complicated here.
editText.addTextChangeListener(this);
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Don't care.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Don't care.
}
}
...
// Somewhere in your activity:
SyncrhonizingWatcher synchronizingWatcher = new SynchronizingWatcher();
synchronizingWatcher.watchView(myEditText1);
synchronizingWatcher.watchView(myEditText1);
Another solution: provide your own KeyListener that decorates existing KeyListener (you can get existing key listener with editText.getKeyListener() and set your decorator with editText.setKeyListener(). Your decorator would also update other edit texts in onKeyUp(). But I would try to stay away from messing with that stuff.

Is there a built in method, InputType, or other clever way to cause an Android EditText widget to refuse to accept commas?

I am creating a comma seperated file and don't want to give the user a way to confuse the app.
Following what RoToRa said, you can delimitate the file using tabs instead.
If you do want to disallow commas, you can add a TextWatcher to modify the string before it is posted to the GUI:
EditText text;
private void foo()
{
text.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
public void afterTextChanged(Editable s)
{
// modify string here
}
});
}
Just check the addTextChangedListener(TextWatcher watcher) method - add the listener, which will check the editText field when it changes.

Categories