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 :)
Related
In my app I have a couple of checkboxes, that when checked combine the first part of an edittext. In the same edittext I would like to allow the user to append some text, while disallowing to delete the built text.
This is how it looks
[This part is build from checkbox combination, and can change in real time][This part is user defined]
Now is there a way to not allow the user to modify the first part of the edittext, but still allow the app to change this text?
You can certainly create a TextWatcher object and add it to your edittext. In your textwatcher, you can store the constant text information in an instance variable. Then, you can fill in the onTextChanged() and afterTextChanged() methods to create the type of behavior you are looking for.
For example, you can check the cursor position (using editText.getSelected()) to see if the user tried to change some of the text that shouldn't be changed-- if they do, then have some code to handle the case.
I know this isn't the best answer, but I don't yet have privilege to make a comment. Hope this helps!
So I'm new to Android development and I'm currently figuring out TextWatcher.
What I'm attempting to do is attach my TextWatcher listener to an EditText widget and after the user has put in some text, say "Hello" and he highlights "llo" and types in r, I display the change in a TextView widget. For the above example it will display "llo --> r".
Now from what I've read and tried, since the textchangelistener is called every time the user types in something, my code ended up crashing when I ran it on my phone.
Is there a way to call the listener only when the highlighted text is changed so as to avoid calling it every time I'm just typing something in the EditText widget? I hope my question makes sense, I've tried looking around before posting here but I couldn't find anything.
Not looking for code, just some pointers so I can figure out how to do this.
Thanks!
Your best bet would be to use a OnEditorActionListener instead of a textwatcher which I didnt know existed until now. It gets called when there is a change to a textview, and since edit text is a textview than it will work perfectly.
Heres some info on the listener
Btw welcome to the android platform, I think you'll find that it is a rich language and definitely worth learning. If you really want some pointers your main point of reference should be this link which has many guidelines and pointers in the develop tab. Good luck
I am currently trying to make a Writing program. I would like the functionality of an Edit tool bar that contains things such as BOLD, ITALIC, UNDERLINE, etc. I see that it is under Typeface but I'm struggling to figure out how to make it function on just selected text or just for formatting text. I already have the GUI and button listeners in place. Thanks.
There is nothing really built into Android for this, except at a fairly low level. I have the beginnings of a RichEditText custom widget, as a drop-in EditText replacement, but it needs a fair bit of work, which I will get to later in June (I hope I hope I hope).
In a nutshell, you will need to get a Spannable object from your EditText via getText(), then call methods like setSpan() on it to apply your formatting.
Two words my friend, "creative commons". In my experience, mundane pieces of code like date/time pickers, RTF text boxes, etc.. they've all been coded and thought through more thoroughly than you'll ever have the time for. For something like this, don't reinvent the wheel, check out someone else's code, build on it, and check it back in.
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!
I have a large text view that I am styling with a SpannableStringBuilder. I want to be able to assign custom click event handlers to certain spans of text. For example, if I click a superscript character I want it to pop up a toast with some information about what the superscript references. So far I have found linkify which helps to make regular expression type of things like emails and phone numbers launch appropriate activities. What I want to be able to do is define a span and its styling and assign a click handler to it. I haven't found anything built in that supports this kind of functionality and so I'm asking for anyone with a fresh idea of how to do this. Thanks.
The only thing I can think of is for you to take a look at the Android source code for the Linkify class and see how that does it.
Take a look at extending ClickableSpan.
http://developer.android.com/reference/android/text/style/ClickableSpan.html
URLSpan which is the span used to Linkify, is an example implementation of ClickableSpan.
http://developer.android.com/reference/android/text/style/URLSpan.html