Reputation: 301
I created a recycleView in my main activity also, a textView which is use to display text if list passed to recycleView is empty. I have given a delete button on card of recycleView. When I open app and if list is empty then textView for empty view is visible and recycleView is invisible. But when I add some elements and delete all element then that textView for empty text is not visible. How can I resolve this?
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="57dp"
android:layout_height="64dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="40dp"
android:clickable="true"
android:onClick="addNewCredentials"
app:backgroundTint="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/add"
app:fabSize="normal"
android:scaleType="center" />
<TextView
android:id="@+id/emptyView"
android:layout_width="326dp"
android:layout_height="71dp"
android:gravity="center_horizontal|center_vertical"
android:text="@string/empty_data"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/recyclerView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
</androidx.recyclerview.widget.RecyclerView>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.passwordmanager
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL,false)
val db = DataBaseHandler(this)
val detailsData = ArrayList(db.readCredentials())
val adapter = CredentialAdapter(this,detailsData)
recyclerView.adapter = adapter
if(detailsData.isEmpty()){
recyclerView.visibility = View.INVISIBLE
findViewById<TextView>(R.id.emptyView).visibility = View.VISIBLE
}
else{
recyclerView.visibility = View.VISIBLE
findViewById<TextView>(R.id.emptyView).visibility = View.INVISIBLE
}
}
fun addNewCredentials(view : View){
print("hello world")
val intent = Intent(this, AddDetailActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
}
}
CredentialAdapter.kt
package com.example.passwordmanager
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.renderscript.RenderScript
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat.getSystemService
import androidx.recyclerview.widget.RecyclerView
import android.content.Context.CLIPBOARD_SERVICE as CLIPBOARD_SERVICE1
//private var items: MutableList<CredentialsModel>,
class CredentialAdapter(ctx: Context, val credentialList : ArrayList<CredentialsModel>): RecyclerView.Adapter<CredentialAdapter.ViewHolder>() {
var context = ctx
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val urlView = itemView.findViewById(R.id.urlView) as TextView
val userNameView = itemView.findViewById(R.id.userNameView) as TextView
val passwordView = itemView.findViewById(R.id.passwordView) as TextView
val noteView = itemView.findViewById(R.id.noteView) as TextView
val delButton = itemView.findViewById(R.id.delButton) as Button
val cpyButton = itemView.findViewById(R.id.cpyButton) as Button
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.list_item_layout, parent, false)
)
}
override fun getItemCount(): Int {
return credentialList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val credential: CredentialsModel = credentialList[position]
holder.urlView.text = credential.url
holder.userNameView.text = credential.userName
holder.passwordView.text = credential.password
holder.noteView.text = credential.note
holder.delButton.setOnClickListener {
val db = DataBaseHandler(context)
if (db.deleteData(credential.id)) {
credentialList.removeAt(holder.getAdapterPosition())
notifyItemRemoved(position)
}
}
holder.cpyButton.setOnClickListener {
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipUser = ClipData.newPlainText("username", credential.userName)
clipboard.setPrimaryClip(clipUser)
Toast.makeText(context, "Copied username to clipboard", Toast.LENGTH_SHORT).show()
val clipPass = ClipData.newPlainText("password", credential.password)
clipboard.setPrimaryClip(clipPass)
Toast.makeText(context, "Copied password to clipboard", Toast.LENGTH_SHORT).show()
}
}
}
Upvotes: 0
Views: 178
Reputation: 160
Easily check your list size in your delete function and make emptyText visible when the list size is zero. Sorry for Java code. Write this function in main activity
public void switchVisiblity(){
emptyTextView.setVisibility(View.VISIBLE);
}
Then call this method in the adapter delete function
holder.delButton.setOnClickListener {
//write this below your code
if(credentialList.size()==0){
((MainActivity)ctx).switchVisiblity();
}
}
Upvotes: 1