Add dollar sign to editText when field is clicked android - java

I would like to add a dollar sign ($) automatically when the editText field is clicked. I've attempted to use addTextChangedListener but it doesn't work as expected.
I do not want to add it after the use has finishes entering all the text. Just want it to be added once the field is clicked or becomes the focus.
EditText editTextAd;
editTextAd.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) {
editTextAd.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL){
keyDel = true;
}else{
keyDel = false;
}
return false;
}
});
if (!keyDel) {
String str = s.toString();
if (s.length() == 0) {
str += "$";
editTextAd.setText(str);
editTextAd.setSelection(str.length());
//keyDel=false;
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});

setOnKeyListener is listening keys from Keyboard(hardware).
Change EditText.text in onTextChanged will cause a never-end loop which actually won't work.
when the editText field is clicked
The event is according to setOnClickListener or setOnTouchListener.
becomes the focus
The event is setOnFocusChangeListener.

Step - 1: First call edittext.setOnfocusChangeListner so that on user click on your editText it detect its focus.
Step - 2: Call edittext.setText("$"); which set $ sign in first position on editText.
Step - 3: Call editText.setSelection(1) so that your cursor will start at position 1 not 0.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus){
editText.setText("$");
editText.setSelection(1);
}
}
});

Related

Enter double space to replace comma in multiautocompletetextview

I want to add comma when I enter double space from keyboard in multiautocompletetextview. I search lots of thing in google. But can't reach my goal. I want to replace comma for double space entering by user.
So obviously, I must have to write something logic in ontextChange() or OnAfterTextChanged() in addtextwatcher listener.but i do't got event of after add 2 space.
I have already used comma tokenizer when select word from list.but i want to add comma when user entering double space using keypad.
Thanks in advance
The simplest solution i can provide you is to use String.replace(), Here is small code snippet to help you
#Override
protected void onCreate(Bundle savedInstanceState) {
...
edt.addTextChangedListener(textWatcher);
}
And TextWatcher which you are going to set on EditText
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
edt.removeTextChangedListener(textWatcher);
String text = edt.getText().toString();
text = text.replace(" ", ",");
edt.setText(text);
edt.setSelection(text.length());
edt.addTextChangedListener(textWatcher);
}
#Override
public void afterTextChanged(Editable editable) {
}
};
Try like this, I have not tried this code
boolean userPressedKey = false ;
int spaceCount = 0;
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
userPressedKey = false ;
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
userPressedKey = true;
});
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (userPressedKey) {
if (keyCode == KeyEvent.KEYCODE_SPACE) {
spaceCount ++;
if(spaceCount == 2){
//append comma to the edittext here
Toast.makeText(MainActivity.this, "White space is clicked twice", Toast.LENGTH_LONG).show();
}
return true;
}else{
spaceCount=0;
}
}
super.onKeyDown(keyCode, event);
}

Rating bar, how to get the number of stars?

I got an edit text and also a rating bar in my program. I am checking if the user has more than 3 characters in the edit text and for rating bar it should have something greater than 0.5 star (basically to check if they at least clicked on any of them).
final EditText commentbox = (EditText)findViewById(R.id.comment);
final RatingBar rating = (RatingBar)findViewById(R.id.rating);
final Button feedbackbutton = (Button)findViewById(R.id.submit);
final TextWatcher watcher = 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(commentbox.length() >= 3 && rating.getNumStars() >=0.5){
feedbackbutton.setEnabled(true);
feedbackbutton.setBackgroundColor(Color.parseColor("#369742"));
} else {
feedbackbutton.setEnabled(false);
feedbackbutton.setBackgroundColor(Color.parseColor("#ffcacaca"));
}
}
};
commentbox.addTextChangedListener(watcher);
rating.setOnRatingBarChangeListener((this));
So as you can see it has to meet those condition in order for a button to become enabled and also to change its background colour. However soon as I write more than 3 characters in the edit text, the button enables itself and changes its colour. It's not looking for the rating bar condition.
Can someone help me please
To get rating from rating value:
float ratingValue = rating.getRating();
Add listener :
rating.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
#Override
public void onRatingChanged(RatingBar arg0, float rateValue, boolean arg2) {
// TODO Auto-generated method stub
Log.d("Rating", "your selected value is :"+rateValue);
}
});
feedbackbutton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//Getting the rating and displaying it on the toast
String rating=String.valueOf(rating.getRating());
Toast.makeText(getApplicationContext(), rating, Toast.LENGTH_LONG).show();
}
});
Hope it solves your problem.

dynamically change the length of String inside the EditText field in Android

I am using an EditText field in my application.
EditText is for entering the ddns input:
e.g www.example.com/xxxx
I want to restrict the length of the ddns id to 30 characters after "/" character.
i.e after "/" character, what follows must be of maximum 30 characters
I want to do it dynamically and restrict user to not type more than 30 characters.
How can i do it.
You can try the below way to restrict the user to enter less than 30 character.
tf.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//processing part
}
});
A very fast and ugly answer would look something like this:
yourEditText.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) {
if(s.toString().contains('/') {
if(s.toString().split('/')[1].length() == 30) {
//By only working with the EditText:
InputMethodManager imm = (InputMethodManager)
getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
yourEditText.setFocusable(false);
yoruEditText.setEnabled(false);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}});
//What i think is the best implementation, adding a TextView sitting on top of
//EditText with visibility set to GONE
yourEditText.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) {
if(s.toString().contains('/') {
if(s.toString().split('/')[1].length() == 30) {
//By working with EditText and a TextView
yourEditText.setVisibility(View.GONE);
yourTextView.setVisibility(View.VISIBLE);
yourTextView.setText(s);
}
}
}
#Override
public void afterTextChanged(Editable s) {
}});
In any case you want to add a textWatcher to your editText and change it accordingly.

Android. How to catch the moment when keyboard gets hidden?

I'm trying to catch the event of removing keyboard from the screen and i'm using the following code:
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
System.out.println("HAS FOCUS");
else
System.out.println("FOCUS LOST");
}
});
But unfortunately it doesn't work. onEditorAction just never called, no matter if i start editing or finish. Regarding onFocusChange method it's called just for the very first time when the keyboard goes up. When the keyboard goes down or when it goes up for the second time it's not called. Can anyone explain what am i doing wrong?
I used GlobalLayoutListener on the activity rootView for checking that whether keyboard is hidden or visible:
It works as follows:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});

Change button behaviour according to user input

I'm writing a simple app in Android.
I've encountered this problem: I've two EditText and a Button. The value of one EditText must be a multiple of the other one EditText.
When the user insert a value in the first EditText and then press the button, the other EditText should show the value calculated with the user input.
This should be possible in other verse, too.
Like a simple unit converter. When I insert value1 in EditText1 and press convert the app must show the converted value in EditText2, but if I insert a value2 in EditText2 and press convert button the app must show the converted value in EditText1.
My problem is: how can I recognize in which EditText there are last user-input?
public void convert(View view) {
EditText textInEuro = (EditText) findViewById(R.id.euroNumber);
EditText textInDollar = (EditText) findViewById(R.id.dollarNumber);
if (toDollar) {
String valueInEuro = textInEuro.getText().toString();
float numberInEuro = Float.parseFloat(valueInEuro);
// Here the conversione between the two currents
float convertedToDollar = unit * numberInEuro;
// set the relative value in dollars
textInDollar.setText(Float.toString(convertedToDollar));
}
if (toEuro) {
String valueInDollar = textInDollar.getText().toString();
float numberInDollar = Float.parseFloat(valueInDollar);
//Here the conversione between the two currents
float convertedToEuro = numberInDollar / unit;
//set the relative value in dollars
textInEuro.setText(Float.toString(convertedToEuro));
}
}
This is the code written. I've thinked to use OnClickListener..but it isn't a good idea..
You can add a TextWatcher to your two EditText in order to know which one has been updated last.
public class MainActivity extends Activity {
EditText dollar;
EditText euro;
private static final int EURO = 0;
private static final int DOLLAR = 1;
private int lastUpdated = DOLLAR;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dollar = findViewById(R.id.dollar);
euro = findViewById(R.id.euro);
dollar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lastUpdated = DOLLAR;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
euro.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
lastUpdated = EURO;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
public void convert(View view) {
switch (lastUpdated) {
case EURO:
//Do work for euro to dollar
break;
case DOLLAR:
//Do work for dollar to euro
break;
default:
break;
}
}
}
I have a different way in mind,you can try this to achieve what you want:
when user comes first on activity,let him edit any edit text fields.(one is being filled,the other will be disabled until he pressed button)
let him click button
once the results are filled and he wants to edit one of the edittext,other edittext would be automatically empty
lets say you have editText_1 and editText_2,and your button is button_1.Also take a boolean variable called convertBoolean and make it false as default.
Now,
editText_1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(convertBoolean==false){
editText_2.setEnabled(false); //disable other edittext when one is being edited
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
#Override
public void afterTextChanged(Editable s) {
String s1=editText_1.getText().toString().trim();
String s2=editText_2.getText().toString().trim();
if(!s1.equals("") && !s2.equals("") && convertBoolean==true){
//of both are filled, empty second edittext
editText_2.setText("");
convertBoolean=false;
}
if(editText_1.getText().toString().trim().equals("") && convertBoolean==false){
editText_2.setEnabled(true);
}
}
});
editText_2.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(convertBoolean==false){
editText_1.setEnabled(false); //disable other edittext when one is being edited
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
#Override
public void afterTextChanged(Editable s) {
String s1=editText_1.getText().toString().trim();
String s2=editText_2.getText().toString().trim();
if(!s1.equals("") && !s2.equals("") && convertBoolean==true){
//of both are filled, empty first edittext
editText_1.setText("");
convertBoolean=false;
}
if(editText_2.getText().toString().trim().equals("") && convertBoolean==false){
editText_1.setEnabled(true);
}
}
});
button_1.setOnClickListener(new OnClickListener(){
public void onClick(){
convertBoolean=true;
editText_1.setEnabled(true);
editText_2.setEnabled(true);
}
});

Categories

Resources