1

I have a custom EditText declared in an XML file and I'm including it like so:

<include layout="@layout/my_edit_text"
        android:id="@+id/passwordField"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/passwordHint"
        android:inputType="textPassword" />

and here is my_edit_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:textColor="@color/gray"
    android:textSize="@dimen/editTextFontSize"
    android:padding="@dimen/editTextPadding"
    android:background="@drawable/edit_text_background"
    android:ellipsize="end" />

However, I can't set the hint or inputType this way, for some reason. If I set it in my_edit_text.xml, it works fine, but I would like to be able to set each reference individually.

The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.

Do I have to do something similar to what this person has? If I do, will I need to actually build a .java subclass and extract the attributes that way? That just seems excessive.

Community
  • 1
  • 1
RileyE
  • 10,874
  • 13
  • 63
  • 106
  • Your my_edit_text layout probably has a group like LinearLayout, so you can't set the hint because LinearLayout doesn't have hint. You can get the EditText in java and set the hint and inputType. – Marcio Covre May 02 '13 at 19:48
  • @nininho It does not, but I like that we're thinking. Thank you! – RileyE May 02 '13 at 19:51
  • Please post `my_edit_text.xml` and explain why it exists in the first place (what are you hoping to accomplish by having it?). Then we may be able to help you find a replacement approach for `my_edit_text.xml` that addresses your issues. – CommonsWare May 02 '13 at 19:59
  • @CommonsWare I've added it and the explanation (Second paragraph after code) – RileyE May 02 '13 at 20:04

1 Answers1

3

The reason that I have the custom EditText is to avoid having to rewrite all of the common values in every one of my EditTexts.

Step #1: Delete my_edit_text.xml.

Step #2: Delete all references to my_edit_text.xml.

Step #3: Define a style resource (e.g., in res/values/styles.xml) akin to:

<style name="rileyText">
  <item name="android:textColor">@color/gray</item>
  <item name="android:textSize">@dimen/editTextFontSize</item>
  <item name="android:padding">@dimen/editTextPadding</item>
  <item name="android:background">@drawable/edit_text_background</item>
  <item name="android:ellipsize">end</item>
</style>

Step #4: Add style="@style/rileyText" to all EditText widgets that you want to have those particular attributes applied to.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    And this is why you're awesome. Thank you! I am just trying to learn all of the proper Android coding practices and this is excellent! – RileyE May 02 '13 at 20:11
  • @RileyE: Sorry that your proposed edit was rejected -- I have fixed the answer. – CommonsWare May 02 '13 at 21:01
  • As long as the edit is made, it will be perfect for future coders. Thanks again. – RileyE May 02 '13 at 21:14