How to use Language injection in Android studio (APG-14)
Rajesh Hadiya
Helps you to master Android & Spring Boot ?? Aaye hain to thoda content dekh ke jaiye ?? Join our next Android meetup (pinned post ??)
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.
2. Choose the language you want to inject.
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.
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-02?(Join this if the first group is full)