Put Constant Text Inside Edittext Which Should Be Non-Editable - Android

Put constant text inside EditText which should be non-editable - Android

Did u try this method?

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());

}

}
});

How to set text in edittext which should not editable

My Question is how to put TextView in EditText?

Not Possible, because This is not Built in behavior of EditText

How is it possible?

You can use a TextView and EditText in a LinearLayout with android:orientation="horizontal"

SAMPLE CODE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/colorBackgroundFloating"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="Enter Name" />

<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="123"
android:padding="5dp" />

</LinearLayout>

</LinearLayout>

OUTPUT

Sample Image

Android Edit Text Some Text Always there not editable

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);

int cursor = et.getSelectionStart(); // where the cursor currently is

// minSelection is the second position number of the non-editable
// text in the EditText. It's the second position because the
// cursor needs to be BEFORE it, and the comparison will be
// if the cursor is LESS than minSelection.
// so it's the length, subtract 5, because the non-editable text
// is 6 chars long. Ex: if the EditText was "test(hint)" then
// minSelection would be the position (as int) of 'h' in the String
int minSelection = length-5;

// if the user has his cursor BEFORE the non-editable text
if (cursor < minSelection)
Toast.makeText(getApplicationContext(), "good to type", 0).show();
else { // otherwise (he can't edit)
Toast.makeText(getApplicationContext(), "can't type", 0).show();
int currentCursor = et.getSelectionStart(); // get where the cursor is
et.setText(pastText); // set the text to what it used to be
et.setSelection(currentCursor-1); // set cursor where it previously was
}

Toast.makeText(getApplicationContext(), "past text: " + pastText, 0).show();

Toast.makeText(getApplicationContext(),
"text: " + editableText + '\n' + '\n'
+ "cursor: " + cursor + '\n' + '\n'
+ "minSelection: " + minSelection, 0).show();
}

@Override public void afterTextChanged(Editable s) {}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// in the method beforeTextChanged, set pastText to be the text
// of the EditText before the text is changed
pastText = et.getText().toString();
}
});
}

Set unchangeable some part of editText android

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

Sample Image



Related Topics



Leave a reply



Submit