Skip to content

Instantly share code, notes, and snippets.

@wolfiestyle
Created March 14, 2012 20:30
Show Gist options
  • Save wolfiestyle/2039291 to your computer and use it in GitHub Desktop.
Save wolfiestyle/2039291 to your computer and use it in GitHub Desktop.
assign all views on R.id to member variables of the same name using reflection
/** assign all views on R.id to member variables of the same name using reflection */
private void findAllViews()
{
Field[] r_fields = R.id.class.getDeclaredFields();
for (Field f: r_fields)
{
String name = f.getName();
Field v;
try
{
v = this.getClass().getDeclaredField(name);
} catch (Exception e)
{
Log.e("findAllViews", "field not found: " + e.getMessage());
continue;
}
int value;
try
{
value = f.getInt(null);
} catch (Exception e)
{
continue;
}
if (value == 0)
continue;
try
{
v.set(this, this.findViewById(value));
} catch (Exception e)
{
Log.e("findAllViews", "error setting value: " + e.getMessage());
continue;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment