If you just want the text to be selectable, simply use a TextView
. They have the textIsSelectable
attribute too and give a nicer UX (because the user thinks he can edit text in EditText
s, but he can't).
Working with EditText as with the TextView - onClickListener
-
13-06-2023 - |
Question
I have EditTexts intstead of TextViews so I can select some text to share.
android:textIsSelectable="true"
android:editable="false"
android:inputType="none"
But I have problem setting onClickListener on it. It barely works, you may tap it 10 times before Log show me result.I can do this :
android:focusable="false"
and onClickListener works fine with this, but I can't select any text to share
Solution 2
OTHER TIPS
this worked for me In this i am able to select text and can show a toast when it is clicked Just long press your edittext box and you select as much text as it contains
layout.xml
<EditText
android:id="@+id/justforFun"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Just for Fun"
android:focusable="true"
android:editable="false"
android:textIsSelectable="true" >
</EditText>
Activity code
EditText text = (EditText) findViewById(R.id.justforFun);
text.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
}
});
yourEditTextHere.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View view, MotionEvent motionEvent) {
// your code here....
//open keyboard all the time
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return false;
}
});
Use TouchListner
instead of clickListner
.
Reference: http://www.edureka.in/blog/android-tutorials-event-listeners
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow