「findViewById」にイラつくことがなくなりそうなプラグイン「Android Layout ID Converter」

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Androidデベロッパーのみなさん、「findViewById」にイラっとくることありませんか?
そんなイライラをライブラリなしですっきり解決する Android Studio プラグイン「Android Layout ID Converter」のご紹介をします。

プラグイン導入

なにはともあれ、プラグインをインストールしましょう。
Preferences... > Plugins > Browse Repositories... とたどっていき検索するとプラグインが表示されるので、[install plugin]ボタンを押します。
いつも通り Android Studio を再起動しろと言われて「は?」っと思いながらも再起動するとプラグインが有効化されます。

installplugin

レイアウトXML

レイアウトのXMLファイルを作り、それぞれにidを付けます。

とりあえずこんな感じにしてみました。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MyActivity">

    <TextView
        android:id="@+id/text_label"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/seekBar" />

    <RatingBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/ratingBar" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New RadioButton"
        android:id="@+id/radioButton" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New CheckBox"
        android:id="@+id/checkBox" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner" />

    <ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New ToggleButton"
        android:id="@+id/toggleButton" />

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar" />

</LinearLayout>

ここからが本番

さあ、ここからがこのプラグインの本領発揮です。

手順 その1

レイアウトXML上で[control + alt + A]を入力する。
javaに変換するルールを選択して[OK]。
tojava
そうすると、画面右上に「クリップボードにコピーしましたよ!」ってでます。
copiedclipboard

手順 その2

おもむろにActivityに貼付ける。

Before
before
After
After
ごらんいただけるように、わずか2ステップで全ての「findViewById」の入力をこなしてくれました!

まとめ

こんな感じで、入力が非常に面倒な「findViewById」を簡単にアシストしてくれます。

prefixの有無や、フォーマットを選べるのは嬉しいですね。(ButterKnifeもあるんですね。。)
また、idにアンダースコアを含めていてもうまいことフィールド名をつくってくれます。

実際のところ、何を変換するか選択できればいいなあとも思いますがとりあえずidを付けなければ変換されないので、そこらへんは適宜調整といったところでしょうか。
Viewの数が多い場合なんかに一発でjavaに変換できるところは魅力的だと思います。
「findViewById」にイラついたら是非お試し下さい。