How to detect span changes in EditText - java

Is there any way to detect changes made to spans in EditText (in Android)?
A TextWatcher added to the EditText is notified only about text changes.
No support in Editable/SpannableStringBuilder too.
A workaround could be to have a fake InputFilter on the Editable, but obviously not ideal, and you never know if the change was actually applied.
Thanks.

Related

How to set a SpannableString to an EditText in an Android Material TextInputLayout?

I can't find a way to set the text of an EditText in a Material TextInputLayout to a SpannableString and have the styling actually show up on screen. I've tried every combination of EditText inside a TextInputLayout, e.g.:
AppCompatEditText
EditText
TextInputEditText
with every way to create a SpannableString, e.g.:
HtmlCompat.fromHtml(string, HtmlCompat.FROM_HTML_MODE_COMPACT)
SpannableString(string).setSpan(...)
TextUtils.stringOrSpannedString(string)
with every way to set the text of an EditText, e.g.:
edittext.setText(spannedString)
edittext.setText(spannedString, TextView.BufferType.SPANNABLE)
edittext.append(spannedString)
The crazy thing is they all work when the EditText is not inside a Material TextInputLayout, so it must be a problem with that. More information: the SpannableString is respected when you set the hint or placeholder text on the TextInputLayout, just not the text of the EditText inside it. I looked through the TextInputLayout source code, but didn't see anything wrong. Thanks to anyone who can help.
I figured it out right when I was going to give up. You can get the Editable object from the EditText and then apply the Span to it. Here's how:
mEditText.text.setSpan(...)
Surprisingly easy when you know where to look.

Change the look of an EditText prgrammatically

I'm making an app that works as follows:
when you click a button, there will appear an EditText and a Button and a TextView.
I have already done that, but the style is not the same as the style when I create the EditText in xml (it looks different, the colour is not the same).
There will always be a couple of EditTexts, so the user will see the difference very well.
I also prefer the colour of the EditText when I create it in xml.
How can I change the style of an EditText programmatically?
Thanks in advance!
http://i.stack.imgur.com/24dzI.png (picture from the difference)
The issue here is that the background is being set differently between the two fields. You can just use setBackground(...) to have it match the other fields.
If you need to get the proper background drawable, look up one of the existing fields, grab the background from there, and assign it to your dynamic field.
http://developer.android.com/reference/android/widget/EditText.html
You can use EditText.setTypeFace() or EditText.setTextAppearance(), depending on what you're trying to change. The link has all the methods that EditText can perform.

How can I stop further text input in EditText on a button click and keep focus?

I am using a Button to check text entered into an EditText. After the button click event I don't want to allow any more text to be entered.
I previously used setEnabled(false) but the problem with this is the software keyboard is then closed, which means the user has to reopen it which is very inconvenient (the process in a loop is essentially: text entry --> button1 (stop text entry) --> button 2 --> text entry etc...). I tried to avoid this by adding in the code:
setEnabled(false);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
But, I notice this creates a short flash sometimes as the keyboard is quickly minimized and then restored again.
There must be another way to stop additional text alteration without losing the focus of the EditText?
Given the situation you are describing, the only thing I can think of would be to store the value and restore it at the end of your operation (as opposed to disabling the text box). It's not a very correct approach, but it should work...
You could use an InputFilter :
int currentTextSize = editText.getText.toString().length();
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(currentTextSize);
editText.setFilters(FilterArray);
and to remove it :
editText.setFilters(new InputFilter[] {});
You could of course declare those filters as static in your class if they are to be used often... This might not be the best way to achieve this but it's the first I thought of ;)
I managed to implement a solution to this that I was happy with, although its not a particularly elegant solution. For anyone who might stumble across this in the future:
I created the following EditText in my xml which provides an EditText to temporarily receive focus and means the keyboard remains open. Note the width and height are set to 0px so it is a hidden object.
<EditText
android:id="#+id/switcher"
android:layout_width="0px"
android:maxLength="1"
android:layout_height="0px"
android:textSize="10sp"/>
Then in my button click event I added the following code:
EditText attempt= (EditText) findViewById(R.id.attempt);
EditText switcher= (EditText) findViewById(R.id.switcher);
switcher.requestFocus();
attempt.setFocusable(false);
This moves the focus to hidden EditText and then stops the attempt text object from being able to be edited again. It can then be removed on another action with:
attempt.setFocusable(true);
attempt.setFocusableInTouchMode(true);
attempt.requestFocus();
Perhaps switcher.setFocusable should be changed between the two (but in the opposite way)

how to keep checking if the EditText are empty (if empty button is enabled else is disabled) : android/java

I have two EditText fields, and one button.
i want to do the following:
if the EditText1 && EditText2 == empty
then
button is disabled
else
button is disabled
the button should be enabled only if the user write something in both fields,
in simple words:
if the user write something in both fields, the button will be enabeld,
if he delete what he write then disable the button again.
i will be regretful if any one answer me.
android 2.2.
You want TextWatcher. Use this to determine when text is changed in both. You could either do an 'if else' every time text is changed or in a while loop. Hope this helps. And I think you will be grateful not regretful for help :)

EditText selects by default?

Whenever I have an EditText field in my android application, it is highlighted with the blinking cursor for input as soon as the activity is started (though the keyboard doesn't pop up). How can I disable this?
EditText and ListView are focusable in touch mode so when you launch your application by tapping on its icon (putting you into touch mode) the first one in your application will likely get focus. Had you entered your application by using the D-pad (moving you out of touch mode) the first Button, Spinner, EditText, or ListView would get the focus since they are all focusable.
I wouldn't get too hung up on the a View being in focus when your application starts but if you really can't stand the default way of handling focus, you could try giving focus to a TextView since it doesn't look different when it is in focus. Keep in mind that it isn't normally focusable in touch mode so you will need to enable it prior to requesting focus. Since this is a bit hacky, others (myself included) will caution you against going down this route and likely encourage you to accept the normal behavior.
View tv = findViewById(R.id.MyTextView);
tv.setFocusableInTouchMode(true);
tv.requestFocus();
PS: You may also want to check out this other SO question as it is nearly identical to yours.
You could call setFocusable(false) to avoid this. But if it should become focus later then maybe you have to call setFocusable(true) again later.

Categories