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
Asked
Active
Viewed 8,097 times
3 Answers
5
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):
yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
@Override
public void onFocusChange(View v, boolean hasFocus){
if (hasFocus){
//((EditText)v).selectAll();
((EditText)v).setSelection(startValue, stopValue);
}
}
});

waqaslam
- 67,549
- 16
- 165
- 178
-
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 03 '12 at 12:07
-
i just updated my answer. You may provide start and end int values to make particular selection – waqaslam Feb 03 '12 at 13:08
0
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
}
});

Community
- 1
- 1

mcnicholls
- 846
- 5
- 10
-
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 03 '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 03 '12 at 12:20
0
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();
}
});

mcnicholls
- 846
- 5
- 10