Some small projects to practice Android Basics with Kotlin
Some small projects to practice Android Basics with Kotlin for beginners. Every project in this repository is simplest. Besides making your own project following this, you should improve the UI for better look and feel, or maybe add more functionalities to upgrade your skill. Happy coding !
How to use:
Simple layout design with TableLayout
Handle click event to change button's attributes
Handle simple logic for game playing
References:
Multiple screens Instruction: These are some kind of screens that you have to support: ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi You have to do this for every Ativity that you need it to be supported.
Follow these steps:
Add new resource file in layout folder
Name that new file by the name of the root activity. And the directory shoud follow this convention: layout-ldpi or layout-xxxhdpi
Use Design tab and choose the device with suitable kind of screen to see your layout in that screen
Change the layout if needed
References:
References:
Not completed yet
References:
Problem with findViewById()
When our app has complex view hierarchies, findViewById()
function will slow down the app. Why? Given that you need to access to a View, which is nested in a nested nested nested ViewGroup...Uh ohh...
Android need to traverse all over the view herrachies from the root until get the needed one. If our app has so complex view, it is expensive to find a desired view. Is there a way for the View to update whenever the data changes and we don't need to go so far with expensive findViewById()
? Use Data Binding
Benefits from Data Binding
Follow these steps:
build.gradle
, add this blockbuildFeatures {
dataBinding true
}
<layout>
as a root viewprivate lateint var binding:ActivityMainBinding
setContentView()
with binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
findViewById()
with references to the view in binding object.
findViewById<Button>(R.id.done_button)
=> binding.doneButton
(Note: name of the view will be the id
in XML but is defined as camel case)<data>
block inside <layout>
tag<variable>
with name and type to the data class<data>
<variable
name="myName"
type="com.example.android.aboutme.MyName" />
</data>
MainActivity
, create value for our binding variable. Ex: private val myName: MyName = MyName("Hieu Vu")
binding.myName = myName
<data>
block. android:text="@={myName.name}"
Note: Whenever you change your data, remember to call invalidateAll()
method to notify the UI to refresh with new data.References:
When onPause()
is called, the app no longer has focus. After onStop()
, the app is no longer visible on screen
onCreate()
and onDestroy()
only run once
onCreate()
: where all your first-time initialization goes, where you set up the layout for the first time by inflating it, and where you initialize your variables, data binding...
onRestart()
method is a place to put code that you only want to call if your activity is not being started for the first time.
When our application starts, the onCreate()
, onStart()
and onResume()
run orderly
At the very first Activity, if we press Physical Back button
and move to the home screen of the device , onPause()
, onStop()
, onDestroy()
run orderly
If we reopen the app, it will call three methods onCreate()
, onStart()
and onResume()
again
If we press Home button
, onPause()
and onStop()
are called
If we reopen our app, the onRestart()
, onStart()
, onResume()
will be called
When we press the Tabs button
, onPause()
and onStop()
will be called
Then if we go back to our app, onRestart()
, onStart()
, onResume()
wil be called
Whatever code runs in onPause()
blocks other things from displaying, so keep the code in onPause()
lightweight. For example, if a phone call comes in, the code in onPause()
may delay the incoming-call notification.
onAttach()
: Called when the fragment is associated with its owner activity.
onCreate()
: Similarly to onCreate() for the activity, onCreate() for the fragment is called to do initial fragment creation (other than layout).
onCreateView()
: Called to inflate the fragment's layout.
onViewCreated()
: Called immediately after onCreateView() has returned, but before any saved state has been restored into the view.
onStart()
: Called when the fragment becomes visible; parallel to the activity's onStart().
onResume()
: Called when the fragment gains the user focus; parallel to the activity's onResume()
onPause()
: Called when the fragment loses the user focus; parallel to the activity's onPause().
onStop()
: Called when the fragment is no longer visible on screen; parallel to the activity's onStop().
onDestroyView()
: Called when the fragment's view is no longer needed, to clean up the resources associated with that view.
To add skeleton override methods to your classes in Android Studio, select Code > Override Methods or pressControl+o
.
Logging with Timber
A logging library better thant the default one. Benefits:
Generates the log tag for you based on the class name.
Helps avoid showing logs in a release version of your Android app.
Allows for integration with crash reporting libraries.
Steps to add Timber:
gradle.build
Application
classonCreate()
of new class, add Timber.plant(Timber.DebugTree())
android:name
with your new application class to <application>
tagTimber.i("Log example")
Kotlin Android Practice is licensed under a Creative Commons Attribution 4.0 International License. .
Based on a work at https://github.com/hieuwu/kotlin-android-practice.