I would like to have an EditText like that :
With the currency (on the left or on the right, it depend of the local currency).
When the user edit the EditText, the "box" have to be only with the decimal (15), and after editing, It would be great to see "$15" or "15€".
I try a lot of way but to do that nothing is working.
This code works fine!!
//metFirstName is a edittext variable name
metFirstName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (!hasFocus) {
metFirstName.setText(metFirstName.getText().toString()+"your special char");
}
}
});
Related
I want to know is there any chance to get our result in an edittext not in textview without using any button. That is when we place cursor on the edittext field, the result we want to print should be in the field. Is there anyone who could help vth this.?
This is untested but it should work I think
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
editText.setText("whatever")
}
}
});
Basically, I am validating a field when the the field loses focus. When an error occurs when we check at the OnFocusChangeListener, I want it to cancel the focus and stay on the same field. Is there a way to cancel the focus on it?
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
if(checkError(v))
{
// Cancel focus here
}
}
}
I think requestFocus() should do the trick, see this answer for usage: https://stackoverflow.com/a/8077842/1173391
Update:
I think you are missing the check for the view id, because the onFocusChange method is implemented via interface in your class:
try setting this:
if (v.getId() == myEditText.id)
as first check on the focus change method; you have to do it for every edit-field (or use a switch instead of if) ....
OR
attach an OnFocusChangeListener to every single field:
edittext1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
}
});
i want to enter a value to a text field and once i enter i want it to view something on a text view automatically.. how to do that in android.??
this is what i tried.
txt.setKeyListener(new KeyListener() {
#Override
public boolean onKeyUp(View v, Editable e, int i, KeyEvent k) {
// TODO Auto-generated method stub
if(i==40)
{
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText("Done");
}
return false;
}
Take your input value in an EditText. Compare your EditText's value and accordingly change your TextView. I don't see any use of onKeyUp method.
Or if you really want to use onKeyUp method, I guess this link might help you!
I am trying to validate my edittext field after focus is changed but it gives when the time of typing only.i.e, after entering one charecter I am used below code
fname.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s){//some validations}
But my requirement, in my form so many fields are there if we go for validation submition time its a risky one. so please help me.
Add OnFocusChangeListener to your edit text
fname.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// If view having focus.
} else {
// If view not having focus. You can validate here
}
}
});
I have a EditText in my app which is supposed to take only numeric values; this is why I set the inputType property this way:
android:inputType="number|numberSigned|numberDecimal"
It works properly, except when the user presses enter, because it losts focus; is there any property to set to avoid this? Thank you for your answers.
What do you want to do? Keep the focus?
You can implement this method and check the value... if the value is not what you expect, you put back the focus.
public void onFocusChange(View v, boolean hasFocus) {
// ...
}
Regarding your comment:
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus && (v instanceof EditText)) {
((EditText) v).requestFocus();
}