I am trying to run this example of face detection with the firebase MLkit on Android. Instead of the built in CameraView I use the library CameraView.
Running the code the logs says: Faces: [], so no faces were found from the camera.
You can checkout the project here on github.
the code:
import android.os.Bundle
import android.util.Log
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import android.widget.TextView
import androidx.annotation.WorkerThread
import com.otaliastudios.cameraview.CameraView
import com.otaliastudios.cameraview.Frame
import com.otaliastudios.cameraview.FrameProcessor
import com.otaliastudios.cameraview.Size
import androidx.core.view.ViewCompat.getRotation
import com.google.android.gms.tasks.OnFailureListener
import com.google.firebase.FirebaseApp
import com.google.firebase.ml.vision.FirebaseVision
import com.google.firebase.ml.vision.common.FirebaseVisionImage
import com.google.firebase.ml.vision.common.FirebaseVisionImageMetadata
import com.google.firebase.ml.vision.face.FirebaseVisionFace
import com.google.firebase.ml.vision.face.FirebaseVisionFaceContour
import com.google.firebase.ml.vision.face.FirebaseVisionFaceDetectorOptions
import com.google.firebase.ml.vision.face.FirebaseVisionFaceLandmark
class MainActivity : AppCompatActivity() {
private lateinit var textMessage: TextView
private lateinit var camera:CameraView
private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
textMessage.setText(R.string.title_home)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_dashboard -> {
textMessage.setText(R.string.title_dashboard)
return#OnNavigationItemSelectedListener true
}
R.id.navigation_notifications -> {
textMessage.setText(R.string.title_notifications)
return#OnNavigationItemSelectedListener true
}
}
false
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
textMessage = findViewById(R.id.message)
navView.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
camera = findViewById<CameraView>(R.id.camera)
camera.setLifecycleOwner(this)
val realTimeOpts1 = FirebaseVisionFaceDetectorOptions.Builder()
.setContourMode(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
.build()
val realTimeOpts = FirebaseVisionFaceDetectorOptions.Builder()
.setPerformanceMode(FirebaseVisionFaceDetectorOptions.ACCURATE)
.setLandmarkMode(FirebaseVisionFaceDetectorOptions.ALL_LANDMARKS)
.build()
FirebaseApp.initializeApp(this);
camera.addFrameProcessor { frame ->
val data = frame.data
val rotation = frame.rotation
val time = frame.time
val size = frame.size
val format = frame.format
val metadata = FirebaseVisionImageMetadata.Builder()
.setWidth(480) // 480x360 is typically sufficient for
.setHeight(360) // image recognition
.setFormat(FirebaseVisionImageMetadata.IMAGE_FORMAT_NV21)
.setRotation(3)
.build()
val image = FirebaseVisionImage.fromByteArray(data, metadata)
val detector = FirebaseVision.getInstance()
.getVisionFaceDetector(realTimeOpts)
val result = detector.detectInImage(image)
.addOnSuccessListener { faces ->
// Task completed successfully
// ...
Log.e("FACE", faces.toString())
for (face in faces) {
val bounds = face.boundingBox
val rotY = face.headEulerAngleY // Head is rotated to the right rotY degrees
val rotZ = face.headEulerAngleZ // Head is tilted sideways rotZ degrees
// If landmark detection was enabled (mouth, ears, eyes, cheeks, and
// nose available):
val leftEar = face.getLandmark(FirebaseVisionFaceLandmark.LEFT_EAR)
leftEar?.let {
val leftEarPos = leftEar.position
}
// If contour detection was enabled:
val leftEyeContour = face.getContour(FirebaseVisionFaceContour.LEFT_EYE).points
val upperLipBottomContour = face.getContour(FirebaseVisionFaceContour.UPPER_LIP_BOTTOM).points
// If classification was enabled:
if (face.smilingProbability != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
val smileProb = face.smilingProbability
}
if (face.rightEyeOpenProbability != FirebaseVisionFace.UNCOMPUTED_PROBABILITY) {
val rightEyeOpenProb = face.rightEyeOpenProbability
}
// If face tracking was enabled:
if (face.trackingId != FirebaseVisionFace.INVALID_ID) {
val id = face.trackingId
}
}
}.addOnFailureListener(
object : OnFailureListener {
override fun onFailure(e: Exception) {
// Task failed with an exception
// ...
Log.e("M", e.toString())
}
})
}}
}
in activity_main.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="280dp"
card_view:cardCornerRadius="280dp"
android:layout_height="280dp">
<com.otaliastudios.cameraview.CameraView
android:id="#+id/camera"
app:cameraFacing="front"
android:keepScreenOn="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.cardview.widget.CardView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Tried the same also with Fotoapparat, same result: No faces found. What am I missing here?
Try getting the height and width from the frame programmatically instead of hard coding them.
I think the problem is frame.rotation value. Maybe you can use FirebaseVisionImageMetadata.ROTATION_90 for FirebaseVisionImageMetadata
Related
I'm trying to create an application and considering my level it's not easy! I hope you could help me since I didn't succeed with the many links I found on the internet.
I can't add the onClick function of View.OnClickListener, each time the Intent function is not recognized. I tried to implement it in the UserViewHolder and FirestoreRecyclerAdapter class but it doesn't work.
Here is my current code:
---------- kotlin part ---------
package edu.stanford.rkpandey.emojistatus
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.*
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.firebase.ui.firestore.FirestoreRecyclerAdapter
import com.firebase.ui.firestore.FirestoreRecyclerOptions
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.ktx.auth
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.android.synthetic.main.activity_main.*
data class User(
val displayName: String? = "",
val emojis: String? = ""
)
class UserViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
class MainActivity : AppCompatActivity() {
private val db = Firebase.firestore
private lateinit var auth: FirebaseAuth
// Query the users collection
private val query = db.collection("users")
val options = FirestoreRecyclerOptions.Builder<User>()
.setQuery(query, User::class.java)
.setLifecycleOwner(this).build()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
auth = Firebase.auth
val adapter = object: FirestoreRecyclerAdapter<User, UserViewHolder>(options) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
val view = LayoutInflater.from(this#MainActivity).inflate(R.layout.item_pack, parent, false)
return UserViewHolder(view)
}
override fun onBindViewHolder(
holder: UserViewHolder,
position: Int,
model: User
) {
val tvName: TextView = holder.itemView.findViewById(R.id.title)
val tvEmojis: TextView = holder.itemView.findViewById(R.id.excerpt)
tvName.text = model.displayName
tvEmojis.text = model.emojis
}
}
rvUsers.adapter = adapter
rvUsers.layoutManager = LinearLayoutManager(this)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.miLogout) {
Log.i("MainActivity", "Logout")
auth.signOut()
val logoutIntent = Intent(this, LoginActivity::class.java)
logoutIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(logoutIntent)
}
return super.onOptionsItemSelected(item)
}
}
------- xml part -------
=> activity_main
<?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.recyclerview.widget.RecyclerView
android:id="#+id/rvUsers"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
=> item_pack
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100sp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="12sp"
android:layout_marginTop="12sp"
android:layout_marginEnd="12sp"
android:focusable="true"
android:clickable="true"
app:cardCornerRadius="10dp"
android:foreground="?android:attr/selectableItemBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:background="#color/colorPack">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5sp"
style="#style/NoteTitleFont"
android:textColor="#color/colorTitle"
tools:text="Note 1" />
<TextView
android:id="#+id/excerpt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12sp"
android:layout_below="#id/title"
android:maxLines="1"
android:ellipsize="end"
android:textStyle="italic"
android:textColor="#color/colorDescribe"
tools:text="test text va se trouver ici, ça va contenir le début de la description du package." />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
This code gives this result :
I would like that when I click on one of the carviews it can go to the activity_pack_detail.
Do you know how to do Intent to PackDetailActivity?
I get this error no matter what I do =>
You're getting that error because you are calling Intent's class constructor with a wrong argument. The first argument should be a Context and not a View. The keyword this is referring in your code to a View and not to a context, hence the error.
To solve this, you have to pass a Context object like this:
val i = Intent(view.getContext(), PackDetailActivity::class.java)
And your error will go away.
I want to display a text view in the preview view of camera. It will appear like a label on the camera preview. It should not affect the image. Just appear like a label on preview view. I tried using frame layout but the text view disappears on the preview view
Here's my layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
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:background="#000000"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<Button
android:id="#+id/camera_capture_button"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="#drawable/ic_baseline_camera_24"
android:elevation="2dp"
android:layout_marginBottom="20dp"
android:scaleType="fitCenter"
android:layout_gravity="bottom|center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.926" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello"
android:textSize="25dp" />
<androidx.camera.view.PreviewView
android:id="#+id/viewFinder"
android:layout_width="379dp"
android:layout_height="576dp"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.camera.view.PreviewView>
</FrameLayout>
Main Activity
Here's the main logic->
package com.arpit.cameraxdemo
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.CameraSelector
import androidx.camera.core.ImageCapture
import androidx.camera.core.Preview
import androidx.camera.lifecycle.ProcessCameraProvider
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*
import java.util.concurrent.ExecutorService
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private var imageCapture: ImageCapture? = null
private lateinit var cameraExecutor: ExecutorService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()
if (allPermissionsGranted()) {
startCamera()
} else {
ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
}
camera_capture_button.setOnClickListener {
takePhoto()
}
cameraExecutor = Executors.newSingleThreadExecutor()
}
private fun takePhoto() {
}
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(viewFinder.surfaceProvider)
}
imageCapture = ImageCapture.Builder().build()
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
cameraProvider.unbindAll()
cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture
)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}
private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
ContextCompat.checkSelfPermission(baseContext, it) == PackageManager.PERMISSION_GRANTED
}
// checks the camera permission
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults:
IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == REQUEST_CODE_PERMISSIONS) {
if (allPermissionsGranted()) {
startCamera()
} else {
Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show()
finish()
}
}
}
companion object {
private const val TAG = "CameraXGFG"
private const val REQUEST_CODE_PERMISSIONS = 20
private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA)
}
override fun onDestroy() {
super.onDestroy()
cameraExecutor.shutdown()
}
}
Try below code with RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<androidx.camera.view.PreviewView
android:id="#+id/previewView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="CameraX" />
<Button
android:id="#+id/imageCaptureBtn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:gravity="center"
android:text="Click Image" />
</RelativeLayout>
Thanks for your help everyone. I have found the answer to my question. Actually what I was looking for is called overlay
viewFinder.overlay.add(tvText)
use the element "elevation" with 2dp
I am trying to find the position of widgets but somehow it always returns 0. I have followed most suggestions but all of them still returns 0. I finally ended up with this, but still it returns a 0. I have read somewhere that the reason it returns a 0 is because I am calling for the position before onCreate() has finished parsing the layout specified in the accompanying .xml file.
Where then should I put the call to the positions then?
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fuelCalculationConstraintLayout"
tools:context=".FuelCalculation">
<ImageView
android:id="#+id/imageViewIconHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/icon_home" />
<TextView
android:id="#+id/textViewSettingValues"
android:text="Settings:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/imageViewIconHome"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:height="30dp"
android:width="100dp"/>
<TextView
android:id="#+id/textViewScratchPad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scratch Pad"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.fuelcalculation3
import android.content.Context
import android.content.Intent
import android.graphics.Point
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class FuelCalculation : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_fuel_calculation)
val updateTextViewSettingValues = findViewById<TextView>(R.id.textViewSettingValues)
val updateTextViewScratchPad = findViewById<TextView>(R.id.textViewScratchPad)
val imageIconHomeClick = findViewById<ImageView>(R.id.imageViewIconHome)
imageIconHomeClick.setOnClickListener(){
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}
fun View.getLocationOnScreen(): Point {
val location = IntArray(2)
this.getLocationOnScreen(location)
return Point(location[0], location[1])
}
val location = updateTextViewSettingValues.getLocationOnScreen()
val absX = location.x
val absY = location.y
val getTextViewSettingsRootView = updateTextViewSettingValues.rootView
val getTextViewSettingsValueWidth = updateTextViewSettingValues.width
val getTextViewSettingsValueTop = updateTextViewSettingValues.top
val getTextViewSettingsValueLeft = updateTextViewSettingValues.left
val getTextViewSettingsValueRight = updateTextViewSettingValues.right
val getTextViewSettingsValueMeasuredWidth = updateTextViewSettingValues.measuredWidth
val getTextViewSettingsValueA = updateTextViewSettingValues.measuredWidth
updateTextViewScratchPad.text = "Top=$getTextViewSettingsValueTop" // returns 0
updateTextViewScratchPad.text = "$absX" // returns 0
}
}
See Why getLocationOnScreen(location) always returns 0?. You are to use postDelayed or getViewTreeObserver to get view position after an activity has rendered. Maybe you should use requestLayout.
My Recyclerview sometimes needs a refresh to start displaying data, some times it need much time to display the data and sometimes it works like a charm. I even used the same adapter for 2 recycler views one of them displayed data immediately but the other didn't show at all like the the image below:
This problem started to show up when I added another recycler view but in a different fragment that used same adapter. I thought using same adapter was the problem so I created a new one but the problem is still there. Note: I'm using same layout for displaying the recycler view items too. Any Ideas would be really helpful.
Thanks
UPDATE: Codes
Adapter :
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.ecommapp.R
import com.example.ecommapp.main_activity.data.Product
import kotlinx.android.synthetic.main.product_item_layout.view.*
class ProductItemAdapter(val listener: ProductItemAdapter.onItemClickListener) : ListAdapter<Product, ProductItemAdapter.ProductViewHolder>(Comparator()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProductViewHolder {
val inflater = LayoutInflater.from(parent.context).inflate(R.layout.product_item_layout, parent, false)
return ProductViewHolder(inflater)
}
override fun onBindViewHolder(holder: ProductViewHolder, position: Int) {
val currentItem = getItem(position)
if (currentItem != null) {
holder.bind(currentItem)
}
}
inner class ProductViewHolder(view : View): RecyclerView.ViewHolder(view){
init {
view.setOnClickListener{
val position = adapterPosition
val product = getItem(position)
if(position != RecyclerView.NO_POSITION){
listener.onItemClick(product)
}
}
}
private val container = view.container_main
private val imageView = view.img_product_item_not_card
private val productName = view.tv_product_item_label
private val productNewPrice = view.tv_product_item_new_price
private val productOldPrice = view.tv_product_item_old_price
fun bind(product: Product) {
productName.text = product.title
productNewPrice.text = product.price
/*var temp = (product.price as Double ) * 1.5
var oldPrice = temp as String
productOldPrice.text = oldPrice
productOldPrice.paintFlags = productOldPrice.paintFlags or Paint.STRIKE_THRU_TEXT_FLAG*/
//Context of the view
Glide.with(imageView.context) // Context
.load(product.image) // Data
.into(imageView) // View
}
}
interface onItemClickListener{
fun onItemClick(product : Product)
}
class Comparator : DiffUtil.ItemCallback<Product>() {
override fun areItemsTheSame(oldItem: Product, newItem: Product) =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Product, newItem: Product) =
oldItem == newItem
}
}
View Model:
import android.content.ContentValues.TAG
import android.util.Log
import androidx.lifecycle.*
import com.example.ecommapp.main_activity.data.Product
import com.example.ecommapp.main_activity.retrofit.ProductsApi
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import javax.inject.Inject
#HiltViewModel
class HomeViewModel #Inject constructor(
private val savedStateHandle: SavedStateHandle,
val api : ProductsApi
) : ViewModel() {
var new_collection_list : MutableLiveData<List<Product>>
var best_sellling_list : MutableLiveData<List<Product>>
init {
new_collection_list = MutableLiveData()
best_sellling_list = MutableLiveData()
get_best_selling_data()
get_new_collection_data()
}
fun get_new_collection_data(){
var call = api.get_products_desc()
call.enqueue(object : Callback<List<Product>> {
override fun onResponse(call: Call<List<Product>>, response: Response<List<Product>>) {
if (response.body() != null){
new_collection_list.postValue(response.body())
Log.d(TAG, "onResponse: Success Response")
}
else{
new_collection_list.postValue(null)
Log.d(TAG, "onResponse: Null Response")
}
}
override fun onFailure(call: Call<List<Product>>, t: Throwable) {
Log.d(TAG, "onFailure: Failure Response")
}
})
}
fun get_best_selling_data() {
var call = api.get_products_asc()
call.enqueue(object : Callback<List<Product>> {
override fun onResponse(call: Call<List<Product>>, response: Response<List<Product>>) {
if (response.body() != null){
best_sellling_list.postValue(response.body())
Log.d(TAG, "onResponse: Success Response")
}
else{
best_sellling_list.postValue(null)
Log.d(TAG, "onResponse: Null Response")
}
}
override fun onFailure(call: Call<List<Product>>, t: Throwable) {
Log.d(TAG, "onFailure: Failure Response")
}
})
}
fun on_swipe_refresh(){
get_new_collection_data()
get_best_selling_data()
}
}
Fragment :
package com.example.ecommapp.main_activity.fragments.home_fragment
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.example.ecommapp.R
import com.example.ecommapp.main_activity.data.Product
import com.example.ecommapp.main_activity.shared_files.recycler_view_adapters.ProductItemAdapter
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.android.synthetic.main.home_fragment_layout.*
#AndroidEntryPoint
class HomeFragment: Fragment(R.layout.home_fragment_layout), ProductItemAdapter.onItemClickListener {
lateinit var new_collection_list : LiveData<List<Product>>
lateinit var best_selling_list : LiveData<List<Product>>
val new_collections_adapter = ProductItemAdapter(this)
val best_selling_adapter = ProductItemAdapter(this)
val viewModel : HomeViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// New Collection Recycler View Setup
rv_new_collections.layoutManager = LinearLayoutManager(
activity?.applicationContext,
LinearLayoutManager.HORIZONTAL,
false
)
rv_new_collections.adapter = new_collections_adapter
// Best Selling Recycler View Setup
rv_best_selling.layoutManager = LinearLayoutManager(
activity?.applicationContext,
LinearLayoutManager.HORIZONTAL,
false
)
rv_best_selling.adapter = best_selling_adapter
//
set_data()
Handler().postDelayed({
if ( isConnected(activity?.applicationContext) ){
layout_new_collection_shimmer.visibility = View.INVISIBLE
rv_new_collections.visibility = View.VISIBLE
layout_best_selling_shimmer.visibility = View.INVISIBLE
rv_best_selling.visibility = View.VISIBLE
set_data()
}
else{
Toast.makeText(activity?.applicationContext, "No Internet Connection, Swipe to reload.", Toast.LENGTH_LONG )
.show()
}
}, 2000)
container_swipe.setOnRefreshListener(object : SwipeRefreshLayout.OnRefreshListener {
override fun onRefresh() {
if ( isConnected(activity?.applicationContext) ){
if( layout_new_collection_shimmer.visibility == View.INVISIBLE){
layout_new_collection_shimmer.visibility = View.INVISIBLE
rv_new_collections.visibility = View.VISIBLE
layout_best_selling_shimmer.visibility = View.INVISIBLE
rv_best_selling.visibility = View.VISIBLE
}
viewModel.on_swipe_refresh()
set_data()
}
else{
Toast.makeText(activity?.applicationContext, "No Internet Connection, Swipe to reload.", Toast.LENGTH_LONG )
.show()
}
// Must be added
container_swipe.isRefreshing = false
}
})
}
fun set_data(){
new_collection_list = viewModel.new_collection_list
best_selling_list = viewModel.best_sellling_list
new_collection_list.observe(viewLifecycleOwner, Observer {
new_collections_adapter.submitList(it)
new_collections_adapter.notifyDataSetChanged()
})
best_selling_list.observe(viewLifecycleOwner, Observer {
best_selling_adapter.submitList(it)
best_selling_adapter.notifyDataSetChanged()
})
}
fun isConnected(ctx: Context?): Boolean {
val hasInternet: Boolean
val connectivityManager =
ctx?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
val networkCapabilities = connectivityManager.activeNetwork ?: return false
val actNw =
connectivityManager.getNetworkCapabilities(networkCapabilities) ?: return false
hasInternet = when {
actNw.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
actNw.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
else -> false
}
} else {
hasInternet = try {
if (connectivityManager.activeNetworkInfo == null) {
false
} else {
connectivityManager.activeNetworkInfo?.isConnected!!
}
} catch (e: Exception) {
false
}
}
return hasInternet}
override fun onItemClick(product : Product) {
val action = HomeFragmentDirections.actionHomeFragmentToProductFragment(product, product.title, R.id.homeFragment)
findNavController().navigate(action)
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/container_swipe"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".main_activity.fragments.home_fragment.HomeFragment"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.SearchView
android:id="#+id/search_view"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintWidth_percent="0.9"
android:elevation="2dp"
android:outlineProvider="bounds"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"/>
<androidx.cardview.widget.CardView
android:id="#+id/img_advertisment"
android:layout_width="0dp"
android:layout_height="180dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/search_view"
app:layout_constraintWidth_percent="0.9"
android:background="#00000000"
app:cardElevation="0dp"
app:cardCornerRadius="10dp"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/advertisment"
/>
</androidx.cardview.widget.CardView>
<RelativeLayout
android:id="#+id/container_new_collection"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/img_advertisment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.9"
android:layout_marginTop="20dp"
>
<TextView
android:id="#+id/tv_new_collection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Collections"
android:textColor="#color/tomato"
android:textSize="25dp"
android:fontFamily="sans-serif-black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show All"
android:textSize="20dp"
android:textColor="#c6c4ce"
android:fontFamily="sans-serif-black"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"/>
<com.facebook.shimmer.ShimmerFrameLayout
android:id="#+id/layout_new_collection_shimmer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:shimmer_repeat_mode="restart"
app:shimmer_shape="linear"
android:layout_below="#id/tv_new_collection"
android:layout_marginTop="10dp">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
</LinearLayout>
</HorizontalScrollView>
</com.facebook.shimmer.ShimmerFrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_marginTop="10dp"
android:id="#+id/rv_new_collections"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_below="#id/tv_new_collection"
tools:listitem="#layout/product_item_layout"
android:visibility="invisible"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/container_best_selling"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/container_new_collection"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintWidth_percent="0.9"
android:layout_marginTop="20dp"
>
<TextView
android:id="#+id/tv_best_selling"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Best Selling"
android:textColor="#color/tomato"
android:textSize="25dp"
android:fontFamily="sans-serif-black"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show All"
android:textSize="20dp"
android:textColor="#c6c4ce"
android:fontFamily="sans-serif-black"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
/>
<com.facebook.shimmer.ShimmerFrameLayout
android:id="#+id/layout_best_selling_shimmer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:shimmer_repeat_mode="restart"
app:shimmer_shape="linear"
android:layout_marginTop="10dp"
android:layout_below="#id/tv_best_selling">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
<include layout="#layout/product_shimmer_layout"></include>
</LinearLayout>
</HorizontalScrollView>
</com.facebook.shimmer.ShimmerFrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_best_selling"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_below="#id/tv_best_selling"
tools:listitem="#layout/product_item_layout"
android:layout_marginTop="10dp"
android:visibility="invisible"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
I Figured out what is the problem. The API takes much time to send the data. So, the recycler view renders the view with null values.
I have two activities-
A. Product List
B. Product Detail
Whenever user clicks on product item, he goes to the product detail activity where an API is called which shows detail of that product.
Now the problem is, that for the first time, the views in Product Detail initially have no data and i am inflating the data in views using livedata in viewmodel, with the help of data-binding. Now when the user navigates back to Product List , finish is called on Product Detail Activity.
Mow when user again clicks on another product, this time when he is taken to the product detail screen, old product detail briefly appears before new product detail is fetched from API and binded in the views. I don't know what is causing this behavior. Can somebody point me in the right direction ?
I have already tried calling finish and System.gc, but somehow the view holds on to the old data before new data is inflated in it.
Product List Activity
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.core.view.GravityCompat
import androidx.appcompat.app.ActionBarDrawerToggle
import android.view.MenuItem
import android.view.View
import androidx.drawerlayout.widget.DrawerLayout
import com.google.android.material.navigation.NavigationView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.app.thingle.R
import com.app.thingle.databinding.ActivitySideMenuBinding
import com.app.thingle.data.model.dashBoardDataPackage.DashBoardStatus
import com.app.thingle.data.model.dashBoardDataPackage.Product
import com.app.thingle.utility.Constants
import com.app.thingle.viewModel.DashboardViewModel
import kotlinx.android.synthetic.main.activity_side_menu.view.*
import kotlinx.android.synthetic.main.app_bar_side_menu.view.*
import java.util.ArrayList
class SideMenuActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
lateinit var sideMenuBinding: ActivitySideMenuBinding
lateinit var dashboardViewModel: DashboardViewModel
private lateinit var cartTextView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
sideMenuBinding = DataBindingUtil.setContentView(this, R.layout.activity_side_menu)
dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)
sideMenuBinding.viewModel = dashboardViewModel
dashboardViewModel.getProductsFromServer()
sideMenuBinding.navView.setNavigationItemSelectedListener(this)
val toggle = ActionBarDrawerToggle(
this,
sideMenuBinding.drawerLayout,
sideMenuBinding.appBarSideMenu.mainToolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
toggle.isDrawerIndicatorEnabled = false
sideMenuBinding.drawerLayout.addDrawerListener(toggle)
toggle.syncState()
if (!dashboardViewModel.isUser()) {
sideMenuBinding.drawerLayout.nav_view.menu.findItem(R.id.nav_log_out).isVisible = false
}
sideMenuBinding.appBarSideMenu.mainToolbar.toolbar_action_holder.menu_icon_id.setOnClickListener {
sideMenuBinding.drawerLayout.openDrawer(GravityCompat.START)
}
cartTextView =
sideMenuBinding.appBarSideMenu.mainToolbar.toolbar_action_holder.cart_holder.cart_amount
setUpBindings()
}
private fun setUpBindings() {
handleActivityStatus()
handleLoaderState()
handleApiResponse()
setUpListUpdate()
setUpCartStatus()
setUpListClicks()
}
private fun setUpListUpdate() {
dashboardViewModel.getProductsByType().observe(this, Observer {
dashboardViewModel.setItemsInAdapter(it)
})
}
private fun setUpCartStatus() {
dashboardViewModel.getCartCount().observe(this, Observer {
if (it > 0) {
cartTextView.visibility = View.VISIBLE
cartTextView.text = it.toString()
}
})
}
private fun handleApiResponse() {
dashboardViewModel.getApiResponse().observe(this, Observer { handleResponse(it) })
}
private fun handleLoaderState() {
dashboardViewModel.getUiState().observe(this, Observer { handleState(it) })
}
private fun handleActivityStatus() {
dashboardViewModel.getDashboardStatus().observe(this, Observer {
when (it) {
DashBoardStatus.WRONG_PRODUCT_TYPE -> {
sideMenuBinding.appBarSideMenu.mainContent.noResultFoundLayout.visibility =
View.VISIBLE
sideMenuBinding.appBarSideMenu.mainContent.recyclerText.visibility = View.GONE
}
DashBoardStatus.GO_TO_CART_SCREEN -> {
showToast("Go to Cart")
}
DashBoardStatus.GO_TO_SEARCH_SCREEN -> {
this.startActivity(
Intent(this, SearchScreen::class.java)
)
}
DashBoardStatus.EMPTY_LIST -> {
sideMenuBinding.appBarSideMenu.mainContent.noResultFoundLayout.visibility =
View.VISIBLE
sideMenuBinding.appBarSideMenu.mainContent.recyclerText.visibility = View.GONE
}
DashBoardStatus.FETCH_API_SUCCESS -> {
sideMenuBinding.appBarSideMenu.mainContent.recyclerText.visibility =
View.VISIBLE
}
DashBoardStatus.FETCH_API_ERROR -> {
sideMenuBinding.appBarSideMenu.mainContent.noResultFoundLayout.visibility =
View.VISIBLE
sideMenuBinding.appBarSideMenu.mainContent.recyclerText.visibility = View.GONE
}
DashBoardStatus.NO_CONNECTION -> {
showToast(getString(R.string.no_network))
}
else -> showToast(getString(R.string.something_went_wrong))
}
})
}
private fun setUpListClicks() {
dashboardViewModel.getSelectedProductType().observe(this, Observer {
this.startActivity(
Intent(this, BooksCategoryActivity::class.java)
.putParcelableArrayListExtra(
Constants.PRODUCT_LIST,
it.Products as ArrayList<Product>
)
.putExtra(Constants.CATEGORY_NAME, it.name)
.putExtra(Constants.ID, it.id)
)
})
dashboardViewModel.getSelectedProduct().observe(this, Observer {
this.startActivity(
Intent(this, BookDetailActivity::class.java)
.putExtra(Constants.PRODUCT_ID, it.id)
)
})
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_borrows -> {
if (dashboardViewModel.isUser()) {
this.startActivity(Intent(this, BorrowHistoryActivity::class.java))
} else {
goToLogin()
}
}
R.id.nav_contributions -> {
if (dashboardViewModel.isUser()) {
this.startActivity(Intent(this, ContributionHistoryActivity::class.java))
} else {
goToLogin()
}
}
R.id.nav_profile -> {
if (dashboardViewModel.isUser()) {
this.startActivity(Intent(this, EditProfileActivity::class.java))
} else {
goToLogin()
}
}
R.id.nav_help_contact -> {
if (dashboardViewModel.isUser()) {
this.startActivity(Intent(this, ContactUsActivity::class.java))
} else {
goToLogin()
}
}
R.id.nav_log_out -> {
logOut()
}
}
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
drawerLayout.closeDrawer(GravityCompat.START)
return true
}
private fun goToLogin() {
this.startActivity(Intent(this, LoginActivity::class.java))
finishAffinity()
}
private fun logOut() {
dashboardViewModel.clearUserData()
goToLogin()
}
override fun onBackPressed() {
val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START)
} else {
this.startActivity(Intent(this, ChooseTypeActivity::class.java))
killActivity()
}
}
private fun killActivity() {
this.finishAffinity()
}
}
layout- Product Detail
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="viewModel"
type="com.app.thingle.viewModel.ProductDetailViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout_holder"
tools:context=".ui.activities.BookDetailActivity">
<include
android:id="#+id/about_book_toolbar"
layout="#layout/thingle_search_and_cart_toolbar" />
<FrameLayout
android:id="#+id/product_not_available_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="3dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/about_book_toolbar">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightPink"
android:fontFamily="#font/avenirltstd_book"
android:gravity="center"
android:padding="5dp"
android:visibility="gone"
android:text="#string/this_product_is_currently_unavailable"
android:textColor="#color/white"
android:textSize="14sp" />
</FrameLayout>
<ScrollView
android:id="#+id/about_product_scrollView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/product_not_available_holder">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/book_image_id"
android:layout_width="100dp"
android:layout_height="140dp"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/book_name_id"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0.1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:fontFamily="#font/avenirltstd_black"
android:textColor="#color/black"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/book_image_id"
app:layout_constraintTop_toTopOf="#id/book_image_id"
app:layout_constraintBottom_toTopOf="#id/book_intro"
/>
<TextView
android:id="#+id/book_intro"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:fontFamily="#font/avenirltstd_book"
android:textColor="#color/black"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/book_image_id"
app:layout_constraintTop_toBottomOf="#id/book_name_id"
app:layout_constraintBottom_toBottomOf="#id/book_image_id"
/>
<Button
android:id="#+id/add_cart_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="#drawable/pink_button_border"
android:fontFamily="#font/avenirltstd_book"
android:onClick="#{viewModel::onAddCartButtonClicked}"
android:text="#string/add_to_cart"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="18sp"
app:layout_constraintEnd_toStartOf="#id/share_id"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/book_image_id" />
<Button
android:id="#+id/share_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:background="#drawable/pink_button_border"
android:fontFamily="#font/avenirltstd_book"
android:onClick="#{viewModel::onShareButtonClicked}"
android:text="#string/share"
android:textAllCaps="false"
android:textColor="#color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="#id/add_cart_id"
app:layout_constraintTop_toBottomOf="#+id/book_image_id" />
<View
android:id="#+id/line_id"
android:layout_width="0dp"
android:layout_height="0.1dp"
android:layout_marginTop="20dp"
android:background="#color/grey"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/add_cart_id" />
<TextView
android:id="#+id/about_book_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:fontFamily="#font/avenirltstd_book"
android:text="#string/about"
android:textColor="#color/black"
android:textSize="22sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/line_id" />
<TextView
android:id="#+id/text_info_book"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:fontFamily="#font/avenirltstd_book"
android:lineSpacingExtra="#dimen/line_spacing"
android:textColor="#color/lightBlack"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/about_book_info" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Product Detail Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.app.thingle.R
import com.app.thingle.data.model.bookDetailDataPackage.ProductDetailStatus
import com.app.thingle.databinding.ActivityBookDetailBinding
import com.app.thingle.utility.Constants
import com.app.thingle.viewModel.ProductDetailViewModel
import kotlinx.android.synthetic.main.thingle_search_and_cart_toolbar.view.*
import android.view.KeyEvent.KEYCODE_BACK
import android.view.KeyEvent
import androidx.lifecycle.ViewModel
class BookDetailActivity : BaseActivity() {
private lateinit var bookDetailBinding: ActivityBookDetailBinding
private lateinit var productDetailViewModel: ProductDetailViewModel
private lateinit var cartTextView: TextView
private lateinit var toolBarTitle: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bookDetailBinding = DataBindingUtil.setContentView(this, R.layout.activity_book_detail)
productDetailViewModel = ViewModelProviders.of(this).get(ProductDetailViewModel::class.java)
bookDetailBinding.viewModel = productDetailViewModel
productDetailViewModel.getProductById(intent.getIntExtra(Constants.PRODUCT_ID, -1))
setUpBindings()
setUpToolbar()
}
private fun setUpToolbar() {
toolBarTitle = bookDetailBinding.aboutBookToolbar.toolbar_action_holder.thingle_toolbar_text
toolBarTitle.text = getString(R.string.about_Book)
bookDetailBinding.aboutBookToolbar.toolbar_action_holder.thingle_logo_toolbar_back.setOnClickListener { onBackPressed() }
bookDetailBinding.aboutBookToolbar.toolbar_action_holder.toolbar_search_id.setOnClickListener {
this.startActivity(
Intent(this, SearchScreen::class.java)
)
finish()
}
bookDetailBinding.aboutBookToolbar.toolbar_action_holder.cart_id.setOnClickListener {
showToast(
"Go To Cart"
)
}
cartTextView =
bookDetailBinding.aboutBookToolbar.toolbar_action_holder.cart_holder.cart_amount
}
private fun setUpBindings() {
setUpValues()
handleLoaderState()
handleApiResponse()
setUpCartStatus()
handleActivityStatus()
setUpButtonClick()
}
private fun setUpValues() {
productDetailViewModel.getProduct().observe(this, Observer {
if (!it.isAvailable) {
bookDetailBinding.productNotAvailableHolder.visibility = View.VISIBLE
bookDetailBinding.addCartId.visibility = View.GONE
}
bookDetailBinding.bookNameId.text = it.title
bookDetailBinding.bookIntro.text = it.shortDescription
bookDetailBinding.textInfoBook.text = it.description
if (it.ProductImages!!.isNotEmpty()) {
if (it.ProductImages[0].imageUrl != null) {
setImageUrlOnImageView(
bookDetailBinding.bookImageId,
it.ProductImages[0].imageUrl
)
}
}
})
}
private fun setUpButtonClick() {
productDetailViewModel.getProductId().observe(this, Observer {
showToast(it.toString())
})
}
private fun handleActivityStatus() {
productDetailViewModel.getActivityStatus().observe(this, Observer {
when (it) {
ProductDetailStatus.FETCH_API_SUCCESS -> {
}
ProductDetailStatus.FETCH_API_ERROR -> {
showToast(getString(R.string.something_went_wrong))
onBackPressed()
}
ProductDetailStatus.NO_CONNECTION -> {
showToast(getString(R.string.no_network))
}
ProductDetailStatus.EMPTY_DATA -> {
showToast(getString(R.string.no_data))
onBackPressed()
}
ProductDetailStatus.SHARE_BUTTON_CLICKED -> {
showToast("Share")
}
//ProductDetailStatus.ADD_CART_BUTTON_CLICKED->{showToast("Add To Cart")}
else -> {
showToast(getString(R.string.something_went_wrong))
}
}
})
}
private fun setUpCartStatus() {
productDetailViewModel.getCartCount().observe(this, Observer {
if (it > 0) {
cartTextView.visibility = View.VISIBLE
cartTextView.text = it.toString()
}
})
}
private fun handleApiResponse() {
productDetailViewModel.getApiResponse().observe(this, Observer { handleResponse(it) })
}
private fun handleLoaderState() {
productDetailViewModel.getUiState().observe(this, Observer { handleState(it) })
}
override fun onBackPressed() {
super.onBackPressed()
killActivity()
}
private fun killActivity() {
finish()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
if (keyCode == KEYCODE_BACK) {
killActivity()
}
return super.onKeyDown(keyCode, event)
}
}
Product Detail Viewmodel
import android.app.Application
import android.view.View
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.app.thingle.data.model.bookDetailDataPackage.ProductDetailStatus
import com.app.thingle.data.model.bookDetailDataPackage.ResponseObject
import com.app.thingle.data.repository.CartRepository
import com.app.thingle.data.repository.ProductDetailRepository
import com.app.thingle.utility.ApiResponseCallback
import com.app.thingle.utility.ConnectionDetector
import com.app.thingle.utility.SingleLiveEvent
class ProductDetailViewModel(application: Application) : BaseViewModel(application) {
private var product: MutableLiveData<ResponseObject> = MutableLiveData()
private var productId: MutableLiveData<Int> = MutableLiveData()
private var userCartItems =
MutableLiveData<List<com.app.thingle.data.model.cartDataPackage.ResponseObject>>()
var cartCount: MutableLiveData<Int> = MutableLiveData()
private val productDetailStatus = SingleLiveEvent<ProductDetailStatus>()
fun getProductById(productId: Int) {
if (product.value == null && productId != -1) {
fetchProductsByType(productId)
} else {
productDetailStatus.value = ProductDetailStatus.EMPTY_DATA
}
}
private fun fetchProductsByType(id: Int) {
if (ConnectionDetector.getInstance(context).isConnectionAvailable()) {
setLoaderState(true)
product = ProductDetailRepository.instance.fetchProductById(
id,
context,
object : ApiResponseCallback {
override fun provideResponse(
isApiError: Boolean,
isResponseError: Boolean,
response: String
) {
if (isApiError || isResponseError) {
setLoaderState(false)
productDetailStatus.value = ProductDetailStatus.FETCH_API_ERROR
} else {
productDetailStatus.value = ProductDetailStatus.FETCH_API_SUCCESS
if (product.value == null) {
productDetailStatus.value = ProductDetailStatus.EMPTY_DATA
}
if (isUser()) {
fetchCartForUser()
} else {
setLoaderState(false)
cartCount.value = 0
}
}
}
})
} else {
productDetailStatus.value = ProductDetailStatus.NO_CONNECTION
}
}
private fun fetchCartForUser() {
userCartItems =
CartRepository.instance.fetchCartForUser(context, object : ApiResponseCallback {
override fun provideResponse(
isApiError: Boolean,
isResponseError: Boolean,
response: String
) {
setLoaderState(false)
if (isApiError || isResponseError) {
cartCount.value = 0
} else {
if (userCartItems.value!!.isNotEmpty()) {
cartCount.value = userCartItems.value!!.size
}
}
}
})
}
fun getProductId(): LiveData<Int> {
return productId
}
fun getProduct(): LiveData<ResponseObject> {
return product
}
fun getCartCount(): LiveData<Int> {
return cartCount
}
fun getActivityStatus(): SingleLiveEvent<ProductDetailStatus> {
return productDetailStatus
}
fun onAddCartButtonClicked(v: View) {
productDetailStatus.value = ProductDetailStatus.ADD_CART_BUTTON_CLICKED
productId.value = product.value!!.id
}
fun onShareButtonClicked(v: View) {
productDetailStatus.value = ProductDetailStatus.SHARE_BUTTON_CLICKED
}
}
I found out the solution by implementing onCleared() inside my viewmodel and clearing my viewmodel objects.
super.onCreate(null) instead of super.onCreate(savedInstanceState) should do the trick.
You might want to put in some code to check if you want this to happen (ie. last instance was finished) or not (ie. you navigated away and system cleaned up your activity which it has to restart)