how to keep button disable any editText Empty ?
i've try with implementation 'androidx.core:core-ktx:1.1.0':
txtEmailOrNoPhone.doOnTextChanged { text, start, count, after ->
if (text.toString().isEmpty()) {
buttonLogin.isEnabled = false
} else {
txtPassword.doOnTextChanged { text, start, count, after ->
buttonLogin.isEnabled = text.toString().isNotEmpty()
}
}
}
but, not work.
because, if i run app (button enable). if i type and delete all (button disable). so, i must type first and delete to (button disable)
i want :
if start app
Since your code inside the doOnTextChanged is not triggered until you type something, your initial state of disabling the button will not work.
For that, just set the button disabled initially in the xml
android:enabled="false"
Related
I want to disable EditText contextmenuitem "Clipboard" to open a popup view. Whenever I long press edittext in my app contextmenuitem appears as "Paste, Clipboard". I want to retain the functionality of "Paste" option but "Clipboard" should not work. I have Made a Custom EditText class where I have callbacks for actions such as cut, paste,copy with respect to their Id. But I am unable to diable "Clipboard" function.
override fun onTextContextMenuItem(id: Int): Boolean {
// Do your thing:
val consumed = super.onTextContextMenuItem(id)
when (id) {
R.id.cut -> onCut()
R.id.copy -> onCopy()
R.id.paste, R.id.pasteAsPlainText -> {
onPaste()
}
android.R.id.keyboardView->{}
R.id.accessibilityActionContextClick->{}
R.id.accessibilityActionHideTooltip->{}
R.id.accessibilityActionImeEnter->{}
}
return consumed
}
Below picture shows the EditText options
and this picture shows when I Click Clipboard option
I have been reading the answers from the post How to set cursor position in EditText? I do have a specific problem in my EditText which might be the cause of the problem. In my xml layout, I define an EditText called "edit" whose inputType = none. According to the android developer documents, if the inputType = none, the text in not editable. The reason why is "none" is caused this is the way I prevent the android general keyboard from popping up over my User-Interface , which works very well.
I want to do something like this: user entered : 5 + sin( ) , now the cursor editText cursor position should be right after the ) symbol, but there's no cursor shown since the inputType = none. What I need to do is move the position of where the next text input will be placed in between the ( ) symbols. What happens is that the next text input goes right after the ) symbol.
So if the next text input is 4, then: 5 + sin( )4 , I don't want this. I rather have the following: 5 + sin(4 )
I am using a custom made keyboard which has the button "( )". Here's what I have tried without any success:
//once the user presses the "( )" button:
Selection.setSelection(edit.getText(),edit.getText().length() - 2);
Another way I have tried:
//once the user presses the "( )" button:
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setSelection(edit.getText().length() - 2);
edit.setInputType(InputType.TYPE_NULL);
Neither of the two ways has worked for me. I've also tried to set the inputType of the editText to something else besides "none" on the xml file. But, the android keyboard pops up on top of my custom made keyboard.
Any thoughts or ideas would be helpful
I don't know how exactly does your () button work but you can give a try this one.
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.length()>0 && s.charAt(s.length()-1) == ')' ){
yourEditText.setSelection(s.length()-1);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
I would need a way to detect if the EditText has been changed by the user typing something or by the app changing the text programmatically. Any standard way of doing this? I guess I could always do something hackish like unsetting the TextWatcher before setText() and setting it back again afterwards, but there's got to be a better way of doing this... right?
I tried checking if the EditText is focused in the TextWatcher, but that was of little help since the EditTexts gets focused "semi-randomly" anyway when scrolling...
Background
I have a ListView with EditTexts in every listitem. I've sorted out the basic problem of storing the values for the EditTexts for reuse when the user scrolls.
I also have a TextWatcher that sums up the values in all EditTexts and displays the sum when the user edits the content of any of the EditTexts.
The problem is that when I'm scrolling the list and my custom adapter is reentering the stored values in the EditTexts on bindView(), that also triggers the TextWatchers afterTextChanged() method, causing the scrolling to lag because the summing-up-function is triggered.
This sorted itself out a long time ago, but for anyone who finds their way here looking for an answer, here's what I did:
I ended up setting the Tag of the EditText to some arbitrary value right before I'm about to change it programmatically, and changing the value, and then resetting the Tag to null. Then in my TextWatcher.afterTextChanged() method I check if the Tag is null or not to determine if it was the user or the program that changed the value. Works like a charm!
Something like this:
edit.setTag( "arbitrary value" );
edit.setText( "My Text Value" );
edit.setTag(null);
and then
public void afterTextChanged(Editable s) {
if( view.getTag() == null )
// Value changed by user
else
// Value changed by program
}
The accepted answer is perfectly valid, but I have another approach;
#Override
public void onTextChanged(CharSequence charSequence,
int start, int before, int count) {
boolean userChange = Math.abs(count - before) == 1;
if (userChange) {
}
}
It works by checking if the change was a single character.
This is not a fool-proof solution as copy-paste operations might be missed, and non-user changes of a single character will also be missed.
Depending on your use case, this might be a viable solution.
One thing that helped to me is having boolean canListenInput field. Use it inside of watcher.
email.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (canListenInput) {
emailChanged = true;
}
}
});
Clear it before changing text programmatically. Set it inside of onAttachedToWindow, (after state) restoration:
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
canListenInput = true;
}
Depending on your use case (e.g. you are auto-populating this field when the user types into another field), you can also check if the view has focus, e.g.:
textView.doAfterTextChanged {
val isValueChangedByUser = textView.hasFocus()
// ...
}
I have created some extension methods to tackle this scenario
inline fun TextView.runTaggingCode(block: () -> Unit) {
this.setTag(R.string.tag_text_id, "set_from_code")
block()
this.setTag(R.string.tag_text_id, null)
}
fun TextView.isTaggedForCode() = this.getTag(R.string.tag_text_id) != null
where I have defined the R.string.tag_text_id as below
<string name="tag_text_id" translatable="false">dummy</string>
Now where I to use these methods, I will simply change my code as below,
override fun beforeTextChanged(
s: CharSequence, start: Int, count: Int,
after: Int,
) {
if (textView.isTaggedForCode()) {
return
}
textView.runTaggingCode {
// your logic here
}
}
But in case you don't want to change the same text view text, in it own TextWatcher you can also see the answer
You can do this by adding:
private String current = "";
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)){
[your_edittext].removeTextChangedListener(this);
//Format your string here...
current = formatted;
[your_edittext].setText(formatted);
[your_edittext].setSelection(formatted.length());
[your_edittext].addTextChangedListener(this);
}
Intro:
I am currently trying to implement an input method for an EditText for my Crossword Puzzle where the user sees something like "____" in the EditText. The underscores mark missing letters, the first char entered will fill the first underscore.
Of course other cells in the puzzle might be solved already, so the EditText text could be "ST_CKOV_RF_OW". I had all this functionality already in my own input view, a subclass of view with an overridden onDraw(). This worked pretty well, except that the view won't appear on some lower Android versions and the Back key slipped through my input routine and wasn't accessible.
So I thought I'd do it with EditText, implement a TextWatcher and be fine, but I can't get it to work properly. What I have right now is working, I can use the keyboard to enter letters, but again the Backspace isn't working, and of course if the user touches into the EditText the position gets messed up.
public void beforeTextChanged(CharSequence s,int start,int count, int after){
et.removeTextChangedListener(textWatcher);
int position = text.indexOf("_");
if(position==-1) onAnswerEntered(et.getText().toString().replace("_", "")); //finished
else {
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(textWatcher);
try {
String currentKey = s.toString().substring(start, start+1);
Logger.log("Current Key", currentKey);
int position = text.indexOf("_");
Logger.log("Current Position _ ", position+"");
//replace _ with key
String sbefore=text.substring(0, position);
String safter=text.substring(position+1, text.length());
text=sbefore+currentKey+safter;
int positionNext = text.indexOf("_");
Logger.log("Next Position _ ", positionNext+"");
if(positionNext==-1) onAnswerEntered(et.getText().toString().replace("_","")); //finished
else {
et.setText(text);
et.setSelection(et.getText().toString().indexOf("_"));
et.addTextChangedListener(textWatcher);
}
} catch(IndexOutOfBoundsException ioobe) {
ioobe.printStackTrace();
}
}
I also tried to set an OnKeyListener, but it won't work on EditText (I can get backspace event, nothing else)
So maybe I am totally on the wrong track, but please help me and give me a clue to how I can accomplish my goal. Thanks.
I gave up on it and implemented a simple but working kind of hack. I receive input in my (hidden) EditText now, the output goes to the visible TextView, a function in between fills the "_" with the input from the EditText.
Ex.
hint = "A_A_A_A"
edittext input = "BBB"
textview shows "ABABABA"
I have a loginscreen.
In this loginscreen I have a button that is by default disabled.
When the user has entered 4 numbers I enable the button and change the textcolor to green.
But when the 4 numbers are not the correct code I clear my edittext and disable my button again.
At the moment the textcolor of this disabled button is offcourse green.
How can I set it back to the default color?
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.length() >= maxLength)
{
btnOk.setEnabled(true);
btnOk.setTextColor(Color.parseColor("#00B32D"));
}
else
{
btnOk.setEnabled(false);
}
private void checkIfValid(String inputPin)
{
if(inputPin.equals("0000"))
{
startActivity(new Intent(this, ShowScreenActivity.class));
finish();
}
else
{
clearText();
====> //Here i want to set my textcolor back to normal.
Toast.makeText(this, "Pincode foutief", Toast.LENGTH_SHORT).show();
}
}
Get the default color of Button using this code,
int DefaultButtonColor = btnOk.getTextColors().getDefaultColor();
If its not what you are looking for, then you can get Android Platform Resource Color using
something like,
android.R.color.secondary_text_dark
Check others too...
Back up your default color in onCreate();
defaultTextColor = btnOk.getTextColors().getDefaultColor();
Then set it back
btn.setTextColor(defaultTextColor);
If you have another button that always maintains default color, you can set the color of your color-modified button to this other button to get back to default. The code might be...
btnOk.setTextColor(btnCancel.getTextColors());
This is a simple one line solution, but you have to be careful the other button color is not being modified for other reasons or this may not work.