Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

집요정 도비의 일기

FindViewById 노가다 그만합시다. Prettify로 1초만에 findViewById 자동으로 다 하기. 본문

개발 일기

FindViewById 노가다 그만합시다. Prettify로 1초만에 findViewById 자동으로 다 하기.

집요정_도비 2017. 3. 12. 02:49

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 직접하면서 시간 낭비하지 말자.





 


Comments