Set unchangeable some part of editText android
Asked Answered
C

5

22

I have some EditText for mobile number input. App must add unique text for every country. For example for Armenia is must add +374 , and user must fill other numbers. Also +374 must be unchangeable, user can't change or remove it. So is there some kind of ways for doing this?enter image description here

EDIT:

I don't want to use textView or another view with this text and put it left of the ediText. I want to find some way with less operations. I need text to be frozen not to check every text changes or add missing text when user will delete some part of it.

Cylindrical answered 5/11, 2013 at 11:54 Comment(4)
possible duplicate of Put constant text inside EditText which should be non-editable - AndroidBebe
the most simplest way - one textview to display "+374" and one edit text to display second part. also you can use single editText, but should to implement TextWatcherTrimeter
Thanks for response, I don't want to use textView, and also about textWatcher I want to find some way with less operations. I need text to be frozen not to check every text Changes or add missing text when user will delete some part of it.Cylindrical
you can capture text event and concat the static part of text when first digit will be entered. Or you can test same thing with fouch gain. Just and idea not sure.Horsemint
C
36

Create a custom drawable class that will help to convert text into drawable.

public class TextDrawable extends Drawable {

  private final String text;
  private final Paint paint;

  public TextDrawable(String text) {
      this.text = text;
      this.paint = new Paint();
      paint.setColor(Color.BLACK);
      paint.setTextSize(16f);
      paint.setAntiAlias(true);
      paint.setTextAlign(Paint.Align.LEFT);
  }

  @Override
  public void draw(Canvas canvas) {
      canvas.drawText(text, 0, 6, paint);
  }

  @Override
  public void setAlpha(int alpha) {
      paint.setAlpha(alpha);
  }

  @Override
  public void setColorFilter(ColorFilter cf) {
      paint.setColorFilter(cf);
  }

  @Override
  public int getOpacity() {
      return PixelFormat.TRANSLUCENT;
  }
}

Then set the drawable to left of the edittext as

EditText et = (EditText)findViewById(R.id.editText1);
String code = "+374";
et.setCompoundDrawablesWithIntrinsicBounds(new TextDrawable(code), null, null, null);
et.setCompoundDrawablePadding(code.length()*10);

Where the edittext is defined in the layout file as

<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:ems="10" >
  <requestFocus />
</EditText>

Final Output looks like

enter image description here

Cycloid answered 5/11, 2013 at 12:44 Comment(6)
How to add the drawable at the right hand side of the EditText?Mendenhall
Use android:drawableRight in the xml fileCycloid
Is it possible to have something like this? i.imgur.com/VVq3jId.png I want to extension .jpg to be uneditable and always stays at the right side of the text that user enters/edits. Been struggling to make it work to no avail..Mendenhall
In your case you can implement TextWatcher to append .jpg in the end as the length of filename may vary in your case.Cycloid
This seems to not work anymore, the text doesn't stay true to the text size of the EditText. When you change the size in the class file along with the padding, it works, but then the added text doesn't align with the entered text.Overhasty
yes alignment issue is there. you can not append it with proper alignment.Mezzotint
C
8
public class MainActivity extends Activity {

private EditText editText;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    editText = (EditText) findViewById(R.id.editText1);
    editText.setText("+374");
    Selection.setSelection(editText.getText(), editText.getText().length());
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!s.toString().startsWith("+374")) {
                editText.setText("+374");
                Selection.setSelection(editText.getText(), editText
                        .getText().length());

            }

        }

    });
}

}
Corinnecorinth answered 5/11, 2013 at 12:20 Comment(7)
Thanks for response, and for code, But I think I can input "5", so I'll get this "5+374" text. I didn't find anything that will forbid user to do this changeCylindrical
sorry now made that change in my answerCorinnecorinth
now you can restrict user to enter anything without start with your fixed textCorinnecorinth
Thanks for your answer and that you have spend time on it. But after comment to my question I have added Edit field in the question, So your answer not is the correct answer I want , But thank yo very much, I'm up-voting your question, but can't accept it.Cylindrical
I wont spend my time to get votes but i want learn more thatswhy i spend sometime...can you tel me whats wrong in my answer so that i can improve thatCorinnecorinth
I have copy some text from my question. "I need text to be frozen not to check every text changes or add missing text when user will delete some part of it. " I think everything is clear nowCylindrical
Best answer for me. :) Will use it in my app.Psychotomimetic
J
1

you can create class extend AppCompatEditText or EditText.

public class CEditTextMaster extends AppCompatEditText {

        private boolean isNeedNoChangeSomeCharacters;
        private String charactersNoChange;

        public CEditTextMaster(Context context) {
            super(context);
            init(context, null);
        }

        public CEditTextMaster(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context, attrs);
        }

        public CEditTextMaster(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init(context, attrs);
        }

        public void init(Context context, @Nullable AttributeSet attrs) {

            if (isInEditMode())
                return;

            addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
                        if (!getText().toString().startsWith(charactersNoChange)) {
                            removeTextChangedListener(this);
                            if (charactersNoChange.length() > s.length()) {
                                setText(charactersNoChange);
                            } else {
                                setText(charactersNoChange + getText());
                            }
                            setSelection(getText().toString().length());
                            addTextChangedListener(this);
                        }
                    }
                }
            });

        }

        @Override
        protected void onSelectionChanged(int selStart, int selEnd) {
            if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
                if (length() > charactersNoChange.length() && selStart < charactersNoChange.length()) {
                    setSelection(charactersNoChange.length(),selEnd);
                }
            }
        }


        @Override
        public void setText(CharSequence text, BufferType type) {
            super.setText(text, type);
            if (isNeedNoChangeSomeCharacters && charactersNoChange != null) {
                if (!getText().toString().trim().startsWith(charactersNoChange)) {
                    setText(charactersNoChange + getText());
                }
            }
        }

        public void setCharactersNoChangeInitial(String charactersNoChange) {
            isNeedNoChangeSomeCharacters = true;
            this.charactersNoChange = charactersNoChange;
            if (!getText().toString().trim().startsWith(charactersNoChange)) {
                setText(getText());
            }
        }

}

for use:

edt.setCharactersNoChangeInitial("+734 ");
Juliannjulianna answered 14/11, 2017 at 11:3 Comment(0)
R
0

Use Text watcher and when user enters +3 it completes with +374 and it compares the first four characters and if it is same disable the back press of the softkeyboard so that it will not change the text and enters the other digit which append for the text used from the textwatcher

Repose answered 5/11, 2013 at 12:17 Comment(2)
Thanks for response. It's a good idea but it has some bug I think. I have +374, then I'm inputting "5" , so I have "+374 5 " then I can go and add "b" from start and So I'll have "b+374 5"Cylindrical
you have to add b after 5 then it will not show any bug..use textwatcher first and then add the digits afterwardsRepose
O
0

I opted for a solution, similar to another one submitted, of extending EditText and modifying onSelectionChanged. This prevents the user from even entering that region.

Here's what I'm using:

import android.content.Context
import android.text.Editable
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatEditText

class PrefixEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs) {
    private var prefix: String? = null

    fun setPrefix(prefix: String) {
        this.prefix = prefix
        setText(prefix)

        addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit

            override fun afterTextChanged(s: Editable?) {
                // Block deleting the prefix
                if (s?.startsWith(prefix) == false) {
                    setText(prefix)
                }
            }
        })
    }

    override fun onSelectionChanged(selStart: Int, selEnd: Int) {
        var newStart = selStart
        var newEnd = selEnd
        prefix?.length?.let {
            newStart = if (selStart < it) it else selStart
            newEnd = if (selEnd < it) it else selEnd
        }

        if (selStart != newStart || selEnd != newEnd) {
            setSelection(newStart, newEnd)
        } else {
            super.onSelectionChanged(selStart, selEnd)
        }
    }

    // Here to conform to EditText's API
    // Could also just override getText()
    fun getPostText(): Editable? {
        return prefix?.let {
            Editable.Factory.getInstance().newEditable(text)?.delete(0, it.length)
        } ?: run {
            text
        }
    }

    // Here for convenience, to avoid instantiating a new Editable, if the text is all you need
    fun getPostCharSeq(): CharSequence? {
        return prefix?.let {
            text?.substring(it.length)
        } ?: run {
            text
        }
    }
}

With tests:

import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.ActivityTestRule
import com.roosterteeth.roosterteeth.TestActivity
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class PrefixEditTextTest {
    @Rule
    @JvmField
    var activityRule: ActivityTestRule<TestActivity> = ActivityTestRule(TestActivity::class.java, true, true)

    private fun setupView(prefix: String, message: String): PrefixEditText {
        val editText = PrefixEditText(activityRule.activity, null)
        val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
        activityRule.activity.addContentView(editText, lp)

        editText.setPrefix(prefix)
        editText.append(message)

        return editText
    }

    @Test
    fun testSelection() {
        activityRule.runOnUiThread {
            val prefix = "pre: "
            val message = "hello world"
            val editText = setupView(prefix, message)

            editText.setSelection(0)
            Assert.assertEquals(editText.selectionStart, prefix.length)
            Assert.assertEquals(editText.selectionEnd, prefix.length)
            editText.setSelection(0, editText.length())
            Assert.assertEquals(editText.selectionStart, prefix.length)
            Assert.assertEquals(editText.selectionEnd, editText.length())
        }
    }

    @Test
    fun testGetPostText() {
        activityRule.runOnUiThread {
            val prefix = "pre: "
            val message = "hello world"
            val editText = setupView(prefix, message)

            Assert.assertEquals(message, editText.getPostText().toString())
            // This test is after to make sure that getting the post text did not actually modify the contents
            Assert.assertEquals("pre: $message", editText.text.toString())
        }
    }

    @Test
    fun testGetPostCharSeq() {
        activityRule.runOnUiThread {
            val prefix = "pre: "
            val message = "hello world"
            val editText = setupView(prefix, message)

            Assert.assertEquals(message, editText.getPostCharSeq())
            // This test is after to make sure that getting the post text did not actually modify the contents
            Assert.assertEquals("pre: $message", editText.text.toString())
        }
    }
}
Ozenfant answered 20/2, 2019 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.