Switch between a TextView and EditText
Is this possible to interchange a TextView and an EditText.
Put both in your layout in the same spot. Make one invisible. Toggle visibility as needed.
Or, put both as children of a ViewSwitcher
or ViewFlipper
, and use that to switch between them.
Is there a method (as in NON-XML) way of editing a TextView or non-editing a EditText?
No, AFAIK.
Yes, in fact, according to the EditText
code:
EditText is a thin veneer over TextView that configures itself to be editable.
And it is very thin! You could basically say a TextView
is an EditText
that is configured to be non-editable. The code for EditText
is so short I put it at the bottom of this question for reference.
The only real difference is in the styles, which are set in XML:
<style name="Widget.EditText">
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:clickable">true</item>
<item name="android:background">?android:attr/editTextBackground</item>
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">?android:attr/editTextColor</item>
<item name="android:gravity">center_vertical</item>
</style>
You can reset those to the TextView
values in java using something like this:
mEditView.setFocusable(false);
mEditView.setFocusableInTouchMode(false);
mEditView.setClickable(false);
mEditView.setBackground(null); // Or setBackgroundDrawable() on early APIs.
mEditView.setTextAppearance(mEditView.getContext(), android.R.attr.textAppearanceSmall);
mEditView.setTextColor(Color.BLACK); // I'm not sure how to get the default here.
mEditView.setGravity(Gravity.TOP | Gravity.START);
You probably won't want to change all of those, e.g. maybe leave gravity and text colour/appearance, so things don't suddenly change weirdly.
EditText
code
public class EditText extends TextView {
public EditText(Context context) {
this(context, null);
}
public EditText(Context context, AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.editTextStyle);
}
public EditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected boolean getDefaultEditable() {
return true;
}
@Override
protected MovementMethod getDefaultMovementMethod() {
return ArrowKeyMovementMethod.getInstance();
}
@Override
public Editable getText() {
return (Editable) super.getText();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, BufferType.EDITABLE);
}
/**
* Convenience for {@link Selection#setSelection(Spannable, int, int)}.
*/
public void setSelection(int start, int stop) {
Selection.setSelection(getText(), start, stop);
}
/**
* Convenience for {@link Selection#setSelection(Spannable, int)}.
*/
public void setSelection(int index) {
Selection.setSelection(getText(), index);
}
/**
* Convenience for {@link Selection#selectAll}.
*/
public void selectAll() {
Selection.selectAll(getText());
}
/**
* Convenience for {@link Selection#extendSelection}.
*/
public void extendSelection(int index) {
Selection.extendSelection(getText(), index);
}
@Override
public void setEllipsize(TextUtils.TruncateAt ellipsis) {
if (ellipsis == TextUtils.TruncateAt.MARQUEE) {
throw new IllegalArgumentException("EditText cannot use the ellipsize mode "
+ "TextUtils.TruncateAt.MARQUEE");
}
super.setEllipsize(ellipsis);
}
@Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(EditText.class.getName());
}
@Override
public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
super.onInitializeAccessibilityNodeInfo(info);
info.setClassName(EditText.class.getName());
}
}