집요정 도비의 일기
FindViewById 노가다 그만합시다. Prettify로 1초만에 findViewById 자동으로 다 하기. 본문
1. Intro
안드로이드 개발하다보면 제일 짜증나고 시간 아까운게 xml의 뷰 컴포넌트들(버튼, 이미지뷰 등등)을 액티비티 코드와 링킹하는 findViewById 과정이다.
안드로이드 해봤으면 굳이 설명 안해도 이게 얼만큼 시간을 쓰레기통에 버리는 일인지 알 수 있다.
본인이 이에 대해 처음 찾아봤을 때는 버터나이프를 쓰라고 글들을 접했으나, 버터나이프도 결국 바인드할 변수는 선언해줘야한다.
그래서 더 찾다가 찾은게 안드로이드 스튜디오의 플러그인인 prettify다.
본인은 이를 도입한지 반년이 넘었는데, 회사 선배가 아직도 findViewById 치고 계신걸 보고 마음이 아파서 공유함.
자동으로 findViewById를 해주는 아주 간단하고 쓰기 편한 플러그인이다.
다음 움짤로 prettify님의 위용을 엿보자.
1. prettify 설치
(1) 파일 - setting - plugin으로 진입 -> 하단의 Browse repositorie... 클릭
(2) preffify 검색 --> 설치 버튼 클릭(본인은 이미 설치가 되어 있어서 안보임.)
(3) 설치 완료 후 시키는대로 안드로이드 스튜디오 재시작.
2. 사용법
(1) 액티비티의 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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
(2) 액티비티 코드로 돌아와서 setContentView(R.layout. ...)에 커서를 주고 alt + insert 하면 다음과 같은 드롭다운 메뉴가 나옴.
*여기서 View Variables를 하면
View view = (View)findViewById(R.id.view);
와 같은 형식으로 위의 xml의 id를 할당한 모든 뷰들이 자동으로 findVIewById됨.
*View Fields의 경우
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.view = (View) findViewById(R.id.view):
위와 같이 필드로 할당됨. 움짤은 필드로 할당되는 예시임.
3. 마무리
제발 findVIewById 직접하면서 시간 낭비하지 말자.
'개발 일기' 카테고리의 다른 글
Apply Digest Auth to Exoplayer (0) | 2021.01.29 |
---|---|
EventBus로 여기저기서 쉽게 콜백 받기 (0) | 2016.12.28 |
Retrofit 2로 Json파싱 없이 Http통신 구현 시간을 단축해보자. (1) | 2016.11.07 |
selector auto creator (0) | 2016.10.18 |
Step 4. 음악 스트리밍 서버(Spring boot), 앱(Android)을 만들어보자. (6) | 2016.03.05 |