how to select a word in android textview - android

how can i get the exact value of the specific word in a textview when:
1 - the user had taped the word (the word can be just one of many in some text chunk),
2 - the textview has in its xml layout, android:textIsSelectable="true" ,
so by default android lets the user to highlight a specific word
and opens a small menu(copy, paste, share). can i manipulate this menu or maybe add more options to it in order to extend it and do something i want with the highlighted word?
i need to get the exact word based on some interaction with the user(click , tap, select....)
i didn't find any straight forward answer. is it even possible?
(i have android htc one x)
Thanks.

A more suitable component to use for your scenario is EditText.
It is declared like this in your manifest file:
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
Then, it is only two lines of code to get the selected text:
EditText et = (EditText)findViewById(R.id.title);
String selectedText = et.getText().substring(et.getSelectionStart(), et.getSelectionEnd());
Here, we assumed that the text was selected from left to right. If it was selected from right to left, then above would result in an exception so you can avoid that by checking if start position is higher than the end position.

Related

How to allow Multiline Text in Android EditText while restricting input using android:digits?

With an EditText it is possible to add an entry to support alphanumeric digits like the following:
android:digits="#string/alphanumeric_allowed_chars_free_textentry"
Where the string is defined as
<string name="alphanumeric_allowed_chars_free_textentry">
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=[];,./~!##$%^</string>
However, as others have noted, when a user sets the digits attribute, it can cause the "enter" button to show a return arrow instead of a next or done button, as described in this related question
The typical solution to get done to show up is to set android:singleLine="true" and android:imeOptions="actionDone", but this will only work for an input box which is not a multi-line box. I have a multi-line edit text and would like to be able to add a done button and also fliter using the digits attribute.
Does anyone know how to allow an EditText to use digits while also being multiline, while also allowing the return button to show "done"? Android 4.4
What if you modify alphanumeric_allowed_chars_free_textentry to include carriage return (\r, \n)?
i was unable to reply / comment to your comment
So added new answer
in multiline edittext
after adding \r\n to my digits property
android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\"
i am able to goto nextline on enter as well using the digits property.
below is my axml code "
i was unable to reply / comment to your comment
in multiline edittext after adding \r\n to my digits property android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\"
i am able to goto nextline as well using the digits property .
"
<EditText
android:id="#+id/NewTask_TaskDescription"
style="#style/MultiLineTextboxStyle"
android:layout_marginBottom="10dp"
android:layout_height="151dp"
android:layout_width="match_parent"
android:inputType="textCapSentences|textMultiLine"
android:singleLine="false"
android:digits="1234567890#$%^&*()_+-=qwertyuiopasdfghjklzxcvbnm:;,.?/|~`{}[]QWERTYUIOPASDFGHJKLZXCVBNM+x÷x€£¥₩×\r\n ●■□⊙☆¤•°《》¿¡\\" />
"

how to NOT select all on EditText long click

When the user long-touches/long-clicks in the EditText, my app always selects all text. How do you instead make the EditText not select all, and instead select only the user-selected text?
I read Select all text inside EditText when it gets focus
then tried explicitly setting android:selectAllOnFocus="false"but that is not working. Double tapping or any other click length doesn't work either.
To override OnLongClickListener might work, but I don't know how to get the user's start and end selected index.
For those who are coming across this question in the future, the (or at least a) solution is to add the textLongMessage option to android:inputType:
<EditText
...
android:inputType="textLongMessage" >
<!-- Or you might have multiple input types -->
<EditText
...
android:inputType="textLongMessage|textMultiLine|textNoSuggestions" >
This will make the EditText's behavior be to select a single word from the text.
Try reading these liknks and see if they help:
how to NOT select all on EditText long click
how to NOT select all on EditText long click
You can try in your main.xml file:
android:selectAllOnFocus="true"

android reading input from user

I want to get input from user . The inputs are text type such as name email phoneno :
Is EditText is the right way for reading input ?
Also i want to have the title of the input to be displayed like
Name
Email
And the data to be entered after these title.
So is it possible to have my cursor start from right end of the EditText and push characters to left as and when entered.
Also , now when i do gettext , even the title will be part of the read text .
IS there a way to get only the user input from EditText
<EditText android:id="#+id/name" android:layout_height="wrap_content"
android:text="Name" android:inputType="textPersonName"
android:layout_width="fill_parent" android:layout_below="#+id/textview1"
android:textSize="15sp" android:textStyle="bold">
</EditText>
Q: Is edittext is the right way for reading input?
A: Sure. Why not? There are other alternatives, but "EditText" is a good choice.
Q: And the data to be entered after these title. SO is it possible to
have my cursor start from right end of the edittext and push
characters to left as and when entered?
It's customary to pair a TextView (also called a "label" on other, non-Android platforms) next to your EditText. The label is read-only; the EditText field is changeable. Wrap both in a Layout to make sure the "prompt" is adjacent to the "edit":
EditText Label?
for the title, you can use:
-textView above the editText
-in the editText property in the xml, set the android:hint

How to allow the user to select a text range in a TextView (Similar to EditText)

I've got a TextView that I would like to allow the user to select a range of text from within it. The TextView takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:
eic = new InputConnection( bookTextView );
eic.beginBatchEdit();
But it doesn't do anything noticable. Does anyone know how to use InputConnection correctly? Thanks.
Edit: I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView or an EditText which looks and feels like a TextView and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).
Here is an idea.. Add an EditText with a TextView background, Here is an example
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
add this to your xml in the place of TextView
You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.
See also:
http://developer.android.com/reference/android/text/Spanned.html
You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.
Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My idea is the same as sandy's.
My code is here, hope it may help you:
https://stackoverflow.com/a/11026292/966405
After long internet surfing to find a solution, i prefered create my own class
https://github.com/orionsource/SelectableTextViewer
Goal features:
Easy to use - only one class
Support for text and Html.fromHtml
Can be in ScrollView with correct touches
Cursors can be redefined
Color of selection can be redefined
All the above solutions either too long or not working for me.
What you need is to add just textView.setTextIsSelectable(true)
in your activity or fragment or adapter.

Custom Position of Hint in Edit Text box.

I was wondering if there was any way that I could get a hint at the bottom of an Edit Text view -- and then the user to start entering text at the top of the box.
As a bonus question, is there any way I can make the hint NOT disappear once the user starts entering text.
You can set the position of the text using the "gravity" attribute (as noted at http://developer.android.com/reference/android/widget/TextView.html#setGravity(int)). So to put the text at the bottom you would have;
android:gravity="bottom"
And to answer your bonus question; No, you can't display the hint when text is entered into the edit text view. Displaying the hint only when the box is empty is the defined behaviour as noted at http://developer.android.com/reference/android/widget/TextView.html#setHint(int)
(and yes, I know the links are for TextView, but EditText derives all of its' text positioning and hint handling functionality from TextView).
Actually THERE IS a way to prevent hint from hiding, and it's a cool one :-)
It gives you the Floating Label look with smooth animation very easily and it's from android itself. No extra libraries and stuff.
Try this:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Touch me and I'll fly!"/>
</android.support.design.widget.TextInputLayout>
You can change the behaviour using other xml tags like android:gravity="start|center|end" and others.
As a BONUS, you can use error messages with it :-) Here's the link to that question: https://stackoverflow.com/a/30953551/6474744
And sorry I do not have enough reputatuion to post images, so help yourself:
http://i0.wp.com/androidlift.info/wp-content/uploads/2015/09/Screenshot_2015-09-28-17-03-561.png
Enjoy :-)
The problem with using both android:gravity and android:hint is that they are inter-linked with regard to cursor position.When you position the hint using gravity and you start entering text, it is entered in the same position as the your hint which is a problem if you want it to start traditionally on the top-left corner.

Categories

Resources