Вы находитесь на странице: 1из 4

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

How to dyamically select text from EditText onclicklistener?


android

android-widget

I want to use the android select text functionality on onclicklistener rather than onlongclicklistener. Is there any way to do this? Can anybody help me
regarding this? Thanks

share

improve this question


Sidharath
105 1 7

3 Answers

Asked
Feb 3 '12 at 11:57

Order By

with xml:
android:selectAllOnFocus="true"

with code (option1):


yourEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
});

with code (option2):

Votes

yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});

share

improve this answer


waqaslam
42.5k 7 92 117

Answered
Feb 3 '12 at 12:01

Edited
Feb 3 '12 at 13:08

Thanks for your reply Waqas. But the thing is want to select the text randomly not the whole text. I want to use android Context menu functionality in simpler way like if i click on the text in
edit text its start the point to select the text and ask to copy and paste after selecting Thats what i want to do. is there any way to do that? Thanks Sidharath Feb 3 '12 at 12:07
i just updated my answer. You may provide start and end int values to make particular selection waqaslam Feb 3 '12 at 13:08
add a comment

This answer gives you several options if you want to select all the text.
If not then use an onclicklistener and call setSelection on your EditText.
EDIT:
theEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.setSelection(editText.getText().length()-1); // selects all the text
}
});

share

improve this answer


mcnicholls
711 3 9

Answered
Feb 3 '12 at 12:03

Edited
Feb 3 '12 at 12:19

Thanks for ur your answer mcnicholls. but Can you please give me detailed example for how to use the setSelection method of edittext. As i have used it by extending the EditText class. But i
didnt get the idea how to use it for selecting the text on click.Thanks. Sidharath Feb 3 '12 at 12:11
I have added a code sample that would select all the text, but you can customise the selection by providing different for the setSelection parameters (there are a few versions of this method).
mcnicholls Feb 3 '12 at 12:20
add a comment

A totally different approach would be to try calling performLongClick from your EditText's onClick handler. This might let you use the default
long click functionality, but call it from your onClick.
theEditText.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
EditText editText = (EditText)view;
editText.performLongClick();
}
});

share

improve this answer


mcnicholls
711 3 9

Your Answer

Answered
Feb 3 '12 at 12:30

log in
or

Name

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc

Вам также может понравиться