Android
 Computer >> コンピューター >  >> プログラミング >> Android

Androidアプリでマテリアルデザインコンポーネントを使う方法をステップごとに解説

本記事では、AndroidアプリにGoogle公式のMaterial Design(マテリアルデザイン)コンポーネントを組み込む方法を、シンプルなログイン画面を題材にして段階的に解説します。マテリアルデザインを活用することで、影・色・動きを備えたモダンで統一感のあるUIを、少ない工数で実現できます。

ステップ1:Android Studioで新規プロジェクトを作成する

Android Studioを起動し、メニューから「File → New Project」を選択します。アプリ名やパッケージ名、保存先など必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。テンプレートは「Empty Activity」を選んでおくとスムーズです。

ステップ2:レイアウトファイル(activity_main.xml)を編集する

res/layout/activity_main.xmlを開き、以下のコードを記述します。ここでは、ロゴ画像・メールアドレス入力欄・パスワード入力欄・ログインボタン・プログレスバー・新規登録ボタンを、赤系の背景色の上に縦一列に配置したログイン画面を構築しています。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F95E5E"
    android:orientation="vertical"
    android:paddingTop="80dp"
    tools:context=".LoginActivity">

    <ImageView
        android:id="@+id/loginLogo"
        android:layout_width="240dp"
        android:layout_height="222dp"
        android:layout_gravity="center"
        android:contentDescription="@string/login_logo"
        android:paddingStart="8dp"
        android:paddingLeft="250dp"
        android:paddingTop="50dp"
        android:paddingEnd="8dp"
        android:src="@drawable/read_my_blog" />

    <EditText
        android:id="@+id/emailText"
        android:layout_width="350dp"
        android:layout_height="60dp"
        android:ems="10"
        android:autofillHints="@string/email_hint"
        android:hint="@string/email_hint"
        android:textColor="#FFFF"
        android:layout_gravity="center"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"/>

    <EditText
        android:id="@+id/pwdText"
        android:layout_width="350dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:textColor="#FFFF"
        android:layout_marginBottom="25dp"
        android:ems="10"
        android:hint="@string/pass_hint"
        android:inputType="textPassword"/>

    <Button
        android:id="@+id/buttonLogin"
        android:layout_width="260dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:backgroundTint="@color/colorPrimary"
        android:text="@string/login_btn_txt"
        android:textColor="@android:color/white"
        android:layout_marginBottom="20dp"/>

    <ProgressBar
        android:id="@+id/loginProgress"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="400dp"
        android:layout_height="20dp"
        android:layout_gravity="center"
        android:visibility="invisible"
        android:indeterminate="true"
        android:layout_marginBottom="30dp" />

    <Button
        android:id="@+id/buttonRegister"
        android:layout_width="260dp"
        android:layout_height="60dp"
        android:layout_gravity="center"
        android:backgroundTint="@android:color/white"
        android:text="@string/login_reg_btn_txt"
        android:textColor="@color/colorPrimary" />

</LinearLayout>

ステップ3:MainActivity.javaを実装する

続いて、src/MainActivity.javaに以下のコードを追加します。今回はレイアウトを表示するだけのシンプルな実装です。

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

ステップ4:マテリアルライブラリの依存関係を追加する

appモジュールのbuild.gradleを開き、dependenciesブロックにMaterial Componentsライブラリを追加します。これにより、マテリアルデザインの各種ウィジェットがプロジェクト内で利用できるようになります。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.app.sample"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    // マテリアルデザインライブラリ
    implementation "com.google.android.material:material:1.1.0-alpha02"
}

ポイント:上記の例ではバージョン「1.1.0-alpha02」を指定していますが、現在はより安定した新版(1.x系の最新版)がGoogle Mavenで公開されています。特別な理由がなければ最新版への更新を推奨します。また、Materialコンポーネントを正しく表示するには、アプリのテーマをTheme.MaterialComponents系(またはTheme.Material3)の子テーマとして定義しておく必要がある点にも注意してください。

ステップ5:AndroidManifest.xmlを確認する

Manifests/AndroidManifest.xmlに以下のコードを記述(または内容を確認)します。MainActivityがランチャーアクティビティとして正しく登録されているかをチェックしておきましょう。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.sample">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

アプリを実行して動作を確認する

それでは、アプリケーションを実行してみましょう。実機のAndroidスマートフォンをPCに接続していることを前提に説明します。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの「Run(実行)」アイコンをクリックしてください。デバイス選択ダイアログで接続した端末を選ぶと、スマートフォン側に以下のようなデフォルト画面が表示されます。

Androidアプリでマテリアルデザインコンポーネントを使う方法をステップごとに解説

まとめ

以上、Material DesignコンポーネントをAndroidアプリで利用するための基本手順を紹介しました。依存関係の追加さえ済ませれば、既存のEditTextやButtonも自動的にマテリアルスタイルへ置き換わり、手軽にUIの品質を高められます。さらにTextInputLayoutFloatingActionButtonなどの専用コンポーネントを組み合わせれば、よりリッチで操作性の高いインターフェースを構築できます。ぜひ実際に試してみてください。

  1. 【Android】CheckBox(チェックボックス)の使い方をサンプルコード付きで解説

    はじめにこの記事では、AndroidアプリでCheckBox(チェックボックス)を使用する方法を、実際のコード例とともにわかりやすく解説します。CheckBoxは、ユーザーに複数の項目を選ばせたい場合に便利なUI部品です。RadioButtonがグループ内で1つしか選択できないのに対し、CheckBoxは複数選択が可能という点が大きな特徴です。このチュートリアルでは、「ピザ」「コーヒー」「バーガー」の3つの商品を選択し、ボタンを押すと選択された商品と合計金額をToastで表示する、シンプルな注文アプリを作成します。手順1:Android Studioで新規プロジェクトを作成するAndroid

  2. Androidアプリでカスタムアラートダイアログを作成する方法をステップ別に解説

    はじめに この記事では、Androidアプリでカスタムアラートダイアログを作成する方法を、実際のコード例とともにステップ形式で解説します。標準のAlertDialogとは異なり、独自のレイアウトやデザインを自由に組み込んだダイアログを実装したい方に最適な内容です。 手順1:新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成してください。 手順2:activity_main.xmlにコードを追加する res/layout/activity_main.xml に以下