Search code examples
androidandroid-linearlayoutorientation

Change default orientation of LinearLayout


I always forget to define android:orientation property to LinearLayout's in my layout-resources. Because of default value of layout's orientation (horizontal) I spent a little of time every time I forget about it. Is there a way to override default orientation of LinearLayout (btw, I don't want to use custom view) in whole application?


Solution

  • I am also having same problem of every time writing that same line.

    There are two ways you can do it:

    1. Create CustomLayout which will extend LinearLayout. [which you and me also don't prefer].
    2. Create Live Template - A cool feature of Android Studio.

    Going for 2nd one(a preferred one):

    1. Open Settings in Android Studio by File > Settings OR Ctrl+Alt+S
    2. Navigate to Editor > Live Templates

    Navigate to Editor

    1. Click on Add button on ADD right side. And select Live Template

    2. As we are creating template for XML, define applicable context as XML Text enter image description here

    3. Now fill details: Abbreviation name will be your short name. Description is optional. Fill template text as this: enter image description here [you can set height or width as whatever you prefer]

    4. Click DONE. That's it.

    Usage:

    Open any Layout xml file. And in Editor, just write your abbreviation which we set earlier.[you can also list out all available live templates by pressing Ctrl+J] You will see a hint: enter image description here

    Just click on that, and there you go. Your template text will be here: enter image description here

    [NOTE: In template text I have used $END$, which means after applying code text, the cursor will be placed at this point. For more Live template variables, please have a look at this link]

    For more cool details about Live Templates: LINK

    Hope, this will help you. You can also create lots of own Live Templates to make life easier. :)