From d58ffb87b139b3c656659ff2404e173505dae79a Mon Sep 17 00:00:00 2001 From: Monthira Chayabanjonglerd Date: Tue, 11 Oct 2022 18:03:20 +0700 Subject: [PATCH 1/5] update readme for add branch information --- README.md | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 58578cd..564233d 100644 --- a/README.md +++ b/README.md @@ -4,14 +4,21 @@ This repository is a part of AscendMoney's Basic Android Development Course. It is published for people who are interested to learn basic Android development. ## Preparation -Please fork this repository and download any required programs before starting the workshop. -Optional : Also prepare your Android device or Android Emulator for running the application. +Please fork this repository and download any required programs before starting the workshop. +Optional: Also prepare your Android device or Android Emulator for running the application. ### Checklist - [Android Studio](https://developer.android.com/studio) : Development Tool - Install Android SDK (Should be automatically downloaded when setup the Android Studio) -- [Create an Android Emulator](https://developer.android.com/studio/run/managing-avds) : For running the application without the real device -- [Connect with an Android Device](https://developer.android.com/studio/debug/dev-options) : If you have an Android Device +- [Create an Android Emulator](https://developer.android.com/studio/run/managing-avds): For running the application without the real device +- [Connect with an Android Device](https://developer.android.com/studio/debug/dev-options): If you have an Android Device + +### Branch +- [main](https://github.com/mikkipastel/android-basic): empty Android Application. +- [activity-lifecycle](https://github.com/mikkipastel/android-basic/tree/activity-lifecycle): application for learning about Android Lifecycle. +- [activity-workshop](https://github.com/mikkipastel/android-basic/tree/activity-workshop): workshop to learn for add activity. +- [fragment-workshop](https://github.com/mikkipastel/android-basic/tree/fragment-workshop): workshop to learn to add fragments. +- [recycler-workshop](https://github.com/mikkipastel/android-basic/tree/recycler-workshop): workshop to learn about RecyclerView and calling with API. ## Content -blog : https://developers.ascendcorp.com/basic-android-development-introduction-with-android-project-fbf754bb4d3a +blog introduction: https://developers.ascendcorp.com/basic-android-development-introduction-with-android-project-fbf754bb4d3a \ No newline at end of file From f0216f3d7e4879fb4b0fc2463133fd18cfd9af47 Mon Sep 17 00:00:00 2001 From: Yenei <> Date: Fri, 28 Oct 2022 14:56:45 +0700 Subject: [PATCH 2/5] Local storage sample and workshop --- app/src/main/AndroidManifest.xml | 5 +-- .../ascedncorp/androidbasic/MainActivity.kt | 4 +++ .../ascedncorp/androidbasic/MainFragment.kt | 26 ++++++++++++-- .../ascedncorp/androidbasic/SecondActivity.kt | 36 +++++++++++++++++++ app/src/main/res/layout/activity_second.xml | 31 ++++++++++++++++ app/src/main/res/layout/fragment_main.xml | 10 ++++++ app/src/main/res/values/strings.xml | 4 +++ 7 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/com/ascedncorp/androidbasic/SecondActivity.kt create mode 100644 app/src/main/res/layout/activity_second.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5fe1253..0985555 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,9 @@ android:supportsRtl="true" android:theme="@style/Theme.Androidbasic" tools:targetApi="31"> - + @@ -23,7 +25,6 @@ - \ No newline at end of file diff --git a/app/src/main/java/com/ascedncorp/androidbasic/MainActivity.kt b/app/src/main/java/com/ascedncorp/androidbasic/MainActivity.kt index a3f208e..7b74014 100644 --- a/app/src/main/java/com/ascedncorp/androidbasic/MainActivity.kt +++ b/app/src/main/java/com/ascedncorp/androidbasic/MainActivity.kt @@ -20,4 +20,8 @@ class MainActivity: AppCompatActivity() { .commit() } } + + companion object { + const val INPUT_TEXT_KEY = "com.ascedncorp.androidbasic.MainActivity.input_text_key" + } } \ No newline at end of file diff --git a/app/src/main/java/com/ascedncorp/androidbasic/MainFragment.kt b/app/src/main/java/com/ascedncorp/androidbasic/MainFragment.kt index 167ccf8..1d77c0a 100644 --- a/app/src/main/java/com/ascedncorp/androidbasic/MainFragment.kt +++ b/app/src/main/java/com/ascedncorp/androidbasic/MainFragment.kt @@ -1,5 +1,7 @@ package com.ascedncorp.androidbasic +import android.content.Context +import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -26,12 +28,32 @@ class MainFragment: Fragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - + // Get a preferences file of this activity + val preferences = activity?.getPreferences(Context.MODE_PRIVATE) + val sharedPreferences = activity?.getSharedPreferences( + BuildConfig.APPLICATION_ID, + Context.MODE_PRIVATE + ) + // Get last input value + binding.edittext.setText(preferences?.getString(MainActivity.INPUT_TEXT_KEY, null)) + // Set buttons' actions binding.button.setOnClickListener { + // Save the value when this button was clicked + preferences?.edit()?.apply { + putString(MainActivity.INPUT_TEXT_KEY, binding.edittext.text?.toString() ?: "") + apply() + } + sharedPreferences?.edit()?.apply { + putString(MainActivity.INPUT_TEXT_KEY, binding.edittext.text?.toString() ?: "") + apply() + } requireActivity().supportFragmentManager.beginTransaction() .add(R.id.container, SecondFragment.newInstance(binding.edittext.text.toString())) .addToBackStack(null) .commit() } + binding.buttonSecondActivity.setOnClickListener { + startActivity(Intent(requireActivity(), SecondActivity::class.java)) + } } -} \ No newline at end of file +} diff --git a/app/src/main/java/com/ascedncorp/androidbasic/SecondActivity.kt b/app/src/main/java/com/ascedncorp/androidbasic/SecondActivity.kt new file mode 100644 index 0000000..62665f4 --- /dev/null +++ b/app/src/main/java/com/ascedncorp/androidbasic/SecondActivity.kt @@ -0,0 +1,36 @@ +package com.ascedncorp.androidbasic + +import android.content.Context +import android.os.Bundle +import androidx.appcompat.app.AppCompatActivity +import com.ascedncorp.androidbasic.databinding.ActivitySecondBinding + +class SecondActivity : AppCompatActivity() { + + private val binding: ActivitySecondBinding by lazy { + ActivitySecondBinding.inflate(layoutInflater) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(binding.root) + // The Preferences won't be shared between activities. + val preferencesInput = getPreferences(Context.MODE_PRIVATE).getString( + MainActivity.INPUT_TEXT_KEY, + getString(R.string.second_activity_text) + ) + binding.tvPreferences.text = String.format( + getString(R.string.second_activity_preferences), + preferencesInput + ) + // The SharedPreferences will be shared to the whole application. + val sharedPreferencesInput = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE).getString( + MainActivity.INPUT_TEXT_KEY, + getString(R.string.second_activity_text) + ) + binding.tvSharedPreferences.text = String.format( + getString(R.string.second_activity_sharedpreferences), + sharedPreferencesInput + ) + } +} diff --git a/app/src/main/res/layout/activity_second.xml b/app/src/main/res/layout/activity_second.xml new file mode 100644 index 0000000..292b23f --- /dev/null +++ b/app/src/main/res/layout/activity_second.xml @@ -0,0 +1,31 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index 1fd54be..c4cd5ed 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -31,4 +31,14 @@ app:layout_constraintStart_toStartOf="@id/edittext" app:layout_constraintEnd_toEndOf="@id/edittext" /> +