Getting error while trying to implement automatic image slider in fragments | Kotlin - kotlin

I'm trying to do Automatic image slider in fragment in kotlin. Any possible solutions??
I tried couple of other things also. But not a possible solution, I can do image slide inside the main activity but not to do the automatic sliding in fragments....
fragment_home.xml
Here is the XML File
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<com.denzcoskun.imageslider.ImageSlider
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/imageSlider"
app:iss_auto_cycle="true"
app:iss_delay="0"
app:iss_placeholder="#color/grey_font"
app:iss_error_image="#color/grey_font"
app:iss_corner_radius="5"
app:iss_selected_dot="#drawable/default_selected_dot"
app:iss_unselected_dot="#drawable/default_unselected_dot"
app:iss_period="1000">
</com.denzcoskun.imageslider.ImageSlider>
</FrameLayout>
HomeFragment.kt
//This is kotlin file
package com.example.fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.denzcoskun.imageslider.ImageSlider
import com.denzcoskun.imageslider.constants.ScaleTypes
import com.denzcoskun.imageslider.models.SlideModel
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
/**
* A simple [Fragment] subclass.
* Use the [HomeFragment.newInstance] factory method to
* create an instance of this fragment.
*/
class HomeFragment : Fragment() {
// TODO: Rename and change types of parameters
private var param1: String? = null
private var param2: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
//Here it is showing error on FindViewById
val imageSlider = findViewById<ImageSlider>(R.id.imageSlider) // HERE IS THE ERROR
val imageList = ArrayList<SlideModel>()
imageList.add(SlideModel("https://www.flickr.com/photos/zillniazi/44334678732","Babusar"))
imageList.add(SlideModel("https://www.flickr.com/photos/zillniazi/44334678732","Babusar"))
imageList.add(SlideModel("https://www.flickr.com/photos/zillniazi/44334678732","Babusar"))
imageList.add(SlideModel("https://www.flickr.com/photos/zillniazi/44334678732","Babusar"))
imageList.add(SlideModel("https://www.flickr.com/photos/zillniazi/44334678732","Babusar"))
imageSlider.setImageList(imageList, ScaleTypes.FIT)
return inflater.inflate(R.layout.fragment_home, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
HomeFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}

Related

Errors in connection of CameraFragment.kt with xml

I need help please, i'm getting errors in fragment.kt in process of getting a camera set up
**
//fragmentcamera.xml
**
**<?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"
tools:context=".MainActivity">
<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/preview"/>
<Button
android:id="#+id/btnTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Take photo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/btnDisplayGallery"
app:layout_constraintBottom_toBottomOf="parent"/>
<Button
android:id="#+id/btnDisplayGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_marginBottom="300dp"
android:text="Display gallery"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/btnTakePhoto" />
</androidx.constraintlayout.widget.ConstraintLayout>**
The following are images for camerafragment.kt
s
the errors that i am getting
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.Toast
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.ImageCaptureException
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.camera.view.PreviewView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import com.google.common.util.concurrent.ListenableFuture
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class cameraFragment : Fragment() {
// TODO: Rename and change types of parameters
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
//ListenableFuture interface listens for async operations external to
// main thread
// requires type of activity being observed - ProcessCameraProvider
private lateinit var cameraProviderFuture:
ListenableFuture<ProcessCameraProvider>
//used to decide whether to use front or back camera
private lateinit var cameraSelector: CameraSelector
//use case for capturing images
private var imageCapture: ImageCapture? = null
//interface that extends Executor to provide thread for capturing an image
private lateinit var imgCaptureExecutor: ExecutorService
//static variables
companion object {
//used for messages output in debugger
val TAG = "cameraFragment"
//used to check request code
private var REQUEST_CODE = 101
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout for requireActivity() fragment
val view: View = inflater.inflate(
R.layout.xml_camera,
container,
false
)
//get instance of ProcessCameraProvider
cameraProviderFuture = ProcessCameraProvider.getInstance(requireActivity())
//set default to back camera
cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
//instantiate imgCaptureExecutor
imgCaptureExecutor = Executors.newSingleThreadExecutor()
//check for permissions (similar to other sensors)
if (ActivityCompat.checkSelfPermission(
requireActivity(),
Manifest.permission.CAMERA
) != PackageManager.PERMISSION_GRANTED
)
//request permissions
ActivityCompat.requestPermissions(
requireActivity(),
arrayOf(
//array containing required permissions
Manifest.permission.CAMERA
),
REQUEST_CODE
)
else {
//if permission already granted, start camera
startCamera()
}
//set up event listener for btnCapture click
val btnTakePhoto: Button = view.findViewById(R.id.btnTakePhoto)
btnTakePhoto.setOnClickListener {
takePhoto()
} //set up event listener for btnGallery click
val btnDisplayGallery: Button = view.findViewById(R.id.btnDisplayGallery)
btnDisplayGallery.setOnClickListener {
displayGallery()
}
return view
}
//invoked when permissions change
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//check that request code matches and permission granted
if (requestCode == REQUEST_CODE &&
grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
//if permission now granted, start camera
startCamera()
}
}
//listen for data from camera
private fun startCamera() {
cameraProviderFuture.addListener(
{
//create ProcessCameraProvider instance
val cameraProvider = cameraProviderFuture.get()
//connect preview use case to the preview in the xml file
val preview: PreviewView = requireView().findViewById(R.id.preview)
val previewCase = Preview.Builder().build().also {
it.setSurfaceProvider(preview.surfaceProvider)
}
//instantiate capture use case
imageCapture = ImageCapture.Builder().build()
try {
//clear all bindings to previous use cases first
cameraProvider.unbindAll()
//bind lifecycle of camera to lifecycle of application
cameraProvider.bindToLifecycle(
requireActivity(),
cameraSelector, previewCase
)
cameraProvider.bindToLifecycle(
requireActivity(), cameraSelector,
imageCapture
)
} catch (e: Exception) {
Log.d(TAG, "Use case binding failed")
}
},
//run asynchronous operation being listened to by cameraProviderFuture
ContextCompat.getMainExecutor(requireActivity())
)
}
//take photo
private fun takePhoto() {
imageCapture?.let {
//create file with fileName using timestamped in milliseconds
val file = File(
requireActivity().externalMediaDirs[0],
"snap_${System.currentTimeMillis()}"
)
//save image in above file
val outputFileOptions =
ImageCapture.OutputFileOptions.Builder(file).build()
//call takePicture method with where to find image
it.takePicture(
outputFileOptions,
imgCaptureExecutor,
//set up callbacks for when picture is taken
object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(
outputFileResults:
ImageCapture.OutputFileResults
) {
Log.i(TAG, "Image saved in ${file.toUri()}")
}
override fun onError(exception: ImageCaptureException) {
Toast.makeText(
requireActivity(),
"Error taking photo",
Toast.LENGTH_LONG
).show()
Log.i(TAG, "Error taking photo: $exception")
}
}
)
}
animateFlash()
}
//flash to provide feedback that photo taken
private fun animateFlash() {
val preview: PreviewView = requireView().findViewById(R.id.preview)
preview.postDelayed({
preview.foreground = ColorDrawable(Color.argb(125, 255, 255, 255))
preview.postDelayed({
preview.foreground = null
}, 30)
}, 60)
}
//display gallery
private fun displayGallery() {
val intent = Intent(requireActivity(), GalleryActivity::class.java)
startActivity(intent)
}
}
Replace all "this,getApplicationContext" with getActivity(). In Java
Replace all "this,getApplicationContext" with activity. In Kotlin.
Two likely definitions:
getActivity() in a Fragment returns the Activity the Fragment is currently associated with. (see http://developer.android.com/reference/android/app/Fragment.html#getActivity()).
getActivity() is user-defined.

Similar to `by`, but for other methods than get/set methods

I only just discovered the by keyword for class, and it seems that it only works for get/set methods. But what about other methods? For example, in the code below, if I omit override fun suggestSelection(...}, it does not seem to return fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);. As you see, all it does is calling the same method in fallback with the same argument. Is this the only way or is there anything to make fallback automatically called for this?
inner class MyTextClassfier(private val fallback:TextClassifier) : TextClassifier by fallback
{
override fun suggestSelection(
text: CharSequence,
selectionStartIndex: Int,
selectionEndIndex: Int,
defaultLocales: LocaleList?
): TextSelection
{
return fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);
}
Adding full code as requested below. It needs the default generated Android project structure by Android Studio.
MainActivity.kt
package com.stackoverflow.test
import android.content.Context
import android.os.Bundle
import android.os.LocaleList
import android.util.Log
import android.view.textclassifier.TextClassification
import android.view.textclassifier.TextClassificationManager
import android.view.textclassifier.TextClassifier
import android.view.textclassifier.TextSelection
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val textClassificationManager = getSystemService(Context.TEXT_CLASSIFICATION_SERVICE) as TextClassificationManager;
val defaultOne = textClassificationManager.textClassifier;
val txt = findViewById<TextView>(R.id.textview1);
txt.setTextClassifier(MyTextClassfier(defaultOne));
}
inner class MyTextClassfier(private val fallback:TextClassifier) : TextClassifier by fallback
{
override fun suggestSelection(
text: CharSequence,
selectionStartIndex: Int,
selectionEndIndex: Int,
defaultLocales: LocaleList?
): TextSelection
{
return fallback.suggestSelection(text, selectionStartIndex, selectionEndIndex, defaultLocales);
//return TextSelection.Builder(0,0).build();
}
override fun classifyText(
text: CharSequence,
startIndex: Int,
endIndex: Int,
defaultLocales: LocaleList?
): TextClassification
{
//Selection ended. User has lifted his finger.
Log.d("stack", "classifyText is called.");
return TextClassification.Builder().build();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="#+id/textview1"
android:text="#string/large_text"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textIsSelectable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Kotlin :Can't get the price of the item of RecyclerView on Fragment

I am attempting to make a recyclerView on Fragment ,
The idea is to get the price of the Item ,when the item been selected ,
and multiply by the select amount from the popup menu also.
Like below :
The recyclerView model item : item_layout.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:orientation="vertical"
android:layout_width="120dp"
android:gravity="center"
android:id="#+id/cardView"
android:layout_margin="10dp"
android:background="#40E0D0"
android:layout_height="200dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Price:"
android:textColor="#color/black"
android:textSize="18sp"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="100"
android:textColor="#color/black"
android:textSize="18sp"/>
</LinearLayout>
The model of the item : Product.kt :
package com.gearsrun.popmenuapplication
data class Product(var price : String)
private selectFuntion(itemPrice:Int){
}
ProductAdapter.kt
package com.gearsrun.popmenuapplication
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_layout.view.*
class ProductAdapter(private val productList:List<Product>,private val itemClick:(Int) -> Unit):RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
private var selectedItemPosition :Int = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
return ProductViewHolder(itemView,itemClick)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val currentItem = productList[position]
holder.price.text = currentItem.price.toString()
holder.itemView.setOnClickListener {
selectedItemPosition = position
notifyDataSetChanged()
}
if(selectedItemPosition == position){
holder.itemView.cardView.setBackgroundColor(Color.parseColor("#FAFAD2"))
}else{
holder.itemView.cardView.setBackgroundColor(Color.parseColor("#FFFFFF"))
}
}
override fun getItemCount() = productList.size
class ProductViewHolder(itemView: View, itemClick: (Int) -> Unit) :
RecyclerView.ViewHolder(itemView) {
val price : TextView = itemView.price
init {
itemView.setOnClickListener {
itemClick(price.text.toString().toInt()) //sortOf if you need String, change that on String in every declaration
}
}
}
}
Fragment.kt
package com.gearsrun.popmenuapplication.fragment
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.recyclerview.widget.LinearLayoutManager
import com.gearsrun.popmenuapplication.Product
import com.gearsrun.popmenuapplication.ProductAdapter
import com.gearsrun.popmenuapplication.R
import kotlinx.android.synthetic.main.fragment_home.*
class HomeFragment : Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//init popup menu
val popupMenu = PopupMenu(
context,
selectedTv
)
//add menu items to popup menu
popupMenu.menu.add(Menu.NONE,0,0,"1")
popupMenu.menu.add(Menu.NONE,1,1,"2")
popupMenu.menu.add(Menu.NONE,2,2,"3")
popupMenu.menu.add(Menu.NONE,3,3,"4")
popupMenu.menu.add(Menu.NONE,4,4,"5")
//handle menu clicks
popupMenu.setOnMenuItemClickListener {menuItem ->
//get id of the item clicked
val id = menuItem.itemId
if(id==0){
selectedTv.text = "1"
}else if(id==1){
selectedTv.text = "2"
}else if(id==2){
selectedTv.text = "3"
}else if(id==3){
selectedTv.text = "4"
}else if(id==4){
selectedTv.text = "5"
}
true
}
//handle button click,show menu
selectedTv.setOnClickListener {
popupMenu.show()
}
//display recyclerview
val productList = generateProductList()
fun selectFuntion(itemPrice: Int){
Log.e("haha","You have click${itemPrice}")
}
var adapter = ProductAdapter(productList,::selectFuntion)
giftRecycleView.adapter = adapter
giftRecycleView.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
}
private fun generateProductList():List<Product> {
val list = ArrayList<Product>()
list.add(Product(1))
list.add(Product(2))
list.add(Product(3))
return list
}
}
Can anyone help me modify my code ?
I will need that the item's value can be catch once click ,and multiply by the select amount ,in order to get the total price .
Thank you so much in advance !!
ProductAdapter.kt
add this:
class ProductAdapter(
private val productList:List<Product>,
private val itemClick: (Int) -> Unit
): RecyclerView.Adapter<ProductAdapter.ProductViewHolder>() {
delete this:
interface onItemClickListener {
fun onItemClick(position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener) {
mlistener = listener
}
replace this:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_layout,parent,false)
return ProductViewHolder(itemView,itemClick)
}
replace this:
class ProductViewHolder(itemView: View, itemClick: (Int) -> Unit) :
RecyclerView.ViewHolder(itemView) {
val price : TextView = itemView.price
init {
itemView.setOnClickListener {
itemclick(price.text.toInt()) //sortOf if you need String, change that on String in every declaration
}
}
}
replace in fragment:
var adapter = ProductAdapter(productList, ::yourFunction)
giftRecycleView.adapter = adapter
add in fragment or ViewModel:
private yourFunction(itemPrice: Int) {
// do something with price
}
if you need to edit that price just make return type:
(Int) -> Int sort of :D
and in adapter
price.text = itemclick(price.text.toInt()).toString()

Kotlin - Set zoom level google map Android Studio

I've added the Google Map API and added my markers, but when the app is run, it is zoomed out quite a lot. I tried researching but I can't find what I need. Can someone please tell me how to set the zoom level once the map is loaded? E.g, it should show only a city not the whole world.
Here's my fragment code
package com.example.myassignment.Fragments
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.myassignment.R
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import kotlinx.android.synthetic.main.fragment_destinations.*
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"
class DestinationsFragment : Fragment(), OnMapReadyCallback {
private var param1: String? = null
private var param2: String? = null
private lateinit var googleMap: GoogleMap
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
arguments?.let {
param1 = it.getString(ARG_PARAM1)
param2 = it.getString(ARG_PARAM2)
}
mapView.onCreate(savedInstanceState)
mapView.onResume()
mapView.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap?) {
map?.let {
googleMap = it
}
val sydney = LatLng(-34.0, 151.0)
val stokeOnTrent = LatLng(53.025780,-2.177390)
val stokeCobrdige = LatLng(53.029380,-2.188740)
// Here's my markers
googleMap.addMarker(MarkerOptions().position(sydney).title("Marker in Sydney"))
googleMap.addMarker(MarkerOptions().position(stokeOnTrent).title("Stoke Center"))
googleMap.addMarker(MarkerOptions().position(stokeCobrdige).title("Cobridge - Stoke"))
googleMap.minZoomLevel
googleMap.moveCamera(CameraUpdateFactory.newLatLng(stokeOnTrent))
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_destinations, container, false)
}
companion object {
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment DestinationsFragment.
*/
// TODO: Rename and change types and number of parameters
#JvmStatic
fun newInstance(param1: String, param2: String) =
DestinationsFragment().apply {
arguments = Bundle().apply {
putString(ARG_PARAM1, param1)
putString(ARG_PARAM2, param2)
}
}
}
}
Thank you!
If you didn't resolve this already, you need to use the function newLatLngZoom() instead of newLatLng() and provide the zoom level - 10.
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stokeOnTrent, someZoomLevel))
More info can be found here, but it says:
val homeLatLng = LatLng(latitude, longitude)
val zoomLevel = 15f
The zoom level controls how zoomed in you are on the map. The following list gives you an idea of what level of detail each level of zoom shows:
1: World
5: Landmass/continent
10: City
15: Streets
20: Buildings
Move the camera to homeLatLng by calling the moveCamera() function on the map object and pass in a CameraUpdate object using CameraUpdateFactory.newLatLngZoom(). Pass in the homeLatLng object and the zoomLevel.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(homeLatLng, zoomLevel))

Kotlin RecyclerView how select first list item after activity load? tried (code below) failed :(

I can select 1st item in RecyclerView (working code below, click on "email" FAB button - boom! 1st selected)
However I cannot get 1st RecyclerView click in code when app starts,
I looked for override fun onViewCreated() but nothing like it for activity,
where can I call selectFirstOnList() after activity & recyclerview fully rendered?
what event fires on activity fully rendered/loaded?
or is my noob kotlin way of thinking flawed? its 99.9% working :(
Thanks in advance for any help :)
SOLUTIUON (code edited to bottom of this post will replace code in ItemListActivity.kt)
postdelay select first (you can see from original code I'd already tried something like this!) PHEW! hope this helps someone :)
ItemListActivity.kt
package ie.dpsystems.asm.list
import android.content.Context
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.snackbar.Snackbar
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import ie.dpsystems.asm.R
import ie.dpsystems.asm.data.Orientation
import ie.dpsystems.asm.data.State
import ie.dpsystems.asm.data.State.Companion.trackLog
import ie.dpsystems.asm.detail.ItemDetailActivity
import ie.dpsystems.asm.detail.ItemDetailFragment
import kotlinx.android.synthetic.main.activity_item_list.*
import kotlinx.android.synthetic.main.item_list_content.view.*
import kotlinx.android.synthetic.main.item_list.*
import android.os.Handler
class ItemListActivity : AppCompatActivity() {
private var twoPane: Boolean = false
private var showToastEvents: Boolean = true
private fun uiIsTwoPane():Boolean{
try{
if (item_detail_container != null) {
// The detail container view will be present only in the
// large-screen layouts (res/values-w900dp).
// If this view is present, then the
// activity should be in two-pane mode.
return true
}
}catch(e:Exception)
{
Toast.makeText(this,"E: ${e.toString()}",Toast.LENGTH_SHORT).show()
}
return false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
trackLog(this,"onCreate()")
setContentView(R.layout.activity_item_list)
twoPane = uiIsTwoPane()
setSupportActionBar(toolbar)
toolbar.title = title
fab.setOnClickListener { view -> onListFabClick(view) }
setupRecyclerView(recycleview_list)
refreshUI()
}
private fun setupRecyclerView(recyclerView: RecyclerView) {
trackLog(this,"setupRecyclerView()")
State.dataRows = ListContent.ITEMS
recyclerView.adapter = SimpleItemRecyclerViewAdapter(
this,
twoPane,
this
)
var recycleViewUI = recycleview_list
postAndNotifyAdapter(Handler(), recycleViewUI)
}
override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
trackLog(this,"onConfigurationChanged()")
}
override fun onStart() {
super.onStart()
trackLog(this,"onStart()")
}
override fun onResume() {
super.onResume()
trackLog(this,"onResume() A")
checkOrientationChange()
refreshUI()
trackLog(this,"onResume() B")
}
private fun checkOrientationChange() {
trackLog(this,"checkOrientationChange()")
if (State.lastOrientation != null) {
val thisOrientation = if (twoPane) Orientation.landscape else Orientation.portrate
if (thisOrientation != State.lastOrientation) {
putDetailFragmentOnDetailFragmentHolder()
}
}
}
private fun putDetailFragmentOnDetailFragmentHolder() {
trackLog(this,"putDetailFragmentOnDetailFragmentHolder()")
if(item_detail_container!=null){
val fragment = ItemDetailFragment() //val fragment = ItemDetailFragment().apply {arguments = Bundle().apply {putInt("SOME UNIQUE TAG", selectedItemUniqueID)}}
val container = item_detail_container
container.removeAllViewsInLayout()
supportFragmentManager.beginTransaction().replace(R.id.item_detail_container, fragment).commit()
}
}
override fun onPause() {
super.onPause()
trackLog(this,"onPause()")
}
override fun onStop() {
super.onStop()
trackLog(this,"onStop()")
}
class SimpleItemRecyclerViewAdapter( private val parentActivity: ItemListActivity
,private val twoPane: Boolean
,private val context: Context //private val context = parentActivity.applicationContext
) : RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder>() {
override fun getItemCount(): Int {
return State.dataRows.size
}
override fun onBindViewHolder(recyclerViewRow: ViewHolder, position: Int) {
trackLog(context, "onBindViewHolder()")
val dataThisRow = State.dataRows[position]
dataThisRow.listRowIndex = position
setDataToRecyclerRow(recyclerViewRow, dataThisRow)
recyclerViewRow.itemView.setOnClickListener {
onListItemClick(dataThisRow.uniqueID)
}
if (dataThisRow.uniqueID == State.selectedListItemUniqueId) {
recyclerViewRow.idRow.setBackgroundColor(Color.parseColor("#009688"))
} else {
recyclerViewRow.idRow.setBackgroundColor(Color.WHITE)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
trackLog(context, "onCreateViewHolder()")
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.item_list_content, parent, false)
return ViewHolder(view, 1)
}
override fun onViewAttachedToWindow(holder: ViewHolder) {
super.onViewAttachedToWindow(holder)
trackLog(context, "onViewAttachedToWindow()")
}
inner class ViewHolder(itemView: View, position: Int) : RecyclerView.ViewHolder(itemView) {
val idView: TextView = itemView.id_text
val contentView: TextView = itemView.content
val idRow = itemView.id_row_linear_layout
}
private fun setDataToRecyclerRow(recyclerViewRow: ViewHolder, data: ListContent.ListItem) {
trackLog(context, "setDataToRecyclerRow(id: ${data.id})")
recyclerViewRow.idView.text = data.id
recyclerViewRow.contentView.text = data.itemTitle
recyclerViewRow.itemView.tag = data
}
private fun onListItemClick(selectedItemUniqueID: Int) {
trackLog(context, "onListItemClick($selectedItemUniqueID)")
State.selectedListItemUniqueId = selectedItemUniqueID
if (twoPane) {
State.lastOrientation = Orientation.landscape
putDetailFragmentOnDetailActivity()
} else {
State.lastOrientation = Orientation.portrate
launchDetailActivity()
}
notifyDataSetChanged()
}
private fun launchDetailActivity() {
trackLog(context, "launchDetailActivity()")
val intent = Intent(
context,
ItemDetailActivity::class.java
) //val intent = Intent(context, ItemDetailActivity::class.java).apply {putExtra(ItemDetailFragment.ARG_ITEM_ID, selectedItemUniqueID)}
context.startActivity(intent)
}
private fun putDetailFragmentOnDetailActivity() {
trackLog(context, "putDetailFragmentOnDetailFragmentHolder()")
val fragment =
ItemDetailFragment() //val fragment = ItemDetailFragment().apply {arguments = Bundle().apply {putInt("SOME UNIQUE TAG", selectedItemUniqueID)}}
val container = parentActivity.item_detail_container
container.removeAllViewsInLayout()
parentActivity.supportFragmentManager.beginTransaction()
.replace(R.id.item_detail_container, fragment).commit()
}
}
private fun onListFabClick(view: View) {
trackLog(this, "onListFabClick()")
selectFirstOnList()
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
private fun refreshUI() {
trackLog(this, "refreshUI()")
var recycleViewUI = recycleview_list
if (State.selectedListItemUniqueId == null) {
selectFirstOnList()
} else {
val selectedListItem: ListContent.ListItem? =
State.findDataRowByUniqueId(State.selectedListItemUniqueId)
if (selectedListItem == null) {
selectFirstOnList()
recycleViewUI.findViewHolderForAdapterPosition(0)?.itemView?.performClick()
} else {
recycleViewUI.getLayoutManager()
?.scrollToPosition(selectedListItem.listRowIndex)
}
}
}
private fun selectFirstOnList() {
trackLog(this, "selectFirstOnList()")
if (twoPane) {
var recycleViewUI = recycleview_list
//recycleViewUI.getLayoutManager()?.scrollToPosition(0)
recycleViewUI.findViewHolderForAdapterPosition(0)?.itemView?.performClick()
}
}
protected fun postAndNotifyAdapter(handler: Handler, recyclerView: RecyclerView) {
trackLog(this, "postAndNotifyAdapter()")
/*
handler.post(Runnable {
if (!recyclerView.isComputingLayout) {
// This will call first item by calling "performClick()" of view.
(recyclerView.findViewHolderForLayoutPosition(0) as RecyclerView.ViewHolder).itemView.performClick()
} else {
postAndNotifyAdapter(handler, recyclerView) //, adapter
}
})
*/
}
}
class State (hold selected on screen rotation/activity changes)
import android.content.Context
import android.util.Log
import android.widget.Toast
import ie.dpsystems.asm.list.ListContent
import java.util.ArrayList
enum class Orientation { portrate, landscape }
class State {
companion object {
public var selectedListItemUniqueId:Int? = null
public var dataRows: MutableList<ListContent.ListItem> = ArrayList()
public var lastOrientation:Orientation? = null
public fun findDataRowByUniqueId(uniqueID:Int?):ListContent.ListItem?{
if(uniqueID==null) return null
return State.dataRows.find { it.uniqueID == uniqueID}
}
public fun trackLog(context: Context, text:String){
//Toast.makeText(context,text, Toast.LENGTH_LONG).show()
Log.d("track",text)
}
}
}
src/main/res/layout-w900dp/item_list.xml (2 pane for tablet)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
tools:context=".list.ItemListActivity">
<!--
This layout is a two-pane layout for list / detail
-->
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/recycleview_list"
android:name="ie.dpsystems.asm.ItemListFragment"
android:layout_width="#dimen/item_width"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context="ie.dpsystems.asm.list.ItemListActivity"
tools:listitem="#layout/item_list_content" />
<FrameLayout
android:id="#+id/item_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3" />
</LinearLayout>
item_list_content.xml (RecyclerView per row layout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/id_row_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
object ListContent (to put fake data into list during development)
import java.util.ArrayList
import java.util.HashMap
/**
* Helper class for providing sample itemTitle for user interfaces created by
* Android template wizards.
*
* TODO: Replace all uses of this class before publishing your app.
*/
object ListContent {
/**
* An array of sample (dummy) items.
*/
val ITEMS: MutableList<ListItem> = ArrayList()
/**
* A map of sample (dummy) items, by ID.
*/
val ITEM_MAP: MutableMap<String, ListItem> = HashMap()
private val COUNT = 25
init {
// Add some sample items.
for (i in 1..COUNT) {
addItem(fakeGetRecordFromSqlite(i))
}
}
private fun addItem(item: ListItem) {
ITEMS.add(item)
ITEM_MAP.put(item.id, item)
}
private fun fakeGetRecordFromSqlite(position: Int): ListItem {
return ListItem(position, -1, position.toString(), "Item " + position, fakeGetRecordCollectionFromSqlite(position))
}
private fun fakeGetRecordCollectionFromSqlite(position: Int): String {
val builder = StringBuilder()
builder.append("Details about Item: ").append(position)
for (i in 0..position - 1) {
builder.append("\nMore details information here.")
}
return builder.toString()
}
/**
* A dummy item representing a piece of itemTitle.
*/
data class ListItem(val uniqueID:Int, var listRowIndex:Int, val id: String, val itemTitle: String, val details: String) {
override fun toString(): String = itemTitle
}
}
class ItemDetailFragment (display details in detail fragment)
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import ie.dpsystems.asm.R
import ie.dpsystems.asm.data.State
import ie.dpsystems.asm.list.ListContent
import kotlinx.android.synthetic.main.activity_item_detail.*
import kotlinx.android.synthetic.main.item_detail.view.*
/**
* A fragment representing a single Item detail screen.
* This fragment is either contained in a [ItemListActivity]
* in two-pane mode (on tablets) or a [ItemDetailActivity]
* on handsets.
*/
class ItemDetailFragment : Fragment() {
/**
* The dummy itemTitle this fragment is presenting.
*/
private var item: ListContent.ListItem? = null
private var selectedItemUniqueID:Int? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//arguments?.let { if (it.containsKey(ARG_ITEM_ID)) {val uniqueID = it.getInt(ARG_ITEM_ID)}}
item = State.findDataRowByUniqueId(State.selectedListItemUniqueId)
activity?.toolbar_layout?.title = item?.itemTitle
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.item_detail, container, false)
item?.let {rootView.item_detail.text = it.details}
return rootView
}
}
class ItemDetailActivity (for single pane screen size devices)
import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NavUtils
import android.view.MenuItem
import android.view.View
import ie.dpsystems.asm.list.ItemListActivity
import ie.dpsystems.asm.R
import kotlinx.android.synthetic.main.activity_item_detail.*
import kotlinx.android.synthetic.main.activity_item_detail.fab
/**
* An activity representing a single Item detail screen. This
* activity is only used on narrow width devices. On tablet-size devices,
* item details are presented side-by-side with a list of items
* in a [ItemListActivity].
*/
class ItemDetailActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_item_detail)
setSupportActionBar(detail_toolbar)
fab.setOnClickListener { view -> onListFabClick(view) }
supportActionBar?.setDisplayHomeAsUpEnabled(true) // Show the Up button in the action bar.
putDetailFragmentOnDetailActivity(savedInstanceState)
}
private fun putDetailFragmentOnDetailActivity(savedInstanceState: Bundle?){ // Create the detail fragment and add it to the activity using a fragment transaction.
// savedInstanceState is non-null when there is fragment state
// saved from previous configurations of this activity
// (e.g. when rotating the screen from portrait to landscape).
// In this case, the fragment will automatically be re-added
// to its container so we don't need to manually add it.
// For more information, see the Fragments API guide at:
// http://developer.android.com/guide/components/fragments.html
if (savedInstanceState == null) {
val fragment = ItemDetailFragment() //val fragment = ItemDetailFragment().apply {arguments = Bundle().apply {putString("SOME UNIQUE TAG",intent.getStringExtra("SOME UNIQUE TAG"))}}
supportFragmentManager.beginTransaction().add(R.id.item_detail_container, fragment).commit()
}
}
private fun onListFabClick(view: View?) {
if(view!=null) Snackbar.make(view, "Replace with your own detail action", Snackbar.LENGTH_LONG).setAction("Action", null).show()
}
override fun onOptionsItemSelected(item: MenuItem) =
when (item.itemId) {
android.R.id.home -> {
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
NavUtils.navigateUpTo(this, Intent(this, ItemListActivity::class.java))
true
}
else -> super.onOptionsItemSelected(item)
}
}
SOLUTIUON
postdelay select first (you can see from original code I'd already tried something like this!) PHEW! hope this helps someone :)
protected fun postAndNotifyAdapter(handler: Handler, recyclerView: RecyclerView) {
trackLog(this, "postAndNotifyAdapter() ${State.selectedListItemUniqueId}")
if (twoPane && State.selectedListItemUniqueId==null) {
Handler().postDelayed({
if (!recyclerView.isComputingLayout) {
trackLog(this, "postAndNotifyAdapter() !recyclerView.isComputingLayout ${State.selectedListItemUniqueId}")
selectFirstOnList()
} else {
postAndNotifyAdapter(handler, recyclerView) //, adapter
}
}, 1000)
}
}
I've edited my original post above to be complete with answer!
I recommend you use an interface to notify when recyclerview is done rendering it's items.
How to know when the RecyclerView has finished laying down the items?