【Android】SlidingDrawerを画面左側からスライド表示させる方法
このチュートリアルでは、AndroidアプリでSlidingDrawer(スライディングドロワー)を画面の左側からスライドアウトさせて表示する方法を、サンプルコード付きでわかりやすく解説します。
※補足:SlidingDrawerはAPIレベル17(Android 4.2)以降で非推奨(Deprecated)となっています。新規開発ではDrawerLayoutなどの利用が推奨されますが、既存コードの保守や仕組みの理解には依然として役立つ知識です。
手順1:新規プロジェクトを作成する
Android Studioを起動し、メニューからFile → New Projectを選択します。必要な項目をすべて入力して、新しいプロジェクトを作成しましょう。
手順2:activity_main.xmlにコードを追加する
res/layout/activity_main.xml に以下のコードを記述します。ポイントは、SlidingDrawer本体とコンテンツ領域に android:rotation="180" を指定することで、標準の下方向スライドを左方向スライドに変換している点です。ハンドルの矢印アイコンも android:rotation="270" で向きを調整しています。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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="4dp" tools:context=".MainActivity"> <SlidingDrawer android:id="@+id/slidingDrawer" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:handle="@id/handler" android:rotation="180" android:content="@+id/content"> <ImageView android:id="@+id/handler" android:layout_width="100dp" android:layout_height="100dp" android:src="@drawable/ic_arrow_upward_black_24dp" android:rotation="270" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:rotation="180" android:id="@+id/content" android:orientation="horizontal"> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image" /> </LinearLayout> </SlidingDrawer> </LinearLayout>
手順3:MainActivity.javaにコードを追加する
src/MainActivity.java に以下のコードを記述します。今回はレイアウトXMLだけで動作を実現できるため、Activity側は最小限のコードで問題ありません。
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: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)アイコン
をクリックしてください。接続したモバイル端末を選択すると、端末上に以下のような画面が表示されます。

-
Chromeアプリランチャーを最大限に活用する5つのテクニック
Chromeアプリランチャーは、Chromebookのオペレーティングシステムに標準で組み込まれている機能です。かつては2013年半ばにWindows版、同年末にMac版も登場しましたが、現在は主にChromebook上で活躍しています。 「Google版スタートメニュー」と呼ばれることもありますが、その役割は単にプログラムを起動することだけではありません。ウェブベースのさまざまな作業をより速く、簡単に、スムーズにしてくれる強力なツールなのです。しかし、その能力を最大限に引き出しているユーザーは実は少数派。ここでは、Chromeアプリランチャーをもっと便利に使いこなすためのヒントとテクニックを
-
Androidの主要レイアウト徹底解説:種類・特徴・使い分けのポイント
AndroidアプリのUI設計において、LinearLayout、RelativeLayout、ConstraintLayout、TableLayout、FrameLayout など、選択できるレイアウトは実に多彩です。では、どれを使うのが最適なのでしょうか?各レイアウトの詳細に入る前に、まずはViewオブジェクトの階層構造と、Androidの描画プロセスについて確認しておきましょう。この基礎知識があると、各レイアウトの挙動やパフォーマンス特性がぐっと理解しやすくなります。ViewとViewGroupViewGroupは、あらゆるViewの親クラスであり、同時にレイアウトの基底クラスでもありま