Android Kotlin Tutorial Day 3
Learning Objective: Let’s try some most important codes like saving data into SharedPreference using Kotlin, Call Online API, and Display Static value into ListView.
SharedPreference Example
val sharedpreferences = requireContext().getSharedPreferences(
MyPREFERENCES,
Context.MODE_PRIVATE
)
val name = "Adarsh"
val editor = sharedpreferences.edit()
editor.putString("name", name)
editor.commit()
Toast.makeText(context, "Pref Saved", Toast.LENGTH_SHORT).show()
Call API Online
fun callAPI() {
// Instantiate the RequestQueue.
val queue: RequestQueue = Volley.newRequestQueue(context)
val url = "https://jsonplaceholder.typicode.com/todos/1"
// Request a string response from the provided URL.
val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
Log.e("cs", response)
try {
val obj = JSONObject(response)
Log.e("cs", obj.getString("userId"))
Log.e("cs", obj.getString("id"))
Log.e("cs", obj.getString("title"))
Log.e("cs", obj.getString("completed"))
} catch (t: Throwable) {
Log.e("cs", "Could not parse malformed JSON: \"" + "" + "\"")
}
}, object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError) {
Log.e("cs", error.toString())
}
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
ListView Display Items
fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="@+id/list1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
SecondFragment.kt
package com.example.kotlin1
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.fragment.app.Fragment
import com.example.kotlin1.databinding.FragmentSecondBinding
/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class SecondFragment : Fragment() {
private var _binding: FragmentSecondBinding? = null
var list1: ListView? = null
var courses = arrayOf("BCA", "PGDCA", "MCA", "MBA")
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view: View = inflater.inflate(R.layout.fragment_second, container, false)
list1 = view.findViewById<View>(R.id.list1) as ListView
val arr1: ArrayAdapter<String>
arr1 = ArrayAdapter(
requireContext(),
androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
courses
)
list1!!.adapter = arr1
// Inflate the layout for this fragment
// Inflate the layout for this fragment
return view
/* _binding = FragmentSecondBinding.inflate(inflater, container, false)
return binding.root*/
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/* binding.buttonSecond.setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}*/
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}