Уроки по Kotlin Android Studio

Room + Hilt + Zxing | #1

Зависимости в build.gradle (Project): id (“com.google.dagger.hilt.android”) version (“2.46.1”) apply false Зависимости в build.gradle (App) plugins: id(“com.google.dagger.hilt.android”)id(“kotlin-kapt”) Зависимости в build.gradle (App) dependencies: implementation(“com.google.dagger:hilt-android:2.46.1”)kapt(“com.google.dagger:hilt-compiler:2.46.1”)implementation(“androidx.room:room-ktx:2.6.1”)kapt(“androidx.room:room-compiler:2.6.1”)implementation (“com.journeyapps:zxing-android-embedded:4.3.0”) Module: import android.app.Applicationimport androidx.room.Roomimport com.neco_desarrollo.qrcodescanerjetpack.data.MainDbimport dagger.Moduleimport dagger.Providesimport dagger.hilt.InstallInimport dagger.hilt.components.SingletonComponentimport javax.inject.Singleton@Module@InstallIn(SingletonComponent::class)object Module { @Provides @Singleton fun provideMainDb(app: Application) : MainDb{ return Room.databaseBuilder( app, MainDb::class.java, “products.db” ).build() }} MainDb: import androidx.room.Databaseimport androidx.room.RoomDatabase@Database( entities = [Product::class], version = 1)abstract class MainDb : RoomDatabase() { abstract val dao: Dao} Dao: import… Подробнее »Room + Hilt + Zxing | #1

CustomView #4

MainActivity.kt import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import android.view.animation.AccelerateDecelerateInterpolator import com.neco_desarrollo.customviewlesson.databinding.ActivityMainBinding import com.squareup.picasso.Picassoclass MainActivity : AppCompatActivity(), TestView.Listener { private lateinit var binding: ActivityMainBinding private val menuList = listOf( “Add”, “Edit”, “Paint”, “Clear”, “Delete”, “Exit”, “Hello”, “Draw” ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) setContentView(binding.root) binding.testView.listener = this } override fun onClick(index: Int) { binding.tvSelection.text = menuList[index] }} TestView.kt import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import… Подробнее »CustomView #4

CustomView #3

TestView.kt import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.util.AttributeSet import android.util.Log import android.view.MotionEvent import android.view.View import java.lang.Math.atan2 import kotlin.math.atan2class TestView( context: Context, attributeSet: AttributeSet) : View(context, attributeSet) { private val paint = Paint() private val paintC = Paint() private val startAngle = 0f private val colors = listOf( Color.RED, Color.BLUE, Color.GREEN, Color.CYAN, Color.BLACK, Color.GREEN, Color.DKGRAY, Color.YELLOW ) private val sweepAngle = 360f / colors.size private var buttonClicked =… Подробнее »CustomView #3

DrawApp | #3

MainActivity.kt import android.os.Bundle import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Canvas import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.StrokeCap import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.input.pointer.pointerInput import com.neco_desarrollo.drawapp.ui.BottomPanel import com.neco_desarrollo.drawapp.ui.PathData import com.neco_desarrollo.drawapp.ui.theme.DrawAppThemeclass MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val pathData = remember { mutableStateOf(PathData()) } DrawAppTheme { Column { DrawCanvas(pathData) BottomPanel(… Подробнее »DrawApp | #3

Draw App: #2

https://youtu.be/NAsEr7bPltw MainActivity.kt import android.os.Bundle import android.util.Log import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Canvas import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateListOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.input.pointer.pointerInput import com.neco_desarrollo.drawapp.ui.BottomPanel import com.neco_desarrollo.drawapp.ui.PathData import com.neco_desarrollo.drawapp.ui.theme.DrawAppThemeclass MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { val pathData = remember { mutableStateOf(PathData()) } DrawAppTheme { Column { DrawCanvas(pathData) BottomPanel{ color… Подробнее »Draw App: #2

Draw App

MainActivity.kt import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Canvas import androidx.compose.foundation.gestures.detectDragGestures import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Path import androidx.compose.ui.graphics.drawscope.Stroke import androidx.compose.ui.input.pointer.pointerInput import com.neco_desarrollo.drawapp.ui.theme.DrawAppThemeclass MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { DrawAppTheme { DrawCanvas() } } }}@Composablefun DrawCanvas() { val tempPath = Path() val path = remember { mutableStateOf(Path()) } Canvas( modifier = Modifier .fillMaxSize() .pointerInput(true) { detectDragGestures… Подробнее »Draw App

ConstrainLayout

Зависимости в build.gradle: implementation(“androidx.constraintlayout:constraintlayout-compose:1.0.1”) MainActivity.kt import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Image import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.constraintlayout.compose.ConstraintLayout import com.neco_desarrollo.constrainlayoutlessony.ui.theme.ConstrainLayoutLessonYThemeclass MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { ConstrainLayoutLessonYTheme { GreetingPreview() } } }}@Preview(showBackground = true)@Composablefun GreetingPreview() { ConstraintLayout(modifier = Modifier.fillMaxSize()) { val (text, button, image) = createRefs() val bottomGuideLine = createGuidelineFromBottom(0.2f) Button(onClick… Подробнее »ConstrainLayout

Custom View #2 (onTouchEvent)

TestView.kt import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.util.AttributeSet import android.view.MotionEvent import android.view.Viewclass TestView( context: Context, attributeSet: AttributeSet) : View(context, attributeSet) { private val paint = Paint() private val paintC = Paint() private val startAngle = -180f private val colors = listOf( Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW ) private val sweepAngle = 360f / colors.size private var buttonClicked = -1 init { paint.style = Paint.Style.STROKE paint.color = Color.GRAY paint.strokeWidth… Подробнее »Custom View #2 (onTouchEvent)

MenuProvider

https://youtu.be/zfGS-c3qaAE MainActivity.kt import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleclass MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) supportFragmentManager .beginTransaction() .replace(R.id.host, MainFragment.newInstance()) .commit() }} MainFragment.kt import android.os.Bundle import android.view.LayoutInflater import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.core.view.MenuProvider import androidx.fragment.app.Fragmentclass MainFragment : Fragment(), MenuProvider{ override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_main, container, false)… Подробнее »MenuProvider

CustomView #1

TestView.kt import android.content.Context import android.graphics.Canvas import android.graphics.Color import android.graphics.Paint import android.util.AttributeSet import android.view.Viewclass TestView( context: Context, attributeSet: AttributeSet) : View(context, attributeSet) { private val paint = Paint() private val paintC = Paint() init { paint.style = Paint.Style.STROKE paint.color = Color.GRAY paint.strokeWidth = 5f paintC.style = Paint.Style.FILL paintC.color = Color.RED } override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), 100f, paintC) canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint) }}… Подробнее »CustomView #1