Full width home advertisement

2 ways to add Prefix and Postfix in Android Edittext For Android


There is basically two way of adding Prefix and Postfix in Android EditTextView.
  • added text change listener to edittext
  • create custom edittext
One way,

final EditText edt = (EditText) findViewById(R.id.editText1);

edt.setText("http://");
Selection.setSelection(edt.getText(), edt.getText().length());


edt.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("http://")){
                edt.setText("http://");
                Selection.setSelection(edt.getText(), edt.getText().length());

            }

        }
    });

Another way


import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;

/**
 * Custom EditText that displays a fixed prefix in line with the text.
* The trick here is to draw the prefix as a drawable and attach it via * setCompoundDrawables(). */ public class PrefixedEditText extends EditText { private ColorStateList mPrefixTextColor; public PrefixedEditText(Context context) { this(context, null); } public PrefixedEditText(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.editTextStyle); } public PrefixedEditText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mPrefixTextColor = getTextColors(); } public void setPrefix(String prefix) { setCompoundDrawables(new TextDrawable(prefix), null, null, null); } public void setPrefixTextColor(int color) { mPrefixTextColor = ColorStateList.valueOf(color); } public void setPrefixTextColor(ColorStateList color) { mPrefixTextColor = color; } private class TextDrawable extends Drawable { private String mText = ""; public TextDrawable(String text) { mText = text; setBounds(0, 0, (int) getPaint().measureText(mText) + 2, (int) getTextSize()); } @Override public void draw(Canvas canvas) { Paint paint = getPaint(); paint.setColor(mPrefixTextColor.getColorForState(getDrawableState(), 0)); int lineBaseline = getLineBounds(0, null); canvas.drawText(mText, 0, canvas.getClipBounds().top + lineBaseline, paint); } @Override public void setAlpha(int alpha) {/* Not supported */} @Override public void setColorFilter(ColorFilter colorFilter) {/* Not supported */} @Override public int getOpacity() { return 1; } } }


3 comments:

  1. At times I receive a prefix of @h &@I before message. What does this mean? Thank you for your time.

    ReplyDelete
  2. this code make prefix text constant, i dont want to make it constant ... can you please help me over it ?
    when i click on edittext certain strings comes as prefix and after that choice is mine that i can delete it or not

    ReplyDelete
  3. YouTube - YouTube - videoodl.cc
    YouTube. YouTube. YouTube. YouTube - YouTube. YouTube is a popular way to watch, youtube to mp3 download and enjoy video games, especially arcade, video slots and video

    ReplyDelete

Bottom Ad [Post Page]