プログラムからAndroidデバイスの画面サイズカテゴリ(小・通常・大・特大)を判定する方法
はじめに
この記事では、Androidアプリ内でプログラムから端末の画面サイズカテゴリ(small:小 / normal:通常 / large:大 / xlarge:特大)を動的に判定する方法を解説します。あわせて、画面密度(density)を取得して分類する方法もご紹介します。
画面サイズカテゴリの判定には Configuration.SCREENLAYOUT_SIZE_MASK を使用し、getResources().getConfiguration().screenLayout の値とビット演算を行うことで、現在の端末がどのカテゴリに属するかを確認できます。
実装手順
ステップ1:新規プロジェクトの作成
Android Studioで新しいプロジェクトを作成します。メニューから「File」→「New Project」を選択し、必要な項目を入力してプロジェクトを作成してください。
ステップ2:レイアウトファイルの作成
res/layout/activity_main.xml に以下のコードを追加します。画面サイズを取得するボタンと、画面密度を取得するボタン、そして説明用の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"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGetScreenSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="GetScreenSize"
android:text="Get ScreenSize" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnGetScreenSize"
android:layout_centerInParent="true"
android:layout_marginTop="10dp"
android:onClick="GetScreenDensity"
android:text="Get Screen Density" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text="Android device screen size category (small, normal, large, xlarge)"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold|italic" />
</RelativeLayout>ステップ3:MainActivityの実装
src/MainActivity.java に以下のコードを追加します。GetScreenSize() メソッドでは画面サイズカテゴリを判定し、GetScreenDensity() メソッドではDisplayMetricsを使って画面密度を判定してToastで表示します。
import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void GetScreenSize(View view) {
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show();
}
}
public void GetScreenDensity(View view) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this, "DENSITY_HIGH... Density is " + (density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this, "DENSITY_MEDIUM... Density is " + (density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this, "DENSITY_LOW... Density is " + (density), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + (density), Toast.LENGTH_LONG).show();
}
}
}なお、上記コードでは「large」「normal」「small」のいずれにも該当しない場合のelse処理がありますが、これは「xlarge(特大)」などのカテゴリが該当します。より厳密に判定したい場合は、Configuration.SCREENLAYOUT_SIZE_XLARGE との比較を追加することをおすすめします。
ステップ4:マニフェストファイルの設定
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」アイコンをクリックします。表示される選択肢から自分のモバイルデバイスを選択すると、端末に以下のようなデフォルト画面が表示されます。

各ボタンをタップすると、Toast(トースト)によって現在の端末の画面サイズカテゴリや画面密度が表示されます。これにより、端末ごとの画面特性に応じたレイアウトやUIの切り替えなど、柔軟なアプリ開発が可能になります。
-
Androidの画面をテレビに映す方法6選!ChromecastからHDMI接続まで徹底解説
Androidデバイスで見ている動画や画像を、もっと大きな画面で楽しみたいと思ったことはありませんか?Androidの画面をテレビやPCにキャスト(ミラーリング)すれば、大画面での視聴が実現します。一部の方法ではAndroidデバイスがMiracastに対応している必要がありますが、選択肢はそれだけではありません。この記事では、初心者でも簡単にできる6つの方法を詳しく解説します。YouTubeやNetflixなどの動画を、快適な大画面で楽しんでみましょう。1. Chromecastを使うGoogleが推奨しているのは、Chromecastを使ったキャスト方法です。お使いのAndroidデバイス
-
Androidの画面が回転しないときの対処法7選|自動回転しない原因と解決策
横向きで動画や写真を見たいのに、Androidの画面が回転してくれない——そんなトラブルに悩まされていませんか?画面が回転しない原因は、主に「画面設定」「センサーの不具合」「ソフトウェア関連の問題」の3つに分けられます。この記事では、Androidの画面が自動回転しない問題を解決するための7つの対処法を、初心者にもわかりやすく順番に紹介します。ぜひ最後まで読んで、お使いの端末に合った解決策を見つけてください。 Androidの画面が回転しないときの対処法7選 以下に、簡単なトラブルシューティングの手順で画面が回転しない問題を解決する方法を紹介します。 方法1:端末を再起動する 最もシンプル