ดูไฟล์ต่างๆเมื่อสร้างโปรเจ็กส์ใหม่แบบ Empty Activity
- AndroidManifest.xml
- build.gradle (Project: MyApplication)
- build.gradle (Module: app)
- activity_fullscreen.xml
- values/attrs.xml
- values/colors.xml
- values/strings.xml
- values/styles.xml
- FullscreenActivity.kt
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.phaisarn.myapplication"> <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=".FullscreenActivity" android:configChanges="orientation|keyboardHidden|screenSize" android:label="@string/app_name" android:theme="@style/FullscreenTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
build.gradle (Project: MyApplication)
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = '1.3.31' repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.4.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
build.gradle (Module: app)
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 29 buildToolsVersion "29.0.0" defaultConfig { applicationId "com.phaisarn.myapplication" minSdkVersion 21 targetSdkVersion 29 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 fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'androidx.core:core-ktx:1.0.2' implementation 'androidx.legacy:legacy-support-v4:1.0.0' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
activity_fullscreen.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0099cc" tools:context=".FullscreenActivity"> <!-- The primary full-screen view. This can be replaced with whatever view is needed to present your content, e.g. VideoView, SurfaceView, TextureView, etc. --> <TextView android:id="@+id/fullscreen_content" android:layout_width="match_parent" android:layout_height="match_parent" android:keepScreenOn="true" android:textColor="#33b5e5" android:textStyle="bold" android:textSize="50sp" android:gravity="center" android:text="@string/dummy_content"/> <!-- This FrameLayout insets its children based on system windows using android:fitsSystemWindows. --> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <LinearLayout android:id="@+id/fullscreen_content_controls" style="?metaButtonBarStyle" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="@color/black_overlay" android:orientation="horizontal" tools:ignore="UselessParent"> <Button android:id="@+id/dummy_button" style="?metaButtonBarButtonStyle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/dummy_button"/> </LinearLayout> </FrameLayout> </FrameLayout>
values/attrs.xml
<resources> <!-- Declare custom theme attributes that allow changing which styles are used for button bars depending on the API level. ?android:attr/buttonBarStyle is new as of API 11 so this is necessary to support previous API levels. --> <declare-styleable name="ButtonBarContainerTheme"> <attr name="metaButtonBarStyle" format="reference"/> <attr name="metaButtonBarButtonStyle" format="reference"/> </declare-styleable> </resources>
values/colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#008577</color> <color name="colorPrimaryDark">#00574B</color> <color name="colorAccent">#D81B60</color> <color name="black_overlay">#66000000</color> </resources>
values/strings.xml
<resources> <string name="app_name">My Application</string> <string name="dummy_button">Dummy Button</string> <string name="dummy_content">DUMMY\nCONTENT</string> </resources>
values/styles.xml
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="FullscreenTheme" parent="AppTheme"> <item name="android:actionBarStyle">@style/FullscreenActionBarStyle</item> <item name="android:windowActionBarOverlay">true</item> <item name="android:windowBackground">@null</item> <item name="metaButtonBarStyle">?android:attr/buttonBarStyle</item> <item name="metaButtonBarButtonStyle">?android:attr/buttonBarButtonStyle</item> </style> <style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar"> <item name="android:background">@color/black_overlay</item> </style> </resources>
FullscreenActivity.kt
package com.phaisarn.myapplication import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.view.View import kotlinx.android.synthetic.main.activity_fullscreen.* /** * An example full-screen activity that shows and hides the system UI (i.e. * status bar and navigation/system bar) with user interaction. */ class FullscreenActivity : AppCompatActivity() { private val mHideHandler = Handler() private val mHidePart2Runnable = Runnable { // Delayed removal of status and navigation bar // Note that some of these constants are new as of API 16 (Jelly Bean) // and API 19 (KitKat). It is safe to use them, as they are inlined // at compile-time and do nothing on earlier devices. fullscreen_content.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION } private val mShowPart2Runnable = Runnable { // Delayed display of UI elements supportActionBar?.show() fullscreen_content_controls.visibility = View.VISIBLE } private var mVisible: Boolean = false private val mHideRunnable = Runnable { hide() } /** * Touch listener to use for in-layout UI controls to delay hiding the * system UI. This is to prevent the jarring behavior of controls going away * while interacting with activity UI. */ private val mDelayHideTouchListener = View.OnTouchListener { _, _ -> if (AUTO_HIDE) { delayedHide(AUTO_HIDE_DELAY_MILLIS) } false } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_fullscreen) supportActionBar?.setDisplayHomeAsUpEnabled(true) mVisible = true // Set up the user interaction to manually show or hide the system UI. fullscreen_content.setOnClickListener { toggle() } // Upon interacting with UI controls, delay any scheduled hide() // operations to prevent the jarring behavior of controls going away // while interacting with the UI. dummy_button.setOnTouchListener(mDelayHideTouchListener) } override fun onPostCreate(savedInstanceState: Bundle?) { super.onPostCreate(savedInstanceState) // Trigger the initial hide() shortly after the activity has been // created, to briefly hint to the user that UI controls // are available. delayedHide(100) } private fun toggle() { if (mVisible) { hide() } else { show() } } private fun hide() { // Hide UI first supportActionBar?.hide() fullscreen_content_controls.visibility = View.GONE mVisible = false // Schedule a runnable to remove the status and navigation bar after a delay mHideHandler.removeCallbacks(mShowPart2Runnable) mHideHandler.postDelayed(mHidePart2Runnable, UI_ANIMATION_DELAY.toLong()) } private fun show() { // Show the system bar fullscreen_content.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION mVisible = true // Schedule a runnable to display UI elements after a delay mHideHandler.removeCallbacks(mHidePart2Runnable) mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY.toLong()) } /** * Schedules a call to hide() in [delayMillis], canceling any * previously scheduled calls. */ private fun delayedHide(delayMillis: Int) { mHideHandler.removeCallbacks(mHideRunnable) mHideHandler.postDelayed(mHideRunnable, delayMillis.toLong()) } companion object { /** * Whether or not the system UI should be auto-hidden after * [AUTO_HIDE_DELAY_MILLIS] milliseconds. */ private val AUTO_HIDE = true /** * If [AUTO_HIDE] is set, the number of milliseconds to wait after * user interaction before hiding the system UI. */ private val AUTO_HIDE_DELAY_MILLIS = 3000 /** * Some older devices needs a small delay between UI widget updates * and a change of the status and navigation bar. */ private val UI_ANIMATION_DELAY = 300 } }