¿How to set automatically degree symbol in EditText? - java

I am making an application in android studio where one of the data that I need to capture is the temperature of the person.
As shown in the image, the only thing that I did was place the degree symbol as default in the EditText. But I have to manually move the cursor to place the number before the symbol.
Does anyone know how I can make android place the degrees symbol automatically AFTER typing the temperature data in EditText?

You should be using TextWatcher for edit Text which has a callback function afterTextChanged(Editable s) in which you should put your desired String/char at the end by contacting like s.toString() + your_char
yourEditText.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) {
//here you are setting the data after the entry of temprature
yourEditText.append(" \u00B0");
}
});
Reference

Related

how to use TextWatcher with query in android?

I have made an EditText where I type inside it some name.
I had like the app to perform search every time something changed in my EditText.
I used the following code:
txt_search.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) {
query = txt_search.getText().toString();
fetchBooks(query);
}
#Override
public void afterTextChanged(Editable s) {
query = txt_search.getText().toString();
fetchBooks(query);
}
} );
Where fetchBooks is the method that performs the search inside the API based on the query.
The problem im facing is that sometimes the search is stuck. If for example, I type pretty fast, it gives the results only for the first few letters and not for the whole query in the end.
Eventually, what I'm trying to get is that the app will constantly perform search based on the text inside the EditText.
Is there a way to obtain it without getting stuck results?
Thank you
First:
Remove search operation code for AfterChangedListener because after text is entered in EditText completely your search operation execute on both onTextChanged listener and afterTextChanged Listener
Second Replace txt_search.getText().toString(); with 's.toString()'
Change your TextWatcher as below
txt_search.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) {
query = s.toString();
fetchBooks(query);
}
#Override
public void afterTextChanged(Editable s) {
}
} );
For more information I recommend you to see below links:
getText vs onTextChanged charSequence
android Edittext with textwatcher

is there a way to call onClick method inside a function , what should i pass to the method in the parameters?

i'm working on A project same like the calculator app but with 4 fields , an app to calculate the score for 4 players , also contains a custom numpad (numbers,operator,clear and equal button) , i can't figure out how to apply the buttons methods on the 4 fields(editText)....
i have searched a lot but i could't find anything related
i've tried (onFocusChangeListener , onTouchListener) with edit texts , but the main problem was when i call the buttons methods inside the onFocusChangeListener (which i used to determine which edit text is selected) the button methods require a parameter , and i don't know what should i pass to the method parameter....
...
like so
...
playerTwoScore.setOnFocusChangeListener(new
View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
onClick("here is my problem");
}
}
});
i expect that when i tab on the first edit text i will be able to click on the buttons and do like "1 + 1 = 2"
and when i tab on the second edit text i will able to do the same
you should us TextWatcher
et1.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
}
});

Updating Seekbar after changing Edittext

So I've got a seekbar and an edittext. My aim is to type in a number in the edittext, and in the same time the seekbar should jump to the related position. This should also work vise versa.
I've already got the code for them seperately. But something is missing. Because when I put in both and start then i can slide the seekbar and the value is changing in the same time in the edittext. But when i try to type in a number, it jumps to the beginning. Example: i want to type 123...result is 321. more or less the same with deleting.
I think the two codes are crashing each other, bit i don't know what to change.
And maybe i should say that i'm new in this field.
I'm not sure if my intentions are understandable..
Here my codes:
1.
value.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int after, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try{
//Update Seekbar value after entering a number
int progress = Math.round(Float.parseFloat(s.toString()));
seekbar.setProgress(progress);
value.setText(value.getText().length());
} catch(Exception ex) {}
}
});
2.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
As you see, there is a parameter passed into "onProgressChanged" function called "fromUser":
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
It defines that progress is changed by "physically" shfting the slider, so all You have to do, is just type as below:
if (fromUser) value.setText(String.valueOf(progress));
It prevents doing this kind of loop.
value.setText(value.getText().length());
This line is called first, changing the text.
value.setText(String.valueOf(progress));
Then this line is called, changing the text to something else. These 2 setText()'s happen so fast, you cannot see that it happened twice, you only see the result of the last one.
I propose you remove the value.setText(value.getText().length());, displaying the length of the value string is not something you want to do, if I understand your question correctly.
The problem is in your afterTextChanged() method.
1. Remove line value.setText(value.getText().length()); from afterTextChanged().
value.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence charSequence, int after, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
try {
int progress = Math.round(Float.parseFloat(s.toString()));
seekBar.setProgress(progress);
} catch (Exception e) {}
}
});
2. Update onProgressChanged() method as below, to move cursor position as per text change.
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
value.setText(String.valueOf(progress));
value.setSelection(String.valueOf(progress).length());
}
OUTPUT:
Hope this will help~
value.setText(value.getText().length()); is not using to display the length. It is used to place the cursor position at the end of edittext instead of going it to start position

How to update the same EditText using TextWatcher?

In my Android application I need to implement a TextWatcher interface to implement onTextChanged. The problem I have is, I want to update the same EditText With some extra string. When I try to do this the program terminates.
final EditText ET = (EditText) findViewById(R.id.editText1);
ET.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
try
{
ET.setText("***"+ s.toString());
ET.setSelection(s.length());
}
catch(Exception e)
{
Log.v("State", e.getMessage());
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
#Override
public void afterTextChanged(Editable s)
{
}
});
My program terminates and even I try to catch the exception like in my code still it terminates.
Does anyone have any idea why this happens and how I can achieve this? Thanks.
The content of the TextView is uneditable on the onTextChanged event.
Instead, you need to handle the afterTextChanged event to be able to make changes to the text.
For more thorough explanation see: Android TextWatcher.afterTextChanged vs TextWatcher.onTextChanged
Note: Error onTextChanged
Obvioulsy, you are causing an endless loop by continuously changing the text on afterTextChanged event.
From the ref:
public abstract void afterTextChanged (Editable s)
This method is called to notify you that, somewhere within s, the text has been
changed. It is legitimate to make further changes to s from this
callback, but be careful not to get yourself into an infinite loop,
because any changes you make will cause this method to be called again
recursively. ...
Suggestion 1: if you can, check if the s is already what you want when the event is triggered.
#Override
public void afterTextChanged(Editable s)
{
if( !s.equalsIngoreCase("smth defined previously"))
s = "smth defined previously";
}
Suggestion 2: if you need to do more complex stuff (formatting,
validation) you can maybe use a synchronized method like in this
post.
Note 2 : Formatting the input as partially hidden with n stars till the last 4 chars ( ****four)
You can use something like this in suggestion 1:
#Override
public void afterTextChanged(Editable s)
{
String sText = ET.getText().toString()
if( !isFormatted(sText))
s = format(sText);
}
bool isFormatted(String s)
{
//check if s is already formatted
}
string format(String s)
{
//format s & return
}
To supplement Zortkun's answer (where the example code is quite broken), this is how you'd use afterTextChanged() to update the same EditText:
editText.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) {
}
#Override
public void afterTextChanged(Editable editable) {
if (!editable.toString().startsWith("***")) {
editable.insert(0, "***");
}
}
});
Get familiar with the Editable interface to learn about other operations besides insert().
Note that it's easy to end up in an infinite loop (the changes you do trigger afterTextChanged() again), so typically you'd do your changes inside an if condition, as above.
As afterTextChanged() javadocs say:
It is legitimate to make further changes to s from this callback, but
be careful not to get yourself into an infinite loop, because any
changes you make will cause this method to be called again
recursively.
late answer, if someone looking this is how i did it.
set addTextChangedListener initially
in one of the call back (say onTextChanged()) remove addTextChangedListener
Still interested in receiving updates add it back again.
here is the code.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("log", "before");
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("log", "after");
editText.removeTextChangedListener(this);
ediText.setText("text you wanted to put");
editText.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here is a snippet that worked for me
etPhoneNumber.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) {}
#Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(Utils.getFormattedNumber(s.toString()))) {
s.replace(0, s.length(), Utils.getFormattedNumber(s.toString()));
}
}
});
where Utils.getFormattedPhoneNumber() is your method returning a formatted number

How to restrict Single Quote in android EditText?

friends,
i want to restrict " character in android EditText any one guide me how to achieve this in java code?
You probably want to set an InputFilter on your EditText object
This page has an example of an InputFilter for numeric values
((EditText)findViewById(R.id.EditText01)).addTextChangedListener(new TextWatcher()
{
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
//check here if character is '"' if yes don't allow to right
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
#Override
public void afterTextChanged(Editable s)
{
// TODO Auto-generated method stub
}
});
May this solve it?
How to restrict special characters from an Android EditText field?
use the onKeyDown or onTextChanged event

Categories

Resources