プログラムを書こう!

実務や自作アプリ開発で習得した役に立つソフトウェア技術情報を発信するブログ

Androidで画面遷移する

この記事は2020年08月11日に投稿しました。

f:id:paveway:20190914064630j:plain

目次

  1. はじめに
  2. Androidで画面遷移する
  3. おわりに

1. はじめに

こんにちは、iOSのエディタアプリPWEditorの開発者の二俣です。
今回は業務で使用しているAndroidで画面遷移する方法についてです。

目次へ

2. Androidで画面遷移する

Androidで画面遷移するには、以下のような実装になります。

実装例

activity_main.xml
<?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">

    <Button
        android:id="@+id/subButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="サブ画面"/>

</LinearLayout>
activity_sub.xml
<?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">

    <Button
        android:id="@+id/mainButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="メイン画面"/>

</LinearLayout>

MainActivity.kt

package info.paveway.samplescreentransition

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // サブ画面ボタンにリスナーを設定します。
        val listener = SubButtonOnClickListener()
        val button = findViewById<Button>(R.id.subButton)
        button.setOnClickListener(listener)
    }

    // サブ画面ボタンがクリックされた時に呼び出されるリスナークラス
    private inner class SubButtonOnClickListener : View.OnClickListener {
        override fun onClick(view: View?) {
            // サブ画面を表示します。
            val intent = Intent(applicationContext, SubActivity::class.java)
            startActivity(intent)
        }
    }
}
SubActivity.kt
package info.paveway.samplescreentransition

import android.os.Bundle
import android.view.View
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class SubActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sub)

        // メイン画面ボタンにリスナーを設定します。
        val listener = MainButtonOnClickListener()
        val button = findViewById<Button>(R.id.mainButton)
        button.setOnClickListener(listener)
    }

    // メイン画面ボタンがクリックされた時に呼び出されるリスナークラス
    private inner class MainButtonOnClickListener : View.OnClickListener {
        override fun onClick(view: View?) {
            // この画面を終了する。
            finish()
        }
    }
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.paveway.samplescreentransition">

    <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>
        <!-- サブ画面の定義を追加します。 -->
        <activity android:name=".SubActivity"/>
    </application>

</manifest>

実行結果

メイン画面

f:id:paveway:20200811071559p:plain

サブ画面

f:id:paveway:20200811071309p:plain

目次へ

3. おわりに

今回はただ単にメイン画面からサブ画面、サブ画面からメイン画面に画面遷移する方法を紹介しました。

紹介している一部の記事のコードはGitlabで公開しています。
興味のある方は覗いてみてください。

目次へ


私が勤務しているニューラルでは、主に組み込み系ソフトの開発を行っております。
弊社製品のハイブリッドOS [Bi-OS][Bi-OS]は高い技術力を評価されており、特に制御系や通信系を得意としています。
私自身はiOSモバイルアプリウィンドウズアプリを得意としております。
ソフトウェア開発に関して相談などございましたら、お気軽にご連絡ください。

また一緒に働きたい技術者の方も随時募集中です。
興味がありましたらご連絡ください。

EMAIL : info-nr@newral.co.jp / m-futamata@newral.co.jp
TEL : 042-523-3663
FAX : 042-540-1688

目次へ