sp
(match the user font preference) For Textdp
(match the user screen pixel 1dp=1px) For Non-TextColor = Color.Blue
or using HEX code color = Color(0xFF[hex-code])
by
keyword in compose to import:kind of like LaunchEffect
but allow to use in non-composable function
Setup a speecher
Speak the text
In the above example the second paramter decided the TTS engine behavior when there's a new text to speak, there are mainly two option.
Usage | Description |
---|---|
TextToSpeech.QUEUE_FLUSH |
Speek immediately |
TextToSpeech.QUEUE_ADD |
Speek right after the current speech is done |
xml
file under res/xml/
withAndroidManifest.xml
HINT: android.appwidget.*
the widget is appwidget
BarWidget
is aGlanceAppWidget()
in this example
In Application Class
Declare a scope to run suspend action
Run the update widget method to update the widget
Remember to bind the application class in AndroidManifest.xml
and add the widget update method to onCreate()
to update the widget when app launch
Under MainAcvtivity
In Composable
Require android studio version above TIRAMISU
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
hasPermission
will be true
)Permission required
Remember to register the receier in the manifest
In this example android.permission.RECEIVE_BOOT_COMPLETED
is not necessary because we don't active the alarm when the device boot up
MainActivity
and set the alarm via alarm managerCalendar is a method for java to convert readable time into a calendar that can be convert to muti format
Article about calendar & alarm
object : TypeToken<List<Music>>() {}.type
(provide by gson) is for non-generic type
withContext(Dispatchers.IO)
to perform the request in backgroundplay()
play the mediapause()
pause the mediastop()
end the mediarelease()
unload playerrelease()
when UI unmountthe playlist in exoplayer is just a set of mediaitem
Add the media items array to the player
Use a player listener to retrive realtime feel meta data from player
SimpleDateFormat
DateTimeForamtter
assets/
/storage/emulated/0/Android/data/your.package.name/files/
/data/user/0/your.package.name/files/
we will call the External Root application file directory root dir
also you can use the Device Explore
in android studio to review edit all the file inside the android elmulator
null
in here mean get the root dir pathboolean
the page set in startDestination
will be displayed when the NavHost Component being render
navigate()
navigate to a screenpopBackStack()
navigate to the last screenExample:
You can use the ViewModel
without any dependencies, but get the limit lifetime (the ViewModel will be destory every time the composables where it declared being recompose or destory), so you have to place the viewModel at the MainActivity
where it won't destory or recompose during the run time
Install the dependencies will extend the life time of the viewModel
Add a class that inherit the viewModel
Call it in composable
function
or
Use ViewModel Factory when you need to pass a value into a ViewModel
context
to PlayerModel via viewmodel factoryremember
MainActivity
kapt
plugin at plugins section@Entity
)val name: String = "EliasChen"
then the default value of name will be "EliasChen"@Dao
)fun
’s data to @query
by adding :
at the start of the string. EX: :name
@Database
)context
from parent).fallbackToDestructiveMigration()
to drop old schema when DB migrateviewModel
for tableNOTE:
All the DB action (database
must be use in a suspend function
or a viewModelScope
)context
for databasedatabase
from MainActivity
and passing database
to the viewModel
that interact with the database
Save small data
JSONArray
JSONObject
JSONArray
or JSONObject
jsonObject[<index>]
or jsonObject.get<DataType>
.getJSONObject(<key>)
.getJSONObject(<index>)
Ex: the data.json
locale in assets/
data.json
@SerializedName
to custom the syntax gson will parsing from json.textContent
to get the value in the tagcontent://contacts/people/1
(accesses a specific contact with ID 1)content://media/external/audio/media/123
(points to an audio file with ID 123 in external storage)content://com.android.calendar/events
(accesses calendar events)file:///storage/emulated/0/Download/sample.pdf
(points to a PDF file in the Download folder)file:///android_asset/index.html
(accesses a file in the app’s assets folder)tel:5551234567
(initiates a phone call to the specified number)mailto:user@example.com
(opens an email client with the specified address)geo:37.7749,-122.4194
(opens a map at the specified coordinates)myapp://profile/user123
(a custom scheme to open a specific user profile in a custom app)
Usage
In project explorer under main/assets
add a HTML file, than use loadUrl("file:///android_asset/...")
to locate it.
NOTE: file:///android_asset
is point to main/assets
Though we need access to the internet, we have to enable the permission connect to the internet in AndroidManifest.xml
by adding:
Create a pager state
pager_state.currentPage
get current page
pager_state.animateScrollToPage(<Index>)
scroll to page (Place in coroutine scope
)
the toDp()
method is only available in Local
Animatable
is a value based animation method
Animatable()
inside (Also set a inital float
value)animateTo()
suspend method to animated value to the target valuesnapTo()
set the value to the target instantly without any animationRetrive widget layout (width, height)
Use .verticalScroll()
or .horizontalScroll()
to do that
After binding the state
use .animateScrollTo()
to scroll to a location with animation
requiredSize |
size |
---|---|
force to set the size no matter what | respect parent layout size if overflow then fit in the parent layout |
same as z-index
in css, it allow you to chagne the layer of the composables
weight()
take the remaining space of the composables
The RoundedCornerShape()
also accept something like CircleShape
(pretty useful)
gradient for different direction
Method | Direction |
---|---|
linearGradient |
topLeft -> bottomRight |
horizontalGradient |
Left -> Right |
verticalGradient |
Top -> Bottom |
Available syntax minWidth
minHeight
maxWidth
maxHeight
ButtonDefaults
allow you to custom the button color without rewrite all the color settingsstickyHeader
-> @OptIn(ExperimentalFoundationApi::class)
columns
is requiredMust calling snackBar in a coroutineScope
Add action
or duration
Combind with NavHost
But in this example we will focus on the BottomBar
To create a BottomBar for our app we need to create it in a Scaffold
Suggestion: Create a list for stroing navitems data then recall in NavigationBar
for more simple and effective code
So what the hell does shape do?
The segmented buttons only have rounded borders on the left for the first button and on the right for the last button. This requires you to provide the index and count so it knows which shape to render.
return a LocalTime
object