Skip to content

Instantly share code, notes, and snippets.

View abdulhafizramadan-ittp's full-sized avatar
👋
I may be slow to respond.

Abdul Hafiz Ramadan abdulhafizramadan-ittp

👋
I may be slow to respond.
  • Indonesia
View GitHub Profile
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / Navigation.kt
Created July 3, 2022 02:54
Android | Navigate to activity in other module
import android.content.Context
import android.content.Intent
fun Context.navigationToHomeModule() {
startActivity(Intent(this, Class.forName("com.ahr.home.HomeActivity")))
}
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / EmailValidation.kt
Last active November 11, 2022 14:05
Android | Validate email format
/**
* Return true if email format valid
* Return false if email format not valid
*/
fun validateEmail(email: String): Boolean {
return Patterns.EMAIL_ADDRESS.matcher(email).matches()
}
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / MainActivity.kt
Created June 2, 2022 14:17
Android | Show PopupMenu
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
...
}
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / NewsDao.kt
Created June 2, 2022 06:38
Android | Query a Raw in Room that return a LiveData<Boolean>
package com.ahr.newsapp.data.local.room
import androidx.lifecycle.LiveData
import androidx.room.*
import com.dicoding.newsapp.data.local.entity.NewsEntity
@Dao
interface NewsDao {
@Query("SELECT EXISTS(SELECT * FROM news WHERE title = :title)")
fun isNewsBookmarked(title: String): LiveData<Boolean>
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / MainDispatcherRule.kt
Last active May 31, 2022 03:15
Android | Testing Coroutines
package com.ahr.newsapp
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / Result.kt
Last active May 30, 2022 16:24
Android | Result Wrapper
package com.ahr.newsapp.data
sealed class Result<out R> private constructor() {
data class Success<out T>(val data: T) : Result<T>()
data class Error(val error: String) : Result<Nothing>()
object Loading : Result<Nothing>()
}
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / LiveDataTestUtil.kt
Last active May 30, 2022 16:21
Android | LiveData Test Utility
package com.ahr.newsapp
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
@Suppress("UNCHECKED_CAST")
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / Compress.kt
Last active May 30, 2022 16:21
Android | Compress File
private fun reduceFileImage(file: File): File {
val bitmap = BitmapFactory.decodeFile(file.path)
var compressQuality = 100
var streamLength: Int
do {
val bmpStream = ByteArrayOutputStream()
bitmap.compress(CompressFormat.JPEG, compressQuality, bmpStream)
val bmpPicByteArray = bmpStream.toByteArray()
streamLength = bmpPicByteArray.size
compressQuality -= 5
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / MainActivity.kt
Last active May 30, 2022 16:22
Android | Launch Intent Gallery
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.galleryButton.setOnClickListener { startGallery() }
@abdulhafizramadan-ittp
abdulhafizramadan-ittp / MainActivity.kt
Last active May 30, 2022 16:22
Android | Hide System Windows
class MainActivity : AppCompatActivity() {
private lateinit var root: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
root = findViewById(R.id.root)