Updating Android EditText's hint on focus change using XML only - android

I read with interest user sunit's answer to this question about updating an EditText's hint but have been unable to find any documentation on using the method that I presume he appears to describe there: using the <selector> element in an XML layout to dynamically adjust attributes of an EditText at runtime when the element is focused/unfocused.
In my case I am actually more interested in adjusting the android:inputType element (because the hint disappears for me when the inputType is specified) but adjusting the hint would work just as well.
To be clear I know how to make this change in Java code--I'm trying to find out if there is a way to specify the behavior in XML. Thanks!

I'm afraid it isn't yet possible. <selector> is only valid to be applied in making state lists out of Drawable and Color resources, it does not yet work for Strings.
With regards to your mention of adjusting android:inputType to make the hint disappear, this is actually a known Android bug that will eventually be fixed in later versions so I wouldn't recommend building your code around this functionality as it will break when they fix it:
http://code.google.com/p/android/issues/detail?id=13895
Since you mentioned that you already know how to do this in the Java code, I won't point out how to call setHint() from within a OnFocusChangeListener ;)
Cheers.

Related

How is the line under the text of an EditText created?

By default android.widget.EditText has a line under its text. How exactly is that technically done? I checked the source code of the EditText class, but couldn't find where this is done?
I think to know that the line is technically a background, but where is this background set? And especially, as it's just a background, how does Android ensure that this line is drawn below the text and not behind it? Answers with proofs / links to source highly appreciated.
yes, this underline is a background, which is probably ColorStateList (e.g. it changes color when pressed), but if you run your code on Android 4.x or older (pre Material Design) then this background is fixed bitmap/drawable
EditText have an android:background attribute, but it is also present for all other Views. so it is set inside source code of View, not extending EditText. and pointer to the proper drawable of background is set by styling/theming (in HERE you have some example)
EditText is using TextPaint (getPaint method) which may be used to add some extra spacing between/over/under/next to letters to ensure not-overlaying. also drawable may have padding attribute - may draw line in last row of pixels but set bottom padding to 1px and that ensures that content of this View (in this case EditText) will not be drawn under/over padding (but drawable does) - sooner View will expand a bit if have wrap_content for height
sorry for not-so-precise answer, topic needs more inwestigation and searching in sources, but maybe my points will help you find answers by yourself

Dynamic colours in an app

I have colors of text and button come dynamic form backend,
I need to change this color of text dynamically when change happens in backend all at time.
I could make it manual
textView.setTextColor(getResources().getColor(R.color.text_color));
Is there a way to change group of text color dynamically or must set every text color manually in code?
I searched for how to change theme colors dynamically at run time and i found this answer this answer.
But I also search again and I found this github
but it doesn't work on Android Marshmallow (6.0+) and it's use is discouraged! as he say.
Is there any lib or method to change the theme on runtime?
So far it's impossible and not viable because of themes are immutable.
GreenMatter becomes outdated so regretfully the answer of your question is No way.
More precisely, the color overriding at runtime is not working. There is no fix found at the moment. The future of this feature is uncertain.

android support lib 23.1.1 changes floating label color on setError()

is there a way to change the color of 'Password'? Basically, I prefer what support lib 23.1.0 had. I have a page that has multiple TextInputLayouts, and it would be hard to read if both hint and error are in red. please see the screenshot below.
there are a few fixes I want in 23.1.1, so please don't ask me to stay with 23.1.0. Thanks for all the help!!
Since it is a change in library it would be difficult to manipulate it. Android has decided to go that way (changing the label color along with the hint's color) and will become a standard soon and users will get familiar with it as time passes by. Not much we can do about it, but adhere, as always.
Workaround:
Don't set the Labels field on the TextInputLayouts. keep it blank and put another TextView just above it with the desired color and properly align it. That will not change, of course.
As per my comment below, you could also try this:
Hint color wont change. Just checked. So you may want to remove label and just add hint as android:hint="Enter password". The word Password in your screenshot is set in the Label and not as a hint.

Remove attribute across all edittexts

Is it possible to remove a feature (autocorrect, in this case) across the entire app from every single edit text by running an extension? Our app has a lot of edit texts across several activities and I would like to avoid running over each file to hit every one just to avoid adding a line of code, if possible. I have a style set up for that each one is connected to and tried adding
<item name="android:inputType">textNoSuggestions</item>
But that didn't do anything. Some of these fields do have input type set to be just number, and others I set for things like textCapWords or `textPersonsName' and things like that. I believe I made sure to give each one some kind of input, so is it because of that that the style line wasn't working?
I'm aware that I can just make a custom edit text and apply that to each layout file, but that would defeat the purpose of not running through each file.
tl;dr - Is there a way to remove autocorrect across every single activity without editing each layout file?

Getting All Views has text on Android layout for setting custom font. What is the Best Practice?

I need to get all views that have text for setting custom fonts.
I can develop a recursive method in myBaseActiviy class for getting all views with checking instanceof when programme is in OnCreate(). But I worry about the performance? I interest your idea? What should I do?
There is no best way. An approach, the one I use, is to subclass TextView, adding a new attribute to specify the font I want to use, delegating the logic to the subclass self.
I think the easiest way is to create your own TextView. It's not as hard as it sounds ;-).
This is the origional answer:
https://stackoverflow.com/a/19679639/2767703
And this is the link you'll need:
http://javatechig.com/android/using-external-fonts-in-android-view
Or if you want to set the font in the xml:
https://stackoverflow.com/a/7197867/2767703
Warning
If you are developing for Android versions lower than Android 4.0 then
you should change the code. In these versions there is a bug that
doesn't free up memory for typefaces. What you should do is create a
HashMap that allows reusage of Typefaces. You can find more in the
comments of the last link (search for the comment with the highest
upvote).
You could also use this:
https://stackoverflow.com/a/16883281/2767703
This changes the font of every text in your application. Note: You still have to look at my warning if you use this.
You can do something like this:
Add new attribute for store font in style.
Extend your view to handle this attr and set font by view when do you need.
You can find example of using and creation of new attribute for font at this link https://stackoverflow.com/a/12282315/2156937
Hope it helps.

Categories

Resources