Android: view/ drawable custom styles possible?

What I'd like to do is change the state (really, the background) of an EditText to reflect validity of its contents. E.g. if the user enters 999 where 999 is contextually invalid, the EditText should have a red border in place of the default orange border, likewise once the text is valid it should have a green border.

Methods I've explored:

  1. Changing the style of the EditText programmatically via something like editor.setStyle(R.styles.SomeID). Seems to be impossible within android.
  2. Adding custom states (state_valid, state_invalid) in R.attr, associating them with red/ green 9-patches, then calling drawable.setState() with one of these states. This worked in the sense that the state could be read back via getState(), but the border did not change colour.
  3. Setting the background resource directly upon detection of (in)validity. This works ok, causing the correct visual effect, but seems a little hokey, and allows only one state (e.g. I have to manually check for whether the EditText is pressed, enabled etc).

Given limited UI real-estate I am hesitant to introduce a separate UI element to visually feedback the text's validity to the user, hence my desire to display it in the EditText itself.

So.. is this something that's even feasible? It seems like a fairly common use case, so has anyone achieved what I'm trying to do in a straightforward and elegant manner?

Asked by: Walter762 | Posted: 24-01-2022

Answer 1

I would recommend changing the text color to indicate validity, rather than changing the color of the focus ring by any of the techniques you describe (of which only #3 seems practical).

Another possibility is to try setCompoundDrawablesWithIntrinsicBounds() to modify an icon on the left or right side of the EditText contents to indicate validity. I remember discussing this technique with somebody a few months back and forget if they got it working or not.

And, of course, another option is to not allow invalid input, via a custom input filter or listener or something.

Answered by: John304 | Posted: 25-02-2022

Answer 2

Well, I'd just extend the EditText class and build the desired functionality on top ( using the third approach you are suggesting, because it works :-) ). Doing this, you have to walk the way only once, and are open to change your implementation once you know the best way ( I would have personally solved it also using the third approach, seems fine to me ).

Answered by: Adelaide296 | Posted: 25-02-2022

Answer 3

i think a call to invalidateDrawable(yourDrawable) would work with approach number 2. i didn't try .. but it make sense

Answered by: Gianna559 | Posted: 25-02-2022

Similar questions

Android: Crop an Image after Taking it With Camera with a Fixed Aspect Ratio

I'm trying to crop an image after taking it, and my code is as follows: private void doTakePhotoAction() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString()); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); ...

Android: Keep GPS active

i'm having a main activity that runs a gps listener. If i'm starting a new activity the listener is destroyed. I need the gps to be still activated, also after starting the 2nd activity. Do i have to implement an own listener there, or is there another solution?

Android: is it possible to add ZIP file as a raw resource and read it with ZipFile?

Is it possible to add ZIP file to APK package as a raw resource and read it with ZipFile class? It looks like it's trivial to open file from SD card, but not from APK.

adb - Android: Delete entire database

I would like to delete a complete database created by my application. Do you know any adb command, or android sentence to do it?

Android: Internal Linkify does not work properly, and text, images flicker inside ListView

I enabled the Linkify property of a textview as follows: txtbox.setAutoLinkMask(Linkify.WEB_URLS); But, when there are URLs like are present (which is very common nowadays in messages like tweets), it doesn't display them properly. The problem is "sometimes" it succeeds and sometimes it fails... The other problem is, if this TextView is part of a custom view for a ListView, then unt...

menu - Android: Start activity from a MenuItem

I'm new on Android, and I'm trying to start an Activity from a MenuItem choose of the user. Actually, I'm building my menu (and is working OK) from my main activity class using a MenuInflater: @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); //the Menu Inflater class allows to create a menu from a XML File ...

Android: textView doesn't show cursor

I have a textView which is configured as an EditText. But the problem is that the cursor doesn't appear when i'm pressing keys (text is written correctly). Thanks

Android: Flush DNS

Closed. This question is off-topic. It is not curre...

Android: Handling Activity stack

I've got the following: Activities A, B, C, D. A and D can be reached at any time, anywhere from the application. B and C are reached like this: A -> B -> C I have the following use case: The user has entered C ( A -> B -> C ) then she has gone to D. ...

Android: Move whole layout upwards

I have a LinearLayout, when user selects my AutoCompleteTextView(ACTW) I want to move the whole layout upwards, so that the ACTW is at the top and there is space between the ACTW and software keyboard for suggestions. 1) How to do this? 2) How to make this animated (but this is not necessary)?

Still can't find your answer? Check out these communities...

Android Google Support | Android Community | Android Community (Facebook) | Android