Блог

Retrofit2 | #1

Зависимости для build.gradle: implementation “com.squareup.retrofit2:retrofit:2.9.0″implementation “com.squareup.retrofit2:converter-gson:2.9.0” Product.kt data class Product( val id: Int, val title: String, val description: String, val price: Int, val discountPercentage: Float, val rating: Float, val stock: Int, val brand: String, val category: String, val thumbnail: String, val images: List<String>) ProductApi.kt import retrofit2.http.GETimport retrofit2.http.Pathinterface ProductApi { @GET(“products/{id}”) suspend fun getProductById(@Path(“id”) id: Int): Product} Разрешение в манифест: <uses-permission android:name=”android.permission.INTERNET”/> activity_main.kt <?xml version=”1.0″ encoding=”utf-8″?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:app=”http://schemas.android.com/apk/res-auto” xmlns:tools=”http://schemas.android.com/tools” android:layout_width=”match_parent” android:layout_height=”match_parent”… Подробнее »Retrofit2 | #1

Bluetooth module | #15

ConnectThread.kt import android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothSocketimport java.io.IOExceptionimport java.util.*class ConnectThread(device: BluetoothDevice) : Thread() { private val uuid = “00001101-0000-1000-8000-00805F9B34FB” private var mSocket: BluetoothSocket? = null init { try { mSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(uuid)) } catch (e: IOException){ } catch (se: SecurityException){ } } override fun run() { try { mSocket?.connect() } catch (e: IOException){ } catch (se: SecurityException){ } } fun closeConnection(){ try { mSocket?.close() } catch (e: IOException){ } }}

Jetpack Compose: BottomNavigation()

Зависимость implementation “androidx.navigation:navigation-compose:2.6.0-alpha04” MainScreen.kt import android.annotation.SuppressLintimport androidx.compose.material.Scaffoldimport androidx.compose.runtime.Composableimport androidx.navigation.compose.rememberNavController@SuppressLint(“UnusedMaterialScaffoldPaddingParameter”)@Composablefun MainScreen() { val navController = rememberNavController() Scaffold( bottomBar = { BottomNavigation(navController = navController) } ) { NavGraph(navHostController = navController) }} BottomItem.kt import com.example.bottomnavigationlesson.Rsealed class BottomItem(val title: String, val iconId: Int, val route: String){ object Screen1: BottomItem(“Screen 1”, R.drawable.icon, “screen_1”) object Screen2: BottomItem(“Screen 2”, R.drawable.icon, “screen_2”) object Screen3: BottomItem(“Screen 3”, R.drawable.icon, “screen_3”) object Screen4: BottomItem(“Screen 4”, R.drawable.icon, “screen_4”)} Screens.kt import androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.wrapContentHeightimport… Подробнее »Jetpack Compose: BottomNavigation()

Bluetooth module | #14

DeviceListFragment import android.Manifestimport android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.content.IntentFilterimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Buildimport android.os.Bundleimport android.util.Logimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private lateinit var discoveryAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> private lateinit var pLauncher: ActivityResultLauncher<Array<String>> override fun onCreateView(… Подробнее »Bluetooth module | #14

Bluetooth module | #13

DeviceListFragment import android.Manifestimport android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.content.IntentFilterimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Buildimport android.os.Bundleimport android.util.Logimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private lateinit var discoveryAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> private lateinit var pLauncher: ActivityResultLauncher<Array<String>> override fun onCreateView(… Подробнее »Bluetooth module | #13

Weather App JetPack Compose #11 : AlertDialog

MainActivity.kt import android.content.Contextimport android.os.Bundleimport android.util.Logimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContentimport androidx.compose.foundation.Imageimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.runtime.MutableStateimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.rememberimport androidx.compose.ui.Modifierimport androidx.compose.ui.draw.alphaimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.res.painterResourceimport com.android.volley.Requestimport com.android.volley.toolbox.StringRequestimport com.android.volley.toolbox.Volleyimport com.meter_alc_rgb.weatherappcomposey.data.WeatherModelimport com.meter_alc_rgb.weatherappcomposey.screens.DialogSearchimport com.meter_alc_rgb.weatherappcomposey.screens.MainCardimport com.meter_alc_rgb.weatherappcomposey.screens.TabLayoutimport com.meter_alc_rgb.weatherappcomposey.ui.theme.WeatherAppComposeYThemeimport org.json.JSONObjectconst val API_KEY = “Ваш API ключь”class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { WeatherAppComposeYTheme { val daysList = remember { mutableStateOf(listOf<WeatherModel>()) } val dialogState = remember { mutableStateOf(false) } val currentDay = remember { mutableStateOf( WeatherModel( “”, “”, “0.0”,… Подробнее »Weather App JetPack Compose #11 : AlertDialog

Bluetooth module | #12

DeviceListFragment.kt import android.Manifestimport android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.content.IntentFilterimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Buildimport android.os.Bundleimport android.util.Logimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private lateinit var discoveryAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> private lateinit var pLauncher: ActivityResultLauncher<Array<String>> override fun onCreateView(… Подробнее »Bluetooth module | #12

Bluetooth module | #11

DeviceListFragment.kt import android.Manifestimport android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.content.IntentFilterimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Buildimport android.os.Bundleimport android.util.Logimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> private lateinit var pLauncher: ActivityResultLauncher<Array<String>> override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState:… Подробнее »Bluetooth module | #11

Bluetooth module | #10

DeviceListFragment import android.Manifestimport android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.Contextimport android.content.Intentimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Buildimport android.os.Bundleimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> private lateinit var pLauncher: ActivityResultLauncher<Array<String>> override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View… Подробнее »Bluetooth module | #10

Bluetooth module | #9

DeviceListFragment import android.app.Activityimport android.bluetooth.BluetoothAdapterimport android.bluetooth.BluetoothDeviceimport android.bluetooth.BluetoothManagerimport android.content.Contextimport android.content.Intentimport android.content.SharedPreferencesimport android.graphics.Colorimport android.os.Bundleimport androidx.fragment.app.Fragmentimport android.view.LayoutInflaterimport android.view.Viewimport android.view.ViewGroupimport androidx.activity.result.ActivityResultLauncherimport androidx.activity.result.contract.ActivityResultContractsimport androidx.recyclerview.widget.LinearLayoutManagerimport com.google.android.material.snackbar.Snackbarimport com.neco_dev.bt_def.databinding.FragmentListBindingclass DeviceListFragment : Fragment(), ItemAdapter.Listener { private var preferences: SharedPreferences? = null private lateinit var itemAdapter: ItemAdapter private var bAdapter: BluetoothAdapter? = null private lateinit var binding: FragmentListBinding private lateinit var btLauncher: ActivityResultLauncher<Intent> override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View { binding = FragmentListBinding.inflate(inflater, container, false) return… Подробнее »Bluetooth module | #9