How to use Language injection in Android studio (APG-14)

How to use Language injection in Android studio (APG-14)

While working on Android project, you often come across situations where you need to use chunk of code which is different language in itself. For example, you may need to write a SQL query or a Regular expression as String literal.

As you are aware, the IDE treats them as string literals and does not give any assistance in editing/validating them.

Normally, you would go to Google and validate the SQL/RegEx, then return to the IDE. However, there is a better way to accomplish all of this within the IDE itself.


Meet Language Injection

Language injections let you work with pieces of code in other languages embedded in your code. When you inject a language (such as HTML, CSS, XML, RegExp, and so on) into a string literal, you get comprehensive code assistance for editing that literal.

To inject a language in your String literal,

1. Place the caret inside the string literal, tag, or attribute, in which you want to inject a language and press Alt+Enter and select Inject language or reference.

No alt text provided for this image

2. Choose the language you want to inject.

No alt text provided for this image

Now, to open an injection editor

1. Position the caret within the injected code piece and press Alt+Enter.

2. Select Edit <language name> Fragment.

Android Studio will open a dedicated editor section for editing the code with the injected language. This editor provides full code assistance, including code completion, inspections, intentions and code style actions.

No alt text provided for this image

#android?#androiddeveloper?#androiddevelopment?#androidstudio?#kotlin?#java


We also have an android developer community on WhatsApp where we enjoy learning and sharing everything android, including live sessions, interview preparation, job referral, tutorials, and Q&A. Join us if you're interested.

https://bit.ly/adc-whatsapp-1

https://bit.ly/adc-whatsapp-02?(Join this if the first group is full)

No alt text provided for this image

要查看或添加评论,请登录

Rajesh Hadiya的更多文章

社区洞察

其他会员也浏览了