0

to speed up the development of an App I created this editText with a label attached.

This is the class:

public class EditTextWithLabel extends LinearLayout {

    @InjectView(R.id.text_edittext_with_label)
    protected TextView label;

    @InjectView(R.id.edittext_edittext_with_label)
    protected EditText editText;

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

    ...

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.layout_edittext_with_label, this, true);
        ButterKnife.inject(this);

        setOrientation(VERTICAL);
    }

    private void getAttributes(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.EditTextWithLabel, 0, 0);

        try {

            CharSequence label = a.getText(R.styleable.EditTextWithLabel_label);
            if (!TextUtils.isEmpty(label))
                setLabel(label);

            CharSequence text = a.getText(R.styleable.EditTextWithLabel_android_text);
            if (!TextUtils.isEmpty(text))
                setText(text);

            CharSequence hint = a.getText(R.styleable.EditTextWithLabel_android_hint);
            if (!TextUtils.isEmpty(hint))
                setHint(hint);

            int maxLength = a.getInt(R.styleable.EditTextWithLabel_android_maxLength, -1);
            if (maxLength > 0)
                setMaxLength(maxLength);

            int type = a.getInt(R.styleable.EditTextWithLabel_android_inputType, InputType.TYPE_CLASS_TEXT);
            setInputType(type);

        } finally {
            a.recycle();
        }
    }

    ...
}

And this is xml:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/text_edittext_with_label"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/edit_text_radius"
        android:paddingBottom="5dp"
        android:text="@string/username"
        android:textColor="@color/text"
        android:textSize="@dimen/text_edit_text"
        />

    <EditText
        android:id="@+id/edittext_edittext_with_label"
        style="@style/EditText"
        android:inputType="textEmailAddress"/>

</merge>

I found out that if I put more of these in a fragment, when I restore it ALL the editTexts show the text that is wrote in the last one.

I cannot explain this behavior, so I hope that somebody could enlight me.

Thank you

EDIT

Thanks to J. Dow answer I was able to solve the issue, I've added at the end of the init method this code:

label.setId((int) System.currentTimeMillis());
editText.setId((int) System.currentTimeMillis());

This randomized the ids enough to avoid the issue.

Marco Fedele
  • 2,090
  • 2
  • 25
  • 45

1 Answers1

0

What merge is doing is basically a simple include. So your final Layout will include multiple "copies" of your EditText.

From the Android documentation:

Note: In order for the Android system to restore the state of the views in our activity, each view must have a unique ID, supplied by the android:id attribute.

http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Thus, when restoring your merged layout, the Android system will encounter multiple EditTexts with the same id and therefore restore each of them with the same state.

J. Dow
  • 563
  • 3
  • 13
  • Thanks, you've been very clear, so isn't there a way to optimize the layouts using always the same object? – Marco Fedele Apr 08 '16 at 16:29
  • 1
    Sure. If you want to reuse the same layout within the same fragment or activity layout, you just have to make sure that every view has a unique id within this layout. You can also overwrite the save/restore methods, but this is probably unnecessary complicated. http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts – J. Dow Apr 08 '16 at 16:37