How to import "xmlns:app="http....pk/res-auto" in xml automatically? or How to creat a template xml which includes this "xmlns:app" scheme?

Viewed 256

As well known, ConstraintLayout is now a most popular and efficient layout on android development. But for using it's attributes, you gotta import "xmlns:app" first. It's very annoying when you need to creat many xml files with ConstraintLayout. How do I creat xml files with this scheme("xmlns:app") automatically? I need a template instead of importing it manually.

enter image description here

3 Answers

The best in my opinion is to use Live Templates in Android Studio. It lets you create code snippets and add them with matter of seconds into the code:

https://medium.com/google-developers/writing-more-code-by-writing-less-code-with-android-studio-live-templates-244f648d17c7

And here is the example with XML:

https://riggaroo.dev/create-live-templates-android-studio/

So what you can really do is put this:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</androidx.constraintlayout.widget.ConstraintLayout>

in Android Studio -> Preferences -> Live Templates

enter image description here

The simple way is go to File -> New -> Edit File Templates.. -> Click + button -> paste your ConstraintLayout layout here -> Click ok. Done. Add custom xml

To use it: Right Click -> New -> MyXML (your xml file name) use custom xml file

Here it is. Go to Setting -> Menus and Toolbars -> Project view popup menu -> Change position of the From Template by press Alt + up (Alt + down), click ok to apply it

arrange custom xml file

Related