Skip to content

Instantly share code, notes, and snippets.

@StefMa
Created July 8, 2015 11:04
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save StefMa/d133bb196bd019afb5f4 to your computer and use it in GitHub Desktop.
Save StefMa/d133bb196bd019afb5f4 to your computer and use it in GitHub Desktop.
TextInputLayoutWrapperView

TextInputLayoutWrapperView


What is it?

This is a simple wrapper View for the new TextInputLayout from Design Library.


Why?

Normaly you use the TextInputLayout like this

<android.support.design.widget.TextInputLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:hintTextAppearance="@style/TextAppearance.AppCompat">

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_password"/>
</android.support.design.widget.TextInputLayout>

So you have a fully "blow up" layout file. TextInputLayoutWrapperView fix this.


How to use?

You only need to put the files below into the specific folders.

  • TextInputLayoutWrapperView into your.package.name
  • attrs.xml into values/
  • text_input_layout_wrapper_view into layout/

You can now use your the Wrapper

<your.package.name.TextInputLayoutWrapperView
     android:id="@+id/dienstnehmer_name_et"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:hint="@strings/hint" />

Woha, more to know?

Nope :)

<resources>
<declare-styleable name="TextInputLayoutWrapperView">
<attr name="hint" format="string" localization="suggested" />
<attr name="text" format="string" localization="suggested" />
</declare-styleable>
</resources>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/text_input_layout_wrapper_view_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
public class TextInputLayoutWrapperView extends TextInputLayout {
private EditText editText;
public TextInputLayoutWrapperView(Context context) {
super(context);
init(null);
}
public TextInputLayoutWrapperView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
private void init(AttributeSet attrs) {
View inflatedView = getInflatedView();
editText = (EditText) inflatedView.findViewById(R.id.text_input_layout_wrapper_view_edittext);
if (attrs != null) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
R.styleable.TextInputLayoutWrapperView, 0, 0);
setText(typedArray.getString(R.styleable.TextInputLayoutWrapperView_text));
setHint(typedArray.getString(R.styleable.TextInputLayoutWrapperView_hint));
typedArray.recycle();
}
}
public CharSequence getText() {
return editText.getText();
}
public void setText(String text) {
editText.setText(text);
}
public CharSequence getHint() {
return editText.getHint();
}
public void setHint(String hint) {
super.setHint(hint);
}
private View getInflatedView() {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.text_input_layout_wrapper_view, this, true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment