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

【Android】TextViewの文字色(フォントカラー)を変更する方法を解説

はじめに

この記事では、Androidアプリ開発においてTextViewの文字色(フォントカラー)を変更する方法を、実際のサンプルコードとともにステップ形式で解説します。Javaコードから動的に色を指定する方法を中心に紹介しますので、初心者の方でもそのまま試せます。

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

まず、Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。

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

次に、res/layout/activity_main.xml に以下のコードを追加します。ここでは、画面中央に2つのTextViewを縦に並べて配置しています。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My color is Blue"
        android:layout_centerInParent="true"
        android:textStyle="bold"
        android:textSize="36sp"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_marginBottom="10dp"
        android:text="My color is Green"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:textSize="36sp"/>

</RelativeLayout>

ステップ3:MainActivity.java を編集する

src/MainActivity.java に以下のコードを追加します。setTextColor() メソッドを使うことで、プログラムの実行時に文字色を動的に変更できます。

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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

        TextView textView1 = findViewById(R.id.textView1);
        textView1.setTextColor(Color.BLUE);

        TextView textView2 = findViewById(R.id.textView2);
        textView2.setTextColor(Color.parseColor("#006400"));
    }
}

文字色の指定方法には主に以下の2パターンがあります。

  • Color.BLUE などの定数を使う方法:Colorクラスにあらかじめ定義された色定数を指定します。
  • Color.parseColor() を使う方法:「#006400」のような16進数カラーコードの文字列を解析して色を指定します。任意の細かい色合いを表現したい場合に便利です。

補足: 新しいプロジェクトではサポートライブラリが非推奨となっているため、android.support.v7.app.AppCompatActivity の代わりに androidx.appcompat.app.AppCompatActivity を使用してください。

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

androidManifest.xml は以下のようになります。特別な権限は不要です。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.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スマートフォンがパソコンに接続されているものとします。Android Studioからアプリを起動するには、プロジェクト内のアクティビティファイルを開き、ツールバーの「Run」アイコンをクリックします。接続したモバイルデバイスを選択すると、スマートフォン上に青色と緑色のテキストが表示され、文字色が正しく変更されたことが確認できます。

【Android】TextViewの文字色(フォントカラー)を変更する方法を解説

補足:XMLだけで文字色を指定する方法

Javaコードを使わず、レイアウトXML内で android:textColor 属性を指定することでも文字色を変更できます。色が固定で問題ない場合は、こちらの方がシンプルです。

android:textColor="#006400"

状況に応じて、XMLでの静的な指定とJavaコードによる動的な変更を使い分けると、より柔軟なUI設計が可能になります。

  1. 【Android開発】カラー整数(int)を16進数カラーコード文字列に変換する方法

    Androidアプリ開発では、色情報がint型の整数値として扱われることが多く、これを「#FF5733」のような16進数のカラーコード文字列に変換したいケースがあります。本記事では、Integer.toHexString()メソッドを使ってカラー整数を16進文字列に変換する方法を、実際のサンプルプロジェクトとともにわかりやすく解説します。変換の仕組みAndroidでは、色はARGB形式(アルファ・赤・緑・青)の32ビット整数として表現されます。そのため、-16895234のような負の値も有効なカラー整数です。Integer.toHexString(int)で16進文字列に変換した後、先頭2桁は

  2. 【Android】ListViewの文字色とフォントを変更する方法をステップ解説

    本記事では、AndroidアプリにおけるListView(リストビュー)の文字色とフォントを変更する方法を、サンプルコードを交えながらステップ形式でわかりやすく解説します。 手順1:新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。 手順2:activity_main.xml を編集する res/layout/activity_main.xml に以下のコードを追加します。ここでは背景色をクリーム色(#fbf9ea)に設定し、ListViewを配置し