0

It's quite long to change the code:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
} 

to:

public class MainActivity extends AppCompatActivity {
    ActivityMainBinding mainBinding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        super.onCreate(savedInstanceState);
        setContentView(mainBinding.getRoot());
    }
} 

every time I create a new project or an activity. Is it possible to automate this process?

Alxa
  • 79
  • 1
  • 2
  • 11
  • 3
    I strongly recommend calling `super.onCreate()` before `ActivityMainBinding.inflate()`. In terms of changing the templates, there has never been a documented and supported way to do that, though you should find instructions for doing it lying around. – CommonsWare Sep 15 '20 at 11:51
  • 1
    I don't think there is a way to change the template cuz I have done little research, I anyone found the solution please update me. thanks – Kamran Ali Sep 15 '20 at 12:00

2 Answers2

2

You can not automate the process since we have to provide the layoutId ourself. Activity is not gonna bind to a layout automatically. What you can do is Create a BaseActivity and inherit it from all your Activites. Below is a template with binding .

public abstract class BaseActivity<B extends ViewDataBinding> extends AppCompatActivity {
protected abstract int getContentViewId();
protected B binding;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, getContentViewId())

}
}

class MainActivity extends BaseActivity<ActivityMainBinding> {
    @Override
    public int getContentViewId() {
        return R.layout.activity_main;
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // now you can directly access binding here
    }
}

This is just for Binding you can Also add some reusable method in BaseActivity and use them in Any Activity without writing them again. and super.onCreate(savedInstanceState) should be first line for call .

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Thank you! But what is ViewDataBinding? Is that a system class or I should create it myself? IDE just can't find it – Alxa Sep 16 '20 at 06:52
  • `ViewDataBinding` is a parent class for binding classes . You can just import it . It should be there it data binding is enabled. – ADM Sep 16 '20 at 06:56
  • I don't understand, for some reason Android studio sees neither ViewDataBinding, nor DataBindingUtil. I tried to import "android.databinding.DataBindingUtil" package, but it didn't work still, in spite of layout bindings work well – Alxa Sep 16 '20 at 07:04
  • rebuild the project if still does not work invalidate and restart . make sure you have data binding enabled in `build.gradle`. – ADM Sep 16 '20 at 07:05
1

No, I think it's not possible but you can make Live Template to get it frequently.

Or Simple that you can make a copy of that folder as a template. Whenever you need to create a new project it will be easy to copy/paste and just change the name of the project.

Thank you.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437