Home » Android » Can Android Studio Automatically Extract References From a Layout XML file into the Activity java file?

Can Android Studio Automatically Extract References From a Layout XML file into the Activity java file?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Back in the day MotoDev Studio (Eclipse based, put out by Motorolla) had a feature to extract references from your XML into your code. Standard Eclipse with the ADT didn’t have this feature but was wondering if it’s possible in Android Studio.

So for example given XML:

<Button
    android:id="@+id/my_button">

It would generate in your activity’s class:

private Button mMyButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    mMyButton = (Button) findViewById(R.id.my_button);
}
How to&Answers:

There is plugin to Android Studio called ButterKnifeZelezny, which provides feature you’re requesting but only if you’re using ButterKnife. You can select what you want to import and so on.

ButterKnifeZelezny

Answer:

The answer is a qualified “yes”, starting with Marshmallow and going back to Gingerbread with its compatibility library, Android now has data binding. It doesn’t extract the references as such, but it does pretty much what you want.

So now instead of

<LinearLayout …>
    <TextView android:id="@+id/name"/>
    <TextView android:id="@+id/lastName"/>
</LinearLayout>

We have

<LinearLayout …>
    <TextView android:text="@{user.name}"/>
    <TextView android:text="@{user.lastName}"/>
</LinearLayout>

And instead of having this java code with extraneous boiler plate code

private TextView mName
protected void onCreate(Bundle savedInstanceState) {
  setContentView(R.layout.activity_main);
  mName = (TextView) findViewById(R.id.name);
  mLastName = (TextView) findViewById(R.id.lastName);
}
public void updateUI(User user) {
  if (user == null) {
    mName.setText(null);
    mLastName.setText(null);
  } else {
    mName.setText(user.getName());
    mLastName.setText(user.getLastName());
  }
}

We now have (which goes a step above what Butterknife does within its updateUI method)

private ActivityMainBinding mBinding;
protected void onCreate(Bundle savedInstanceState) {
  mBinding = DataBindingUtil.setContentView(this,
                           R.layout.activity_main);
}

public void updateUI(User user) {
  mBinding.setUser(user);
}

And the improvements go even beyond that, so I’d suggest you review the following presentation given by the Google Engineers, George Mount and Yigit Boyar.

It’s well worth the investment of going through it. All of this is done at compile time, so there is no incurred runtime penalty. And like I said at the beginning, it’s backwards-compatible down to Android 2.3, so there is really no reason not to use this new functionality.

Answer:

You could check out ButterKnife. It doesn’t exactly do what you want but it gets rid of a lot of boiler plate. @zigi ‘s solution seems to import everything which, in the real world, isn’t great!