- エーオーシステム コーポレートサイト
https://www.aosystem.co.jp/ - エーオーシステム プロダクトサイト
https://ao-system.net/ - レンタルサーバー
- バーチャル展示会
- ウェブカタログサービス
- 3Dグラフィック
- Android アプリ
- iOS (iPhone,iPad) アプリ
- Flutter開発
- プログラミング記録QuickAnswer
- 無料画像素材
- スカイボックス 3D SKY BOX
このページのQRコード
下記アプリの主要なソースコードを公開しています。アプリ開発の参考になれば幸いです。
画像等が別途必要ですので下記情報のみでアプリが完成するものではありません。 アプリは少しずつ機能拡張していますのでストア公開されているアプリと内容が異なる場合があります。 コードはコピーして自由にお使いいただけます。ただし著作権は放棄しておりませんので全部の再掲載はご遠慮ください。部分的に再掲載したり、改変して再掲載するのは構いません。 自身のアプリ作成の参考として個人使用・商用問わず自由にお使いいただけます。 コード記述のお手本を示すものではありません。ミニアプリですので変数名などさほど気遣いしていない部分も有りますし間違いも有るかと思いますので参考程度にお考え下さい。 他の賢者の皆様が公開されているコードを参考にした箇所も含まれます。Androidアプリ開発の熟練者が書いたコードではありません。 エンジニア向け技術情報共有サービスではありませんので説明は省いています。ご了承ください。 GitHubなどへの公開は予定しておりません。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.32"
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.0'
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()
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "jp.aosystem.localhtmlbrowser"
minSdkVersion 21
targetSdkVersion 30
multiDexEnabled true
versionCode 5
versionName "1.4"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary true
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.gms:play-services-ads:20.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="jp.aosystem.localhtmlbrowser">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<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/Theme.LocalHtmlBrowser">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-0000000000000000~0000000000">
</meta-data>
</application>
</manifest>
package jp.aosystem.localhtmlbrowser
import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.os.LocaleList
import android.os.StrictMode
import android.os.StrictMode.VmPolicy
import android.text.InputType
import android.util.DisplayMetrics
import android.view.View
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import jp.aosystem.localhtmlbrowser.databinding.ActivityMainBinding
import java.util.*
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel: ConstraintLayout
private var localeLanguage: String = ""
//adMob
private lateinit var adView: AdView //adMob
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = this.binding.adContainer.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
companion object {
private const val AD_UNIT_ID: String = "ca-app-pub-0000000000000000/0000000000" //adMob
internal const val SETTINGS: String = "settings"
internal const val URI: String = "uri"
internal const val PIN: String = "pin"
internal const val LOCALE_LANGUAGE: String = "localeLanguage"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
this.binding = ActivityMainBinding.inflate(layoutInflater)
this.viewModel = this.binding.root
setContentView(this.viewModel)
//
supportActionBar?.hide() //タイトルバー非表示
//adMob
MobileAds.initialize(this) { }
this.adView = AdView(this)
this.binding.adContainer.addView(this.adView)
loadBanner()
//
val builder = VmPolicy.Builder() //ローカルハイパーリンクを有効にする
StrictMode.setVmPolicy(builder.build()) //ローカルハイパーリンクを有効にする
this.replaceFragment(FirstFragment())
}
//adMob
@SuppressLint("MissingPermission")
private fun loadBanner() {
this.adView.adUnitId = AD_UNIT_ID
this.adView.adSize = adSize
val adRequest = AdRequest
.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build()
this.adView.loadAd(adRequest)
}
fun onClickHome(v: View) {
this.replaceFragment(FirstFragment())
recreate()
}
fun onClickConfig(v: View) {
val pin: String = this.loadPin()
if (pin == "") {
this.replaceFragment(ConfigFragment())
} else {
val editPin = EditText(this)
editPin.inputType = InputType.TYPE_CLASS_NUMBER
AlertDialog.Builder(this).apply {
setTitle("PIN")
setView(editPin)
setPositiveButton("OK", DialogInterface.OnClickListener { _, _ ->
val inputPin = editPin.text.toString()
if (pin == inputPin) {
this@MainActivity.replaceFragment(ConfigFragment())
}
})
setNegativeButton("Cancel", null)
show()
}
}
}
override fun onBackPressed() {
val currentFragment = supportFragmentManager.findFragmentById(R.id.container1)
if (currentFragment is FirstFragment) {
if (currentFragment.webViewCanGoBack()) {
currentFragment.webViewGoBack()
} else {
super.onBackPressed()
}
}
}
//フラグメント差し替え
private fun replaceFragment(fragment: Fragment) {
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.container1, fragment)
fragmentTransaction.commit()
}
//PIN読み出し
private fun loadPin(): String {
val pref = this.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
return pref.getString(PIN, "").toString()
}
//----------------------------------------------
//言語設定
override fun attachBaseContext(base: Context) {
val pref = base.getSharedPreferences(SETTINGS, Context.MODE_PRIVATE)
this.localeLanguage = pref.getString(LOCALE_LANGUAGE, "") ?: ""
val loc: Locale? = if (this.localeLanguage != "") Locale(this.localeLanguage) else null
if (loc != null) {
val res = base.resources
val config = Configuration(res.configuration)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { //minSdkVersion 24
val localeList = LocaleList(loc)
LocaleList.setDefault(localeList)
config.setLocales(localeList)
} else { //minSdkVersion 17 16はダメ
config.setLocale(loc)
}
super.attachBaseContext(base.createConfigurationContext(config))
} else {
super.attachBaseContext(base)
}
}
//----------------------------------------------
}
package jp.aosystem.localhtmlbrowser
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebViewClient
import androidx.fragment.app.Fragment
import jp.aosystem.localhtmlbrowser.databinding.FragmentFirstBinding
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [FirstFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class FirstFragment : Fragment() {
private var _binding: FragmentFirstBinding? = null
private val binding get() = _binding!!
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_first, container, false)
_binding = FragmentFirstBinding.inflate(inflater, container, false)
return binding.root
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FirstFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic fun newInstance(param1: String, param2: String) =
FirstFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val uri: String = this.loadUri()
if (uri == "") {
val initMessage: String = getString(R.string.initmessage)
val prefix1: String = """<!doctype html><html><head><meta charset="utf-8">"""
val prefix2: String = "</head><body>"
val suffix: String = "</body></html>"
this.binding.webView1.loadDataWithBaseURL(
null,
prefix1 + prefix2 + initMessage + suffix,
"text/html",
"utf-8",
null
)
} else {
val activity: Activity? = activity as MainActivity?
if (activity != null) {
//this.binding.webView1.settings.domStorageEnabled = true
this.binding.webView1.settings.allowFileAccess = true //ファイルアクセス可
this.binding.webView1.settings.javaScriptEnabled = true
//this.binding.webView1.settings.setSupportZoom(true)
this.binding.webView1.settings.displayZoomControls = false //zoomコントロール非表示
this.binding.webView1.settings.builtInZoomControls = true //zoom可
//this.binding.webView1.settings.loadWithOverviewMode = true
//this.binding.webView1.settings.useWideViewPort = true
this.binding.webView1.webViewClient = WebViewClient() //新しいブラウザを開かない
//this.binding.webView1.setWebViewClient(object : WebViewClient() {
// //新しいブラウザを開かない
// override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
// return false
// }
//})
val context: Context = activity.applicationContext
//this.binding.webView1.loadUrl("file://" + context.getFilesDir() + "/" + uri)
this.binding.webView1.loadUrl("file://" + context.getExternalFilesDir(null) + "/" + uri)
}
}
}
// TIPS: fragmentではdestroyされる時にbindingをクリアにする処理が必要
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
//読み出し
private fun loadUri(): String {
val activity: Activity? = activity as MainActivity?
var uri: String = ""
if (activity != null) {
val pref = activity.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE)
uri = pref.getString(MainActivity.URI, "").toString()
}
return uri
}
fun webViewCanGoBack(): Boolean {
return this.binding.webView1.canGoBack()
}
fun webViewGoBack() {
this.binding.webView1.goBack()
}
}
package jp.aosystem.localhtmlbrowser
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.app.ActivityCompat.recreate
import androidx.fragment.app.Fragment
import jp.aosystem.localhtmlbrowser.databinding.FragmentConfigBinding
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [ConfigFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class ConfigFragment : Fragment() {
private var _binding: FragmentConfigBinding? = null
private val binding get() = _binding!!
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_config, container, false)
_binding = FragmentConfigBinding.inflate(inflater, container, false)
return binding.root
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment ConfigFragment.
*/
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(param1: String, param2: String) =
ConfigFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val activity: Activity? = activity as MainActivity?
if (activity != null) {
val context: Context = activity.applicationContext
//this.binding.textFilesDir.text = context.getFilesDir().toString() + "/"
this.binding.textFilesDir.text = context.getExternalFilesDir(null).toString() + "/"
}
this.loadConfig()
}
// TIPS: fragmentではdestroyされる時にbindingをクリアにする処理が必要
override fun onDestroyView() {
super.onDestroyView()
this.saveConfig()
_binding = null
}
//保存
private fun saveConfig() {
val activity: Activity? = activity as MainActivity?
if (activity != null) {
activity.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE).edit().apply {
putString(MainActivity.URI, this@ConfigFragment.binding.editUri.text.toString())
putString(MainActivity.PIN, this@ConfigFragment.binding.editPin.text.toString())
var localeLanguage: String = ""
if (this@ConfigFragment.binding.radioLanguageEn.isChecked) {
localeLanguage = "en"
} else if (this@ConfigFragment.binding.radioLanguageJa.isChecked) {
localeLanguage = "ja"
}
putString(MainActivity.LOCALE_LANGUAGE, localeLanguage)
apply()
}
}
}
//読み出し
private fun loadConfig() {
val activity: Activity? = activity as MainActivity?
if (activity != null) {
val pref = activity.getSharedPreferences(MainActivity.SETTINGS, Context.MODE_PRIVATE)
val uri: String = pref.getString(MainActivity.URI, "").toString()
this.binding.editUri.setText(uri)
val pin: String = pref.getString(MainActivity.PIN, "").toString()
this.binding.editPin.setText(pin)
val localeLanguage: String = pref.getString(MainActivity.LOCALE_LANGUAGE,"").toString()
when (localeLanguage) {
"en" -> this.binding.radioLanguageEn.isChecked = true
"ja" -> this.binding.radioLanguageJa.isChecked = true
else -> this.binding.radioLanguageSystem.isChecked = true
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="horizontal">
<ImageButton
android:id="@+id/imageButton"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:contentDescription="Home"
android:onClick="onClickHome"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_home" />
<View
android:id="@+id/divider"
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/cardview_dark_background" />
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_weight="1"
android:background="@android:color/transparent"
android:contentDescription="Config"
android:onClick="onClickConfig"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:scaleType="fitCenter"
app:srcCompat="@drawable/ic_gear" />
</LinearLayout>
<LinearLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"></LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/ad_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"></LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
tools:context=".FirstFragment">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<?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:layout_marginTop="5dp"
tools:context=".ConfigFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/rootdirectory" />
<TextView
android:id="@+id/textFilesDir"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/toppage" />
<EditText
android:id="@+id/editUri"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textUri"
android:importantForAutofill="no" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/toppage01"
android:textColor="@android:color/secondary_text_dark" />
<TextView
android:id="@+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/toppage02"
android:textColor="@android:color/secondary_text_dark" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/toppage03"
android:textColor="@android:color/secondary_text_dark"
android:textSize="12sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="@string/pin" />
<EditText
android:id="@+id/editPin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:importantForAutofill="no" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/pin01"
android:textColor="@android:color/secondary_text_dark" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/pin02"
android:textColor="@android:color/secondary_text_dark"
android:textSize="12sp" />
<View
android:id="@+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="30dp"
android:background="?android:attr/listDivider" />
<TextView
android:id="@+id/textLanguage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/language"
android:textColor="?android:attr/textColorPrimary" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="@+id/radioLanguageSystem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/languageSystem" />
<RadioButton
android:id="@+id/radioLanguageEn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/languageEn" />
<RadioButton
android:id="@+id/radioLanguageJa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/languageJa" />
</RadioGroup>
<View
android:id="@+id/divider3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:background="?android:attr/listDivider" />
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp" />
</LinearLayout>
</ScrollView>
</FrameLayout>
このページのQRコード
便利ウェブサイト
便利 Android アプリ
便利 iOS(iPhone,iPad) アプリ