[Kotlin Logo]


안드로이드 뿐만 아니라 코드를 작성하다보면 보일러플레이트 코드가 생기기 마련이다.

예를들면 Java에서 Getter, Setter를 추가 한다던지, 안드로이드에서 findViewById를 사용한다던지..

반복되는 작업이지만 안할 수도 없는 것들이라 언제나 고민거리다. 하지만 나의 생각은 또 다른 누군가의 생각인 법!

이를 해결하기 위해 다양한 언어에서 다양한 방법이 나와있으니, 꼭 사용하여 반복되는 코드를 피할 수 있으면 피하는 것이 좋다.


안드로이드를 기준으로 Java에서는 Lombok과 ButterKnife의 어노테이션을 사용하여 위의 코드들을 제거할 수 있다.

코틀린에서는 data class와 kotlin android extensions(synthetic)를 사용하여 제거가 가능하다. (butterKnife -> kotterKnife도 있음)



이제 본론으로..예제

오늘은 코틀린에서 XML을 템플릿 처럼 사용하는 방법을 알아보려고 한다.

[toolbar의 title을 사용하지 않고 toolbar 내부에 textView를 넣어 textView의 값을 변경하는 예제]


1. toolbar.xml (템플릿처럼 사용 할 toolbar layout)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
card_view:cardCornerRadius="0dp"
card_view:cardElevation="4dp">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:minHeight="?attr/actionBarSize"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" >

<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="@string/toolbar_title"
android:textColor="@color/md_black_1000"
android:textSize="18sp"
android:textStyle="bold" />

</android.support.v7.widget.Toolbar>
</android.support.v7.widget.CardView>

2. MainActivity.kt

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.toolbar.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

toolbar_title.text = "제목이 들어갑니다."

}
}

3. activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/topView"
layout="@layout/toolbar" />

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/topView">

</android.support.v7.widget.RecyclerView>
</RelativeLayout>


toolbar.xml을 만들어두고 사용할 화면에서 <include>를 해준 다음 Activity코드에서 toolbar.xml로 바로 접근이 가능하다.

toolbar의 설정을 변경하고 싶으면 toolbar.xxx , 내부에 textView를 변경하고 싶으면 toolbar_title.xxx 

물론 recyclerView도 바로 접근이 가능하여 recyclerView.xxx를 사용할 수 있다.

(일반적인 자바로 작성했었다면 findViewById ... toolbar ... recyclerView....... 이제 findViewById는 잊어도 된다.)


이와 비슷한 방법으로 Button을 만들어두고 Style을 지정하여 xml로 만들어두면 반복잡업 뿐만 아니라 다양한 어플리케이션에서도 활용할 수 있으니 얼마나 편한가!

또한 이러한 작업을 해두면 똑같은 버튼이라도 1px, 1dp가 다른 버튼이 생겨 전체의 틀이 망가지는 일을 없어 더욱 더 깔끔한 앱이 나올 것이다.