0% found this document useful (0 votes)
642 views

Android Edit Text Some Text Always There Not Editable - Stack Overflow PDF

This document is a discussion on Stack Overflow about adding non-editable text to an EditText in Android. The original poster wants to display static text alongside any user input in the EditText. Several users provide suggestions on how to accomplish this using a TextWatcher and manipulating the EditText text on each change event to append the static text. One user provides a code sample that successfully adds the static text but has an issue with cursor placement on backspace. Others warn about potential infinite loops and provide links to related questions for more information.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
642 views

Android Edit Text Some Text Always There Not Editable - Stack Overflow PDF

This document is a discussion on Stack Overflow about adding non-editable text to an EditText in Android. The original poster wants to display static text alongside any user input in the EditText. Several users provide suggestions on how to accomplish this using a TextWatcher and manipulating the EditText text on each change event to append the static text. One user provides a code sample that successfully adds the static text but has an issue with cursor placement on backspace. Others warn about potential infinite loops and provide links to related questions for more information.

Uploaded by

Shoaib Quraishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

Android Edit Text Some Text Always there not editable


android

android-edittext

I want to put some custom text on EditText . Which would be non editable And always appear on edit text.When we add something on edit text my non
editable text should move forward.
Something like this should appear by default
Non editable Text

And when i type something on edit text it should appear like this
Hello World Non editable Text

share

improve this question


Nouman Ahmad
8 3

Asked
Nov 20 '13 at 0:09

Cactus
12.2k 6 32 70

Edited
Mar 2 at 7:43

There is a programmatical approach to this. I'll write up an answer in a bit once I figure it out. But here is another way to do it: stackoverflow.com/questions/17517278/ mike yaworski
Nov 20 '13 at 1:02
could you please take a look at my answer and see if it works for you. Leave a comment with some feedback. mike yaworski Nov 20 '13 at 20:51

add a comment

add a comment

2 Answers

Order By

Votes

The text of the EditText is "(hint)" <-- this is important because of the lengths I use

This is the code. However, the only problem seems to be that when the user backspaces, it will not put the cursor in the correct place. You can
probably fix that witht the setSelection(int) lines. I'll make an update if I figure it out.
Note: all of the Toasts are just tests so you know what is happening with the code.
String pastText = ""; // represents the text BEFORE the text is changed each time
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et = (EditText)findViewById(R.id.et);
// run this code every time the EditText String is changed
et.addTextChangedListener(new TextWatcher()
{
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// length of the String in the EdiText
int length = et.getText().toString().length();
// is the String of the EditText from first position to length - 6 position
// (take away the non-editable text)
String editableText = et.getText().toString().substring(0, length - 6);

share

improve this answer


mike yaworski
6,596 10 29 58

Answered
Nov 20 '13 at 3:34

Thanks bro .. It worked for me ! Nouman Ahmad Nov 23 '13 at 13:52


add a comment

Maybe you can do this with addTextChangedListener with your EditText.


But be carefull:
This method is called to notify you that, somewhere within s, the text has been
changed. It is legitimate to make further changes to s from this callback, but
be careful not to get yourself into an infinite loop, because any changes you
make will cause this method to be called again recursively. (You are not told
where the change took place because other afterTextChanged() methods may already
have made other changes and invalidated the offsets

(As you can see here: http://stackoverflow.com/a/10862398/2668136)


You need to read these answers:
1. Put constant text inside EditText which should be non-editable - Android
2. Android java : Update same EditText in textChanged event
3. Access edittext from textwatcher
I suppose you can getLength() of the user's input and add your String at the end of your EditText.
Hope this helpful.

share

improve this answer


Fllo
8,152 3 16 47

Answered
Nov 20 '13 at 0:45

Edited
Nov 20 '13 at 0:51

This could definitely work. You would merely set your the inputed text + " (non editable text)" with each event. Mark Freeman Nov 20 '13 at 0:57
Thanks for your help ! It worked for me . I used afterTextChanged. And it worked just like the way i want. Nouman Ahmad Nov 23 '13 at 14:07
add a comment

Your Answer

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

You might also like