My everyday Android practice demos with Kotlin in 100(far and far more...) days.
Date: 2018-8-10
What I learned from this demo:
- Make a simple image puzzle game with bitmap data operations: scale, draw, clip and so on
- Use the powerful
BRVAH
library to make a simpleRecyclerView
with XML layout - Now remove the support library and migrate to
androidX
packages, so Anko layout is not supported right now
PS: Restart(cold reboot) simulator to make the downloaded images appear in gallery.
# while build failed with this problem:
# Cause: delight/rhinosandox/RhinoSandboxes
# Try run the command in terminal:
gradlew clean assemble -stacktrace
Resource: How to Build a Jigsaw Puzzle Android Game
Date: 2018-7-26
What I learned from this demo:
- Build simple game in Android with
libgdx
framework - Manage multi scenes in the game with
Stages
andCamera
andSprites
- Use
Box2D
to do simple physics simulation in the game
Date: 2018-7-25
What I learned from this demo:
- Basic unit test with
JUnit4
, andEspresso
test framework in Android - Use
mockK
to do Kotlin unit test: mock, spy, function callback captures and verify, ect - Simple UI test in Android with
Espresso
andbarista
library: find view, click,recyclerview
test and waiting and so on
Resource: mockK
Date: 2018-5-17
What I learned from this demo:
- Create
Service
in Android app withIBinder
, send notification background - Use
Guideline
andBarrier
inConstraintLayout
with Anko Layout - Switch threads in
RxAndroid
, and indoOnComplete
PS: To correctly use Guideline
in constraint layout as the item ui in RecyclerView
, I add the code bellow to make sure the layout renders expectedly:
constraintLayout {
//Importance!
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
guideline {
id = ID_GUIDELINE
}.lparams(width = matchConstraint, height = matchConstraint){
orientation = ConstraintLayout.LayoutParams.VERTICAL
guidePercent = 0.25f
}
//... other code
}
Date: 2018-5-2
What I learned from this demo:
- Create
Observable
from buttons and text changes, and merge them together - Switch work threads, use
doOnNext
to show progress bar, and dispose them when needed - Use arrays in XML resource, change the button appearance use the theme of
Widget.AppCompat.Button.Colored
Date: 2018-3-11
What I learned from this demo:
- Basic material animations for Fragment: enter/exit transition, shared element trasition
- Use coroutines to do time-consuming process:
async
,await
inlaunch
coroutines scope - Build animations in
ConstraintLayout
use simple and powerfulConstraintSet
andTransition
- Single activity as a container with multiple fragments
Resource: Android KTX library
Keyframe Animations with ConstraintLayout and ConstraintSet
PS: While use shared element between fragments, I notice that, the 1st: add
will not work but replace
works, and 2nd: I set transitionName
in the CardView
instead of directly in the ImageView
, which is the sub-view of CardView
, that works!
Date: 2018-3-7
What I learned from this demo:
- Work with ObjectBox to do the operations of databases
- Try to use Kotlin Coroutines to simulate some time-consuming process, it's just a test
- Create the custom
Application
, use open source toast:Toasty
, useNestedScrollView
inCoordinatorLayout
Resource: ObjectBox API A test project, lost of functionalities are not accomplished yet.
Date: 2018-3-1
What I learned from this demo:
- Use the basic MVP pattern in Android project, to make code clean and testable
- Understand and use the abstract class and abstract methods
- The data binding in view, use this library:
KotlinAndroidViewBindings
(I just copied some code in my project.)
Resource: KotlinAndroidViewBindings
Date: 2018-2-6
What I learned from this demo:
- Try with
Retrofit
andRxAndroid
/RxJava
to populate data in web service - No XML layout now, try to work with pure
Anko
layouts, and the awesomeAnko ConstraintLayout
library - How to change the layout in
RecyclerView
, and smooth scroll to the desired position - Anko layout with
Coordinator Layout
,CardView
,AppBar Layout
,Toolbar
and so on indesign
support library - Pre-load or pause-load behavior in
Glide
image loader withRecyclerView
while scrolling
Resource: Anko ConstraintLayout Glide: RecyclerView integration library
Date: 2018-1-29
What I learned from this demo:
- It works like a charm!!! Five days cost to finish my little simple
ViewGroup
! That deserves. - Got the basic idea of the processes of
measure
,layout
,touchEvent
,interceptEvent
, and custom child layout parameters - Use the
scroller
to make the movement much smooth - Let the
ViewGroup
support click event and long click events
Date: 2018-1-23
What I learned from this demo:
- Show map, display marker, animate camera, search, deal with the latitude and longitude with A Map API
- Fullscreen control, show and hide the soft keyboard
- Work with the
AutoCompleteTextView
widget
Resource: A MAP
Date: 2018-1-21
What I learned from this demo:
- Custom view with basic workflow: measure, layout, draw and touch event handle
- Add custom properties or attributes in XML of view
- The basics of drawing api in Android with: path, paint, canvas
Date: 2018-1-11
What I learned from this demo:
- No XML layouts, the UI is completely replaced with Anko layout codes
- Work with assets in Android, and use
AssetManager
to do asset management - Use
SoundPool
to play tinywav
audio files - The basic knowledge of Anko layouts, pure code to make views
Date: 2018-1-8
What I learned from this demo:
- Use RecylcerView list item animations with XML resource
- Understand the
layout_behavior
inCoordinatorLayout
andlayout_anchor
property - Try to use Fragment instead the Activity for better practise, it makes ViewPager very esay to build
- Work with the alert dilog with custom view
The refactor is not so easy for me, a lot of functionalities is not finished, and I will try to get that done later.
Date: 2017-12-29
What I learned from this demo:
- Work with OKHttp to post data: upload files, get and send cookies, retrive the result
- Use XML Shape to create nice controls such as buttons
- Crop image and get the bitmap data through
FileProvider
in API greater than 24 - Use view animations, Gson to analysis json string,
indeterminateProgressDialog
andalert
in anko
Realy thanks to the guy in Weixin group: @���е���ؤ���� for solving my code problem, while reuse the animation XML will cause wried things!
Date: 2017-12-26
What I learned from this demo:
- Get images throught network, decode stream and display
- Use
LruCache
to cache the bitmap data for quick loading and recylcling data usage - The same Activity is setted as both the Detail activity and Add New activity
- Load data from database in the asynchronized way
If too much photos loaded, will the app get the changce to crash for OOM? I should try to figure it out!
Date: 2017-12-23
What I learned from this demo:
- Use
Parcelable
objects for data passing between activities - Try Android unit test with: Instrumented Test and Unit Test
- Data class used both as Parcelable data object and Room database entity
- Solve two problems through StackOverFlow.com: Gradle error, failed to create directory and Empty test suite
A lot of functionalities are not implemeted yet in this app, and I will finish that in my next days, hold on please! :)
Date: 2017-12-16
What I learned from this demo:
- Work with
ViewPager
andTabLayout
together to display walk through pages - Use
FragmentStatePagerAdapter
as theViewPager
adapter and, the fragments - Finish the activity so that make sure it is removed from the stack
Resource: ViewPager Tutorial: Getting Started in Kotlin
Date: 2017-12-16
What I learned from this demo:
- No layout XML files, but replace with the anko layout library to create views
- Use
WindowManager
to display floating views on the window - Deal with the back button pressed to hide the activity, prevent from finishing the app
After 2 months at last! Still 2 problems or questions:
1. If back pressed and app is killed, then the android.view.WindowLeaked
exception throws, how to solve?
2. In the emulator, if the drag and drop event happens outside, then the position of the window will be some wried.
Date: 2017-10-18
What I learned from this demo:
- Work with WebView, set the client as
WebViewClient
andWebChromeClient
- Use the ToolBar instead of ActionBar, try
SearchView
in the tool bar - Save and retrieve list items (
StringSet
) in shared preferences - Add or remove item in
RecyclerView
, handle long click events on list item - Deal with the WebView in NestedScrollView through the library:
NestedScrollWebView
in Github
Resource: NestedScrollWebView
Date: 2017-10-14
What I learned from this demo:
- Use the basic notifications in Android
- The asynchronized task with
doAsync
anduiThread
in Kotlin - Basic file and stream operations in Android
Date: 2017-10-4
What I learned from this demo:
- Use SharedPreferences to store and retrieve simple data
- Basic file operations on Android system, with
openFileInput
andopenFileOutput
- Set data in the activity result and return
Date: 2017-09-27
What I learned from this demo:
- Use one of the most four important components in Android: Broadcast Receiver
- Extension functions in Kotlin with
AppCompatActivity
- Try the open-source library: EventBus to post and handle events, and also the CircleImageView
- Reduce the redundant of layout by using
<inlucde>
tags
I am still not very clear with Android BroadcastReceiver
, I found that it sometimes(especially for the single app development) can be replaced with EventBus
through publish/subscribe pattern, is that right?
Date: 2017-09-12
What I learned from this demo:
- Use the Fragments in the Activities
- Specify different layouts for different size or screen orientation (layout-land and layout-large)
- Use the empty view element as spacer or divider (be careful of the tag, it is
View
notview
!) - Dynamically set the visibility of view
Till now I have no idea of using the savedInstanceState
variable to make the data consistent while rotate the screen orientation, I should try it out later.
Date: 2017-09-04
What I learned from this demo:
- Use the Room Persistence Library for the basic database operations: INSERT/DELETE/UPDATE/QUERY
- Work with data class in Kotlin and the
let
lamda, and the annotations - Convert date types to string types, and vice versa by using
SimpleDateFormat
orDateFormat.getDateInstance()
- Use
adb shell
andsqlite3
command to query the data in emulator local system files:
$ adb quit
# for more than one device found here
$ adb devices
$ adb -s <emulator name> shell
$ cd data/data/me.liuqingwen.android.projectdatabaseroom/databases
$ sqlite3
$ .open <dbname.db>
$ .tables
$ SELECT * FROM <table name>
Date: 2017-09-01
What I learned from this demo:
- Transitions between activities use
overridePendingTransition
- Work with animation xml resource file, the difference of "50%" and "50%p" (relative to parent view)
- Override
onBackPressed
method to finish the activity
Date: 2017-08-31
What I learned from this demo:
- Dealing with the custom item click handler of RecyclerView
- Start an intent to show another Activity by
startActivityForResult
- Get the result from another activity through method of
onActivityResult
Date: 2017-08-30
What I learned from this demo:
- Custom view with custom attributes (declared in the xml value file)
- Use
onMeasure
to set the correct size of view - Use
onDraw
to display the paint on the canvas - Work with the basic object animator and animator-set
- The importance of lazy properties in Kotlin, think about the code:
//the [sunColor] initialized here will be changed later in the constructor through xml user attributes.
private var sunColor = Color.RED
/*
//the paint directly initialized will not be the expected one, as the [sunColor] will change later for xml attributes!
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
this.color = [email protected]
this.isAntiAlias = true
}
*/
//by using the lazy properties, the paint will be all right with the [sunColor] property!
private val paint by lazy {
Paint(Paint.ANTI_ALIAS_FLAG).apply {
this.color = this@SunView.sunColor
this.isAntiAlias = true
}
}
Date: 2017-08-28
What I learned from this demo:
- Multiple constructors from base class inheritance in Kotlin
- The basic knowledge of custom view: onLayout (left, right, top, bottom)/setFrame/onTouchEvent (Here I should always return
true
for receiving other touch events) - Multi-touch handle on views: use
event.actionMasked
instead ofevent.action
This is a bad-experienced project(view), I think I have to improve that in the next days while learning.
Date: 2017-08-17
What I learned from this demo:
- Try to figure out the differences of usage between Handler and AsyncTask in Android
- Download file from server via OkHttp and read bytes from InputStream
- RandomAccessFile for writing file content from specified positions
Till now I can't figure out a solution to pause/resume the downloading tasks, and I will try it later.
Date: 2017-08-16
What I learned from this demo:
- Work with SwipeRefreshLayout and RecyclerView
- Basic AlertDialog and Snackbar usage
- Custom header and footer item in RecyclerView (It looks a little weird!)
Date: 2017-08-14
What I learned from this demo:
- The first time explore the MediaPlayer with SurfaceView
- Got idea of LayoutParams and its simple properties (ConstraintLayout.LayoutParams)
Date: 2017-08-12
What I learned from this demo:
- Basic material design elements: DrawerLayout, NavigationView, CoordinatorLayout, AppBarLayout with Toolbar and FloatingActionButton
- Try to use the open source Android libraries of RxAndroid, OkHttp, Gson and Glide, ect.
- DrawerLayout must work with a child with
layout_gravity
property specified. - Gson tokens with types:
object : TypeToken<List<Turns>>() {}.type
is the right way.
Resource: RxAndroid, OkHttp, Gson, Glide
Date: 2017-08-11
What I learned from this demo:
- CardView is very cute, isn't is? (I hate the gap between image nad text!)
- Load json data through HttpURLConnection and display view with ViewStub
- Use
doAsync{}
anduiThread{}
to do asynchronized task in Kotlin - Read InputStream in Kotlin and convert string data to JSONObject
Date: 2017-08-09
What I learned from this demo:
- The very basics of RecycleView
- RecycleView with custom ViewHolder and Adapter, and layout managers
- Using
lazy
delegates , andPair<out A, out B>
in Kotlin
In fact it shows the unexpected results, but I will give more effort to the RecycleView app next time.
Date: 2017-08-08
What I learned from this demo:
- Start a intent and get the result from that
- Work with local images and camera basics
- Display bitmap data on an ImageView
Date: 2017-08-06
What I learned from this demo:
- Work with LocationManager, get GPS providers and locations
- Got to know how to request specified Permissions at RunTime
- Use HttpUrlConnection to fetch data with url and read the input streams
- The LAMDA of
thread
andrunOnUiThread
My network is not stable, and I really do a lot of hard-code, I think I can fix that later.
Date: 2017-08-05
What I learned from this demo:
- Work with EditText and SeekBar
- The editor of keyboard event handler with EditText
- Strings to formatted floats and strings remove specified prefix in kotlin
And what I cannot resolve is the focus changes of EditText and auto-hided of keyboard, I hope I can work it later.
Date: 2017-08-04
What I learned from this demo:
- Delegates of property in Kotlin
- Basic usage of Timer and TimerTask
- Button long click and touch event listener
- AnkoLogger for test (I have deleted the test code, but the activity has implemented the interface)
Date: 2017-08-03
What I learned from this demo:
- Menu resource file creation and option menus add to title bar
- Use anko library to reach UI elements easily in layout
- Button click handler with lambda in kotlin
- Change the button and text view appearance in editor
- Basic usage of Git commands and Github repository with AS 3.0
Resource: anko