Skip to content
This repository

A utility class to automatically match Views in an Android Layout to a Field in any given Object

branch: master

Update readme

latest commit 0d4f82cecc
Xavier GOUCHET authored July 11, 2012
Octocat-spinner-32 .gitignore Initial commit July 11, 2012
Octocat-spinner-32 Licence.txt Initial version July 11, 2012
Octocat-spinner-32 README.md Update readme July 11, 2012
Octocat-spinner-32 ViewFieldMatcher.java Initial version July 11, 2012
README.md

ViewFieldMatcher

A utility class to automatically match Views in an Android Layout to a Field in any given Object

Basic Usage

Use the following code in your Activity's onCreate method.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/buttonSomething"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <CheckBox
        android:id="@+id/checkBoxEggs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/textName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </EditText>

</LinearLayout>
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        ViewFieldMatcher matcher = new ViewFieldMatcher(R.id.class);
        matcher.matchViews(findViewById(android.R.id.content), this);
    }

    public Button buttonSomething;
    public CheckBox checkBoxEggs;
    public TextView textName;

When creating the ViewFieldMatcher instance, you give it the class where your ids are declared. Usually, you'll be using your project's R.id class or sometimes the android.R.id class.

Then you simply give the topmost view you want to match (android.R.id.content is the topmost view in any android window), and a Target object. The target object is the object with the fields you want to match with your views.

In the above example, the fields in the Activity will be matched with the corrsponding views in the Layout.

Note Be sure that your fields are declared as public for the tool to work.

Advanced Usage

By default the utility needs both the id name and the field name to be identicall. But sometimes you want your fields to be named differently. Although it may lack some options, you can still match fields with views with different names. Here's an example, using a standard Java notation for fields (m prefix).

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_layout);

        ViewFieldMatcher matcher = new ViewFieldMatcher(R.id.class);
        matcher.setPrefix("m";
        matcher.setCapitalizeName(true);
        matcher.matchViews(findViewById(android.R.id.content), this);
    }

    public Button mButtonSomething;
    public CheckBox mCheckBoxEggs;
    public TextView mTextName;

The setPrefix() and setSuffix() methods let you provide text to put before / after the View's id name. Additionaly, you can capitalize the first letter of the View's id. In our example, you can match a view with a buttonSomething id to the mButtonSomething field.

Feedback

If you have any comment, feedback, suggestions feel free to contact me at android@xgouchet.fr. And of course you can fork this project.

Something went wrong with that request. Please try again.