2025年5月5日月曜日

Android gradle.properties に定数を入れる

どうも。どっことです。今回はgradle.propertiesを使った設定値の管理方法について紹介します。

gradle.propertiesで設定値を管理する

gradle.propertiesを用意し定数値感覚で設定すれば、アプリのバージョンやtargetVersion/minSDKVersionなどの値を一つのファイルで管理させることができます。

設定方法

例えば以下のbuild.gradleにあるマジックナンバーを定数化しgradle.propertiesに移して参照しましょう。

android {
    namespace = "com.mkt120.sampleapplication"
    compileSdk = 35
    defaultConfig {
        applicationId = "com.mkt120.sampleapplication"
        minSdk = 26
        targetSdk = 35
        versionCode = 100
        versionName = "1.0.0"
        ... 
    }
    ... 

gradle.propertiesに外出しする定数を記述します。

ANDROID_SDK_VERSION=35
ANDROID_MIN_SDK_VERSION=26
ANDROID_VERSION_NAME="1.0.0"
ANDROID_VERSION_CODE=100

上で配置した定数をbuild.gradleで参照します。

android {
    namespace = "com.mkt120.sampleapplication"
    compileSdk = Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)

    defaultConfig {
        applicationId = "com.mkt120.sampleapplication"
        minSdk = Integer.parseInt(project.ANDROID_MIN_SDK_VERSION)
        targetSdk = Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
        versionCode = Integer.parseInt(project.ANDROID_VERSION_CODE)
        versionName = project.ANDROID_VERSION_NAME
        ...        
    }
    ...

gradle.propertiesに記述した定数はproject.XXXXXの形式で参照することができます。

まとめ

今回はgradle.propertiesを使った設定値の管理方法について紹介しました。

参考

2025年5月4日日曜日

Android いまさら聞けないRelativeLayout

どうも。どっことです。今回はRelativeLayoutについて解説します。

RelativeLayout

RelativeLayoutはViewを相対的な位置関係で配置するためのViewGroupです。親のViewGroupを基準とした位置や同じRelativeLayout内の他Viewを基準として位置を決めることができ、FrameLayoutLinearLayoutより柔軟にViewを配置することができます。現在はConstraintLayoutが制約という形でRelativeLayoutよりも難しい配置設定を実現できるようになったことから、RelativeLayoutはレガシーなViewGroupという整理になっていますが、長く開発されてきたアプリの場合RelativeLayoutを使って画面レイアウトを実装しているものも多いと思いますので、理解しておくに越したことはないでしょう。今回はそんなRelativeLayoutの使い方を解説します。

押さえておくポイント

RelativeLayoutを使ううえで、押さえておくべきポイントは以下です。

  1. layout_alignParent***
  2. layout_center***
  3. layout_toStartOf
  4. layout_toEndOf
  5. layout_above
  6. layout_below
  7. layout_align***

順番に解説していきます。

layout_alignParent**

まずはlayout_alignParent**です。layout_alignParent** はViewを上下左右のいずれかに寄せる設定です。以下の4種類があります。

  • layout_alignParentTop
    • Viewを上寄せに配置する(デフォルトなので使うことが少ない)
  • layout_alignParentBottom
    • Viewを下寄せに配置する
  • layout_alignParentStart(Left):
    • Viewを左寄せに配置する(デフォルトなので使うことが少ない)
  • layout_alignParentEnd(Right):
    • Viewを右寄せに配置する

以下はサンプルです。layout_alignParentToplayout_alignParentStartは設定してもViewが移動しないので、省略します。

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:text="alignParentEnd" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="alignParentBottom" />

</RelativeLayout>

こちらを実装した時のプレビューは以下です。

layout_center**

layout_center**は上下中央、左右中央、中央にViewを配置するための設定です。以下の3種類があります。

  • layout_centerVertical
    • Viewを上下中央に配置する
  • layout_centerHorizontal
    • Viewを左右中央に配置する
  • layout_centerInParent
    • Viewを中央に配置する

以下はサンプルです。

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Center\nIn\nParent" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Center\nHorizontal" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="Center\nVertical" />

</RelativeLayout>

こちらを実装した時のプレビューが以下です。

layout_toStartOf

ここまでの配置設定は親Viewを基準にして設定するものでしたが、ここからは同じRelativeLayout内にある他のViewに対する設定となります。

まずはlayout_toStartOfです。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:layout_toStartOf="@id/text_view_target"
        android:text="toStartOf" />

</RelativeLayout>

layout_toStartOfを設定すると、指定したViewのと自身のの位置を揃えてくれます。

layout_toEndOf

次にlayout_toEndOfです。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/text_view_target"
        android:padding="8dp"
        android:text="toStartOf" />

</RelativeLayout>

layout_toEndOfを設定すると、指定したViewのと、自身のの位置を揃えてくれます。

layout_above

次にlayout_aboveです。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/text_view_target"
        android:padding="8dp"
        android:text="above" />

</RelativeLayout>

layout_aboveを設定すると、指定したViewのと自身のの位置を揃えてくれます。

layout_below

そしてlayout_belowです。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/text_view_target"
        android:padding="8dp"
        android:text="below" />

</RelativeLayout>

layout_belowを設定すると、指定したViewのと自身のの位置を揃えてくれます。

layout_align**

最後に紹介するのは、layout_align**です。この設定は指定のViewの端を揃えるものです。以下の4種類があります。

  • layout_alignTop
    • 指定Viewの上端を揃える
  • layout_alignBottom
    • 指定Viewの下端を揃える
  • layout_alignStart(Left):
    • 指定Viewの左端を揃える
  • layout_alignEnd(Right):
    • 指定Viewの右端を揃える

layout_alignTop

layout_alignTopです。指定したViewの上端と揃う位置に配置されます。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/text_view_target"
        android:padding="4dp"
        android:text="alignTop" />

</RelativeLayout>

指定したtargetと上端が揃う位置にViewが配置されます。

layout_alignBottom

次にlayout_alignBottomです。指定したViewの下端と揃う位置に配置されます。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/text_view_target"
        android:padding="4dp"
        android:textSize="8dp"
        android:text="alignBottom" />

</RelativeLayout>

指定したtargetと下端が揃う位置にViewが配置されます。(テキストサイズを小さくしています)

layout_alignStart

続いてlayout_alignStartです。指定したViewの左端と揃う位置に配置されます。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@id/text_view_target"
        android:padding="4dp"
        android:text="alignStart" />

</RelativeLayout>

layout_alignEnd

最後にlayout_alignEndです。指定したViewの右端と揃う位置に配置されます。

<?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/text_view_target"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="target" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@id/text_view_target"
        android:padding="4dp"
        android:text="alignEnd" />

</RelativeLayout>

まとめ

今回はRelativeLayoutについて解説しました。すでにレガシーとなったViewGroupですが、まだまだ利用されているプロジェクトも多いと思うので、それぞれの設定について理解しておきたいですね。

参考

2025年5月3日土曜日

Android いまさら聞けないLinearLayout

どうも。どっことです。今回はLinearLayoutについて解説します。

LinearLayout

LinearLayoutは順番にViewを並べてくれるというかなり便利なViewGroupであり、それでいて利用するために必要な設定が多くないところから今でも利用頻度が高いものとなっています。

今回はそんなLinearLayoutの使い方を解説します。

押さえておくポイント

LinearLayoutを使う上で押さえておくべきポイントは以下です。

  1. orientation
  2. layout_weight
  3. layout_gravity/gravity

順番に解説していきます。

orientation

orientationは、Viewをどの方向に並べるか設定するものです。Viewを順番に並べる機能を持つLinearLayoutの最も重要な設定となります。

設定は縦方向(vertical)か横方向(horizontal)のいずれかで、縦方向を設定した場合は上から下に、横方向を設定した場合は左から右にViewが配置されます。

もし画面に収まらない場合は当然はみ出してしまいますが、LinearLayoutにはスクロールする機能がないので、ScrollViewHorizontalScrollViewの中にLinearLayoutを配置することで画面をスクロール可能にしましょう。

layout_weight

layout_weightはLinearLayout内に並べたViewの大きさを比率で決定するためのものです。

例えばいくつかのViewを同じ大きさで並べたい場合は、すべてのViewのlayout_weightを1に設定することで全てのViewの大きさを1:1にすることができます。Viewの大きさを決めるlayout_widthまたはlayout_heightを0dpに設定する必要があります。

例えば3つのViewを均等幅(1:1:1)で並べる場合は以下です。

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="左"
        android:background="@android:color/holo_red_dark"
        android:gravity="center" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="中"
        android:gravity="center" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="右"
        android:background="@android:color/holo_blue_dark"
        android:gravity="center" />
</LinearLayout>

このレイアウトのプレビューが以下です。

またlayout_weightの便利な使い方として、並べたViewのうちの1つだけ画面いっぱいに表示したいケースが挙げられます。

例えば画面内にヘッダ・フッタを持つページの場合です。ヘッダとフッタは固定(または可変長)、コンテンツ領域は残った領域全てとしてViewを配置するケースがほとんどだと思いますが、このようなケースでlayout_weightが大活躍します。

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="ヘッダ領域" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="コンテンツ領域" />


    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="フッタ領域" />
</LinearLayout>

この実装をプレビュー表示すると以下になります。

RelativeLayoutConstraintLayoutでも同様の画面構成を実現できますが、これらを使う場合はヘッダ・フッタ領域にidを付与する必要があり、それを基準にコンテンツ領域に対して制約をつける必要があります。

layout_gravity/gravity

layout_gravityはViewの位置を決める設定です。LinearLayout内のViewに対して設定します。

この設定はFrameLayoutにも同様にありますが、LinearLayoutではorientationの設定が反映されつつ、layout_gravityの設定が反映されます。

例えば、以下のケースを見てみましょう。

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_red_dark"
        android:layout_gravity="end"
        android:text="左" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="中" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_blue_dark"
        android:text="右" />
</LinearLayout>

このレイアウトのプレビューは以下になります。LinearLayoutのorientationの制約が守られつつ、layout_gravityに合わせた位置にViewが配置されています。

また、LinearLayoutに対してgravityを設定することもできます。gravityはView内の要素に対して位置を指定するもので、テキストの改行位置などを決めるためにTextViewでも指定できます。

↑の例にgravityを適用させるとどうなるか見てみましょう。LinearLayoutにgravity:centerを設定します。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
    <!-- 内部は同じなので省略 -->
</LinearLayout>

これのプレビューは以下になります。


全てのViewが中央寄せになりましたが、個別にlayout_gravityを設定しているは右寄せのまま、逆にもともと左寄せになっていたはLinearLayoutのgravityの設定により中央寄せに移動しました。

何も設定していなかったが中央寄せに移動するのは良いとして、が右寄せのままになっているのは、layout_gravity親のViewGroup(LinearLayout)のgravityの設定を上書きしているから、と考えられますね。

まとめ

今回はLinearLayoutについて解説しました。非常に高頻度に使われるとても使いやすいViewGroupなので、ぜひ使いこなせるようにしておきましょう。

参考

移行予定

どうも。どっことです。 タイトルの通りですが、諸事情により GitHubPage に移行予定です。 https://mkt120.github.io/ この備忘録に記載の内容を転記しつつ、今後はこちらのページを更新していく予定です。