I have a vertical linear layout with some input fields. Using TextInputLayout I get a nice flow with labels and built-in error messages. My problem is when I add and remove the error-messages.
If I add an error message it is positioned below the edit-text and everything looks good.
If I remove the error message with setError(null) the message is removed but the space is still there. This is per googles design apparently(see https://code.google.com/p/android/issues/detail?id=176005). I would very much like this space removed as it makes the UI look very wrong.
If I do .setErrorEnabled(false) the view is removed and everything looks normal again. However if the user changes the data and I do another setError the error-message is not shown (only the edit text line is red).
As of Support library version 23.1.1 (and perhaps earlier), this should no longer be the case. You should be able to call TextInputLayout.setErrorEnabled(false) to hide the error TextView and calling TextInputLayout.setError(error) now internally calls TextInputLayout.setErrorEnabled(true) if the error isn't null or empty. See the code snippet below, taken from the support library:
public void setError(#Nullable CharSequence error) {
if (!mErrorEnabled) {
if (TextUtils.isEmpty(error)) {
// If error isn't enabled, and the error is empty, just return
return;
}
// Else, we'll assume that they want to enable the error functionality
setErrorEnabled(true);
}
...
}
For me, below code is working fine.
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(mobileNoInputLayout.isErrorEnabled()){
mobileNoInputLayout.setErrorEnabled(false);
}
}
Related
I imagine someone has had this question before, I just don't quite know what the right keywords are to find the answer? I am making an android app with an activity that includes tabs using TabLayout. Nothing fancy, just really standard stuff. In fact, so far I've done literally nothing but make a completely new application with a single tabbed activity using the auto-generated code from Android Studio. Everything works fine, but there is one feature I cannot figure out how to turn off -- when I long click on any tab, a little rectangular alt text or something with the title of the tab pops up on screen just above the tab. It's not the end of the world if I can't eliminate it, I just find it to be irritating and incompatible with the overall desired feel of my app given that it's literally just duplicating the tab title. I can't find any code that is causing this to appear, so I don't know how to delete it. The picture below shows what I'm talking about circled in red.
If anyone needs me to post code to help answer, I can... but you can also just make a new tabbed activity in a throwaway application in Android Studio and get exactly the same boilerplate code I have.
Edit: I added the term "tooltip" to the title so others can find the relevant thread more easily if they have the same problem.
Kudos to Mike M. for the answer, shown in comments above. I implemented it successfully, so if anyone comes back here looking for the answer, here's the successful java code, which is placed in the onCreate() method of the activity containing the tabLayout:
// turn off that tooltip text thing immediately on activity creation
for (int i=0; i<tabs.getTabCount(); i++) {
TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
}
tabs.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
int tabPosition = tab.getPosition(); // syntactic sugar
viewPager2.setCurrentItem(tabPosition, true);
// Repeat of the code above -- tooltips reset themselves after any tab relayout, so I
// have to constantly keep turning them off again.
for (int i=0; i<tabs.getTabCount(); i++) {
TooltipCompat.setTooltipText(Objects.requireNonNull(tabs.getTabAt(i)).view, null);
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
I am using EditText's to accept an OTP, where user has focus on next EditText once he enters a digit to a field and so. It works fine on all devices. But on devices running android OS P i.e. API 28, requestFocus() does not work, and user is not able to enter digits to consecutive EditTexts as focus doesn't move automatically.
Here is the code - by default all EditText's are disable to prevent from opening system keyboard. I am using my own CustomKeybaord to accept numbers. However it works except Android P.
mEtCode1.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.d("BEFORE_", charSequence.toString());
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
hideError(charSequence.toString());
if (!charSequence.toString().isEmpty()) {
mEtCode2.requestFocus();
mEtCode1.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
mEtCode2.setBackground(getResources().getDrawable(R.drawable.verify_code_edit_text_background));
mEtCode1.setEnabled(false);
}
}
#Override
public void afterTextChanged(Editable editable) {
}
});
Please help me with this
Thank you, in advance
I had the same issue with my OTP screen on devices with Android P sdk. The problem was that i set the height and width of the editText to 0dp, which is focus disabling in android P,
as described in Android Developer page in the Android P change log:
android-9.0-changes-28#ui-changes
Views with 0 area (either a width or a height is 0) are no longer focusable.
This is issue with Android P. And what worked for me is the following code block, So sharing here:
enterOtpTextFrame.postDelayed(Runnable {
enterOtpTextFrame.requestFocus()
}, 100
)
we require to call requestFocus with postDelayed with some small time amount. In my case it is 100 millisecond.
Here is the official documentation for requestFocus method. It states there that it only works if the desired view for which you want to have focus is enabled, has size, is visible, is focusable and is FocusableInTouchMode.
I had exact same issue. I was using databinding to set the enable state of my EditText. I realised that requestFocus was not working because databinding, due to some unknown reasons, was not enabling my textview in time.
Here is my code:
/*
setMyEditTextEnabled is my method to which my view is binded i.e.
android:enabled="#{vm.myEditTextEnabled, default=false}"
This worked for all version except Android P because it has
some timing issues with API 28 (not sure what)
*/
//binding.getVm().setMyEditTextEnabled(true);
/*
So to make it work, I am enabling my EditText directly and
it works for all versions.
*/
binding.myEditText.setEnabled(true);
binding.myEditText.requestFocus();
Also, as mentioned in following post: EditText requestFocus not working
Do set focusable and focusableInTouchMode to true as well.
In short, my point is to make sure that your edit text is fulfilling all requirements as mentioned in official doc in order to requestFocus to work.
Encountered a similar problem when popping an alert dialog. In my case, postDelayed'ing a focus request or forcing the soft keyboard to pop up didn't work. Even if I could manage to pop up the keyboard, the focus stayed on an EditText in the main activity; needless to say I have tried clearing its focus and even disabling it.
However, popping the alertDialog delayed did the trick:
final AlertDialog alertDialog = alertDialogBuilder.create(); //a builder of your own
final Runnable r = new Runnable() {
public void run() {
alertDialog.show();
}
};
editTextOnMainActivity.postDelayed(r, 100);
So I noticed when I was debugging that there seems to be a tag that's repeating through my app entitled "BubblePopupHelper" with text: "isShowingBubblePopup : false"
Screenshot of the log
To my knowledge, I'm not using or causing it. Does anyone have an idea of what's going on? The application is the one I'm writing.
Upon further inspection, I did notice that every time I'm updating text (via a TextView) it displays onscreen. If there's a better way of doing so, please let me know.
Thanks!
The message seems to be logged by some SDK libraries whenever setText is called in a TextView. I get it in Android Studio developing with min API 14.
One interim solution till Google removes it would be using the filtering feature of Android Studio by writing a RegEx that only includes your log messages. For example if I have all my tags start with 'Braim' then 'Braim.*' can be used
If you want to filter this annoying logs away you can use the following regex:
by Log Tag: (?!^BubblePopupHelper)(^.*$)
Have you added "OnGlobalLayoutListener"?
I've encountered same problem and finally I found that getViewTreeObserver().addOnGlobalLayoutListener caused the problem.
Here is my solution:
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
...
textCategory.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
I believe I may have found a bug in the Android UI, but I'd like to make sure I'm not doing something flat out wrong. Here is a simplified version of the code I'm running that's causing the problem. This listener is set on a specific NumberPicker in my UI, and it properly disables / enables things, but if the user has changed the value of one of the other NumberPickers that it disables, it behaves a little bit oddly.
It still properly disables the input, but it fails to grey the value out, making it look like it's still enabled. Is this intended? Am I doing something wrong?
NumberPicker.OnValueChangeListener diceChangeListener = new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
View parent = (View) picker.getParent();
if (newVal == 0) {
((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(false);
} else if (oldVal == 0) {
((NumberPicker) parent.findViewById(R.id.diceCountPicker1)).setEnabled(true);
}
}
};
Let me know if there's a better way to do this, or if this is a legitimate bug. If it is a bug, is there a way to work around it?
Edit: Here's the definition of diceCountPicker1 in the XML:
<NumberPicker
android:id="#+id/diceCountPicker1"
android:layout_width="48dp"
android:layout_height="128dp"
android:descendantFocusability="blocksDescendants"
/>
Edit 2:
I have tested this in emulators, and confirmed that the problem doesn't exist before Jellybean (4.1). It works properly in ICS. That's... annoying. But I may be able to live with it for now. I'll leave this open for potential ways to work around the bug, but it looks to me like a real bug, and I doubt it can be fixed.
Try in this way.
NumberPicker npicker = (NumberPicker) findViewById(R.id.diceCountPicker1);
npicker.setMaxValue(100);
npicker.setMinValue(0);
npicker.setOnValueChangedListener(new OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal,
int newVal) {
// Conditions for Enable/Disable picker
if (newVal == 0) {
picker.setEnabled(false);
} else if (oldVal == 0) {
picker.setEnabled(true);
}
}
});
It works well for me and it is behaving properly with Enable/Disable as per added conditions.
Thanks.
Well, with no additional feedback coming in on this, I'm going to go ahead and close it out. Here's what I know:
This appears to be a bug introduced in Jellybean. I have tested in emulators, and the UI works as expected in ICS and earlier. For the time being, I'm just going to deal with this, and attempt to submit an official bug report.
Setting android:enabled="false" in NumberPicker and DatePicker xml layouts doesn't gray-out the controls for me on Android 4.0.4 device. However doing so programmetically via setEnabled(false) works as expected.
If I disable an EditText widget using
editText.setEnabled(false);
I can still type into it using the on-screen input method (in both the emulator and on the G1). Is this intended? How can I workaround this issue?
I notice that you can't activate the on-screen keyboard by tapping on a disabled EditText, and also the DEL key doesn't work, so this looks like a bug to me. I filed it as issue 2771 in the Android issue tracker.
edittext.setKeyListener(null);
This will help you
Use this, it worked for me
setFocusableInTouchMode(boolean);
setFocusable(boolean);
I fixed this issue but the patch only got included in Honeycomb. That's why I've created a little project which will contain my backported fixes to versions starting from 2.1. It contains the fix for bug 2771: http://code.google.com/p/android-fixes/
You can check out the "library" from the svn and include it in your project. Then instead of android.widget.EditText import edu.ubbdroid.android.widget.EditText (which extends the original EditText) and the problem should be gone :)
Perhaps You could alternatively dynamically substitute with TextView and back. But You would probably need to adjust font to match the EditText.
etComment.setEnabled(flag);
etComment.setFocusable(flag);
etComment.setFocusableInTouchMode(flag);
if (flag) {
etComment.requestFocus();
}
etComment.setFilters(new InputFilter[] { new InputFilter() {
#Override
public CharSequence filter(
CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!flag) {
return source.length() < 1 ? dest.subSequence(dstart, dend) : "";
}
return null;
}
}
});
for all
you can got it!
I think you should be able to editText.setOnClickListener() with your own function and call super.onClickListener() if you want your text edited.
Edit:
Following link has some answers which sound more like the right way:
Can we have uneditable text in edittext