I'm looking for a way to have a grayed out text as prefix in an EditText. This text should be not selectable.
It's a bit like the To field when you're composing a message with Gmail. The only (visual) difference is that this text disappears when you start typing.
Is there any trick to achieve this in Android?
Thanks!
You can use an image of the part "EUR 2500". this you can display in your editbox without affecting the rest of the part. Follow the code:
Drawable editTextDrawable = context.getResources().getDrawable(imageId);
editTextDrawable.setBounds(0, 0, editTextDrawable.getIntrinsicWidth(),
editTextDrawable.getIntrinsicHeight());
The drawable can be used inside the edittext as follows:
editTxtItemName.setCompoundDrawables(,
ListViewConstants.editTextDrawable, null, null, null);
As an ultimate solution, you can rewrite the full EditText class by extending it and modifying it in a way that it has a custom Background set by you, and a predefined padding set by you.
Put the EUR as the background, positioning it in the left side, and then give the starting padding of the EditText in such a way that the text the user types, starts right after the EUR text.
This maybe regarded as an overkill or a poor-man's solution to this problem, but still its the ultimate option. Not the smartest one perhaps, and I also don't know if its gonna work for sure :P
All the best!
Related
I was wondering how to make a text editor toolbar for an android app using Kotlin.
I've already implemented the view as you can see here. But I can't find a way to make it work propperly. I've tried to use Spannable String and Typeface but I think that they are not what I am looking for.
When using spannable, I need to put some string. But I can't put a string because there may be no text yet.
Maybe using the textchange listener of the EditText would work? I've tried it too but it doesn't seem to fit into my logic.
This is what I was thinking about: A listener for every button. Whenever a button is pressed, add that style to the future text that will come. To specify the start and the end of the text, maybe I could take the current position of the cursor to specify the start and the position of the cursor whenever the button is "unpressed" to specify the end. But I can't find something that fits my logic.
Maybe you could give me some ideas, another logic...Which is the proper way to do it?
Thank you very much :)
I'm looking forward to create an EditText with an Icon on the right that has some kind of functionality.
I've seen that the Google IO 2013 app has something like that on the search widget.
I want to know if its possible to do that with a simple EditText.
I've tried with a simple ImageView on the right of the EditText, but it looks really bad.
I would really appreciate if someone could point me in the right direction.
Thanks.
EDIT 1
This is an example of what I want. It looks really good. I have used the drawableRight and it doesn't look this good.
http://img836.imageshack.us/img836/1521/6faa.png
EDIT 2
I managed to build something that looks really similar to the SearchWidget with the voice recognition option.
I put two EditText next to each other (with a RelativeLayout), and set the margin left for the one on the right to -10dp. This way it looks like one EditText. I know this is not proper, but right now does what I want.
My problem now is the image, I'm using a 32x32 px image, because it fits fine, but it has really low quality, I'll look into that.
http://img560.imageshack.us/img560/9394/ilwp.png
You can definitely use a custom EditText with drawableRight as follows:
android:drawableRight="#drawable/your_drawable"
Refer
1.Custom EditText- 1
2.Custom EditText- 2
Hey guy's
First of all thanks for reading this.
I'm having trouble to find a way to change my EditText when I loose focus to it.
I would like it to be greyed out when this happens, but I don't want it to be disabled because the user can touch it and edit the text later.
Anyone?
Greetings!
You can set different colors to the text based on an OnFocusChangeListener
Another option is to set a style in xml. See this question for details: Android: change style when focused
To change the opacity, use setAlpha. In this answer I show it how to do it in an animation: Two questions about custom app ui's and AlphaAnimation
First, let me say, new to java, new to android. I'm attacking this head- with an ambitious first real project, a special kind of text editor.
I have figured out styling using spannables, but thats not particularly important.
I need a dynamic margin inside edittext. when I click the button, I want it to indent all of the text after that, on both left and right, until there is a carriage return, then it should drop back to no indent on a new line.
Think dialog in a play for an example of the output.
Thanks in advance for any information you might lend.
The answer in this post (look at the OLD ANSWER part) should help you solve the margin problem, but you can't have two different margins in one edit text. Consider creating a new edit text when a user presses carriage return or something of the sort.
Hope it helps.
I'm trying to make iPhone-style EditText element on android.
The one that will have an additional clear button appear on the right after text input.
Adding a new button is not a problem, but I'm a bit stuck with another thing.
A button occupies some space on the right part of EditText, and now characters display beneath the button. How to change maximum shown length of input for EditText?
I want EditText width to be N pixels, and editable area to be N-M pixels.
EditText.setWidth changes width for whole edit box.
EditText.setEllipsize should be the proper solution, but docs are empty, and as I see it truncates text based on some String value.
Applying a LengthFilter cut's the input length to number of characters.
Thanks in advance.
I suspect that android:drawableRight will save you a lot of pain.
Seems I've found a solution.
EditText.setPadding(l,t,r,b) seems to work fine, applying only for editable area
Try this : http://mytechead.wordpress.com/2012/02/07/create-ios-like-cleartextbutton-in-android/
This is a very old question, but thought I'd add my two cents for kicks. I'd probably use a 9 patch for this and set the content area to stop the text before it hits the button area.
This would require creating a custom view so that you can add a button in the desired position using relative layout so that it can be clicked to clear the edittext.
Alternatively you can use the compound drawables, but you would need to implement something like this
Handling click events on a drawable within an EditText so that you can handle the click events. Keep in mind that I doubt the button states (eg the down state) will work using this method.