Kyle Banks

Open Sourced: IconEditText for Android

Written by @kylewbanks on Sep 5, 2015.

IconEditText provides a reusable view for displaying an ImageView with an EditText for Android 4.0+.

Note: For more up-to-date documentation, check GitHub.

Usage

Using the IconEditText is easy enough, just clone the repo and add it to your project as a Library.

After adding the library, you can add an IconEditText like so:

XML

<!-- Note the declaration of the `widget` namespace. -->
<LinearLayout 
  ...
  xmlns:widget="http://schemas.android.com/apk/res-auto">

  <com.kylewbanks.android.iconedittext.IconEditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      widget:isPassword="false"
      widget:hint="@string/username"
      widget:iconSrc="@drawable/username_icon" />

</LinearLayout>

Options

The IconEditText currently supports the following properties:

  • isPassword {Boolean}: If true, will mask the EditText's input.
  • hint {String}: The hint text to display, if any.
  • iconSrc {Drawable}: The source of the icon image to display.

Java

From Java, you can reference the IconEditText like any other view. For example, from an Activity:

IconEditText iconEditText = (IconEditText) findViewById(...);

You can access the underlying EditText and ImageView like so:

EditText editText = iconEditText.getEditText();
ImageView imageView = iconEditText.getImageView();

There is also a convenience method for accessing the EditText's Editable:

Editable editable = iconEditText.getText();

Contributing

Contributions are always welcome, so don't hesitate to head over to GitHub and submit a pull-request.

Let me know if this post was helpful on Twitter @kylewbanks or down below!