Skip to content

Instantly share code, notes, and snippets.

View mduccc's full-sized avatar
🏠
Working from home

Minh Duc mduccc

🏠
Working from home
View GitHub Profile
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:onClick="@{() -> RegisterViewModel.onCLicked()}"
bind:toastMessageAction="@{RegisterViewModel.toastMessage}"/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:onClick="@{() -> RegisterViewModel.onCLicked()}"
bind:toastMessageAction="@{RegisterViewModel.toastMessage}"/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:maxLines="1"
android:inputType="text"
android:text="@={RegisterViewModel.username}"/>
<data>
<variable
name="RegisterViewModel"
type="com.example.mvvm_example.viewmodels.RegisterViewModel">
</variable>
</data>
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bind="http://schemas.android.com/tools">
<data>
<variable
name="RegisterViewModel"
type="com.example.mvvm_example.viewmodels.RegisterViewModel">
</variable>
</data>
companion object {
@BindingAdapter("toastMessageAction")
@JvmStatic
fun runMe(view: View, message: String?) {
message?.let {
Toast.makeText(view.context, it, Toast.LENGTH_SHORT).show()
}
}
}
}
class RegisterViewModel : BaseObservable() {
private var register = RegisterDataModel("", "", "")
@Bindable
private var toastMessage: String? = null
private fun isValid(): Boolean {
return getUsername().isNotEmpty() && getPassword().isNotEmpty() && getCountry().isNotEmpty()
}
data class RegisterDataModel(var username: String, var password: String, var country: String)
package com.indieteam.android_permission_simple
import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.LocationManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.core.app.ActivityCompat
private fun getCurrentLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
val location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
location?.let {
it.apply {
val lat = latitude
val lon = longitude
texview.text = "Lat: $lat \n Lon: $lon"