how make visible and invisible Drawable Left on EditTextView? [duplicate] - java

This question already has answers here:
How to remove drawableleft
(6 answers)
Closed 4 years ago.
I have read many answers in Stack but none of them can't help me, so please don't mark it as Duplicate ,I have EditText, and I want to make visible drawable left when the length of inserted data in EditTextView become equals to 11.

If you want to visible/invisible drawable left on user typing then do like this:
EditText et = (EditText)view.findViewById(R.id.edt);
et.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable editable) {
if(editable.length() >= 11){
// visible
et.setCompoundDrawablesWithIntrinsicBounds(R.drawable.your_icon, 0, 0, 0);
}
else {
// hide
et.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
}
}
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
});

You can use the setCompoundDrawables method to do this:
Drawable img = getContext().getResources().getDrawable(R.drawable.add_more);
img.setBounds(0, 0, 60, 60);
edittext.setCompoundDrawables(img, null, null, null);
if (edittext.lenght == 11) {
img.setVisible(false, false);
}
I hope it will help you!

Related

Progreess Bar tracking multiple TextField states

I'd like to have a progress bar that tracks a user's form progress. For example:
TextFieldName: _______
TextFieldPhone: ________
With both fields empty, the progress bar should read 0%. If the user has filled out one of either their name or phone, the progress bar should update to 50%. If the user has filled out both name and phone, the progress bar should read 100%.
I have tried to approach the problem, but I can't deal with the fact that there should be some sort of global variable that's stored somewhere that can be grabbed at any time between edits (also not sure where that would be as I am new to this). Here is my code:
binding.progressBarNewRelease.setProgress(0);
float numInputs = 20;
float increment = (1 / numInputs) * 100;
binding.textInputEditTrackName.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
int currProgress = binding.progressBarNewRelease.getProgress();
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() > 0) {
binding.progressBarNewRelease.incrementProgressBy((int)increment);
}
else {
binding.progressBarNewRelease.incrementProgressBy((int)-increment);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
binding.textInputEditArtist.addTextChangedListener(...); // similar code for artist name field
I have been playing around with the code a bit so it's missing most of what I have tried like setting the maxProgress to (currentProgress + increment) to prevent additional increments once the field is not empty.
Also, if there is a way, I would like to avoid having to have multiple bindings for each field, but if that is unavoidable then I understand.
EDIT
I have achieved the initial functionality I was looking for by using a combination of beforeTextChanged and onTextChanged, although I am a little skeptical about whether it covers all edge cases:
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() == 0) {
binding.progressBarNewRelease.incrementProgressBy((int)increment);
}
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().length() == 0) {
binding.progressBarNewRelease.incrementProgressBy((int)-increment);
}
}
I am also now setting the progress bar's max to the number of fields, and incrementing/subtracting by 1 for each field. Additionally, rather than new TextWatcher(), I am also now using a shared function where possible, which reduces amount of code.
Going to explore getting all the logic right, but still looking for how I could avoid having to repeat the same code for every field. For example, this is how I am handling multiple buttons:
MaterialButtonToggleGroup.OnButtonCheckedListener gridRowButtonListener = new MaterialButtonToggleGroup
.OnButtonCheckedListener() {
#Override
public void onButtonChecked(MaterialButtonToggleGroup group, int checkedId, boolean isChecked) {
if (isChecked) {
binding.progressBarNewRelease.incrementProgressBy(1);
}
else {
binding.progressBarNewRelease.incrementProgressBy(-1);
}
}
};
binding.buttonsGridRowOne.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowTwo.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowThree.addOnButtonCheckedListener(gridRowButtonListener);
binding.buttonsGridRowFour.addOnButtonCheckedListener(gridRowButtonListener);

I want to detect the end of scroll event [duplicate]

This question already has answers here:
Detect end of ScrollView
(13 answers)
Closed 4 years ago.
I want to detect the end of scroll event of a scrollView.
first,i implement the OnCrollChange method and i try to listen some values if they can help me to know the end of scroll.
Thank to help me.
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener(){
#Override
public void onScrollChange(View view, int i, int i1, int i2, int i3){
Toast.makeText(getApplicationContext(),"in SCroll ------->"+i+" "+i1+" "+i2+" "+i3,Toast.LENGTH_SHORT).show();
shadowView.setBackgroundResource(R.drawable.shadow);
if ((i3==0)){
Toast.makeText(getApplicationContext()," equality ----------------------> ",Toast.LENGTH_SHORT).show();
shadowView.setBackgroundResource(R.drawable.trans);
}
else {
// Toast.makeText(getApplicationContext()," NO ----------------------> ",Toast.LENGTH_SHORT).show();
}
}
});
This will help
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
if (scrollView != null) {
if (scrollView.getChildAt(0).getBottom() <= (scrollView.getHeight() + scrollView.getScrollY())) {
//scroll view is at bottom
} else {
//scroll view is not at bottom
}
}
}
});
Modify your onScrollChanged method as below
#Override
public void onScrollChanged(View view, int i, int i1, int i2, int i3){
View view = (View) getChildAt(getChildCount()-1);
int delta = (view.getBottom()-(getHeight()+getScrollY()));
if(delta == 0){
// You have reached bottom of the scroll view
}
}
Thank to all of you.
after some testes;i see that the values int i, int i1, int i2 take the same value egual to 0 at the real time so it solve my problem.
I will not use your suggestions now but i learn some important notion from them only by reading.

EditText drawable left will not be changed

I have an EditText box that I want to change the background color and drawable left of when the character count is greater than or equal to 4. As seen in my code snippet below I used a TextWatcher to capture typing events and at the moment the background color changes, but the setCompoundDrawable does not change the drawable on the EditText box.
Am I doing something wrong, or is it a glitch of some sort?
final EditText input = (EditText)view.findViewById( R.id.editText );
input.addTextChangedListener( new TextWatcher() {
#Override
public void beforeTextChanged( CharSequence charSequence, int i, int i2, int i3 ) { }
#Override
public void onTextChanged( CharSequence charSequence, int start, int count, int after ) { }
#Override
public void afterTextChanged( Editable editable ) {
if(editable.length() <= 3 ){
input.setBackgroundColor( getResources().getColor( R.color.edittext_background_red) );
input.setCompoundDrawables( getResources().getDrawable( R.drawable.ic_cross ), null, null, null );
}else if(editable.length() >= 4 ){
input.setBackgroundColor( getResources().getColor( R.color.edittext_background_green ));
input.setCompoundDrawables( getResources().getDrawable( R.drawable.ic_tick ), null, null, null );
}
}
} );
use setCompoundDrawablesWithIntrinsicBounds(...) or call setBounds on your left Drawable before calling setCompoundDrawables

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.

Monitor the changes of a EditText [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is there a way to fire an event when the contents of an EditText view have changed?
I'd like to listen when the user changes a edit text in order to formatting it. Is there some kind of listener or interface I can use for that? Thanks
What you're looking for is probably a TextWatcher, check this link for more information. Here's how to implement it:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void afterTextChanged(Editable s) {}
});

Categories