I want to implement this adding by kotlin language. In steps:
- For each data needs to add, the user clicks on
Add other deposit
, represent the first area to allow add a new data, andAdd other deposit
button goes to down. - And add feature if the user wants to cancel this adding, he just click on
×
button to cancel and theAdd other deposit
button return to up again. - The entered data in
EditText
and selected choice inRadioButton
to be hold in a variable and submit it byFloatingActionButton
blew left .
Thanks so much for your efforts in advance
CodePudding user response:
this can be done with create a list and when you click on the Add other deposit button you add a new item to the list using recyclerview adapter. One problem with the input fields, i save information when the user presses on the keyboard the done button, it is also possible to save data from the field when the focus is lost, decided that it is better to save after pressing.
package com.myply.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnAddOtherDeposit = findViewById<TextView>(R.id.btn_add_other_deposit)
val rvCustomers = findViewById<RecyclerView>(R.id.rv_customers)
val fabDone = findViewById<FloatingActionButton>(R.id.fab_done)
val adapter = MyRecyclerAdapter(this, arrayListOf(CustomerModel()))
rvCustomers.adapter = adapter
rvCustomers.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter.mClickListener = object : MyRecyclerAdapter.ItemClickListener {
override fun onItemRemoveClicked(position: Int) {
adapter.removeAt(position)
}
}
btnAddOtherDeposit.setOnClickListener {
/*add empty model without information */
adapter.add(CustomerModel())
}
fabDone.setOnClickListener {
/*collect all data*/
var customers = adapter.data
}
}
}
adapter
package com.myply.myapplication
import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyRecyclerAdapter internal constructor(
val context: Context?,
val data: MutableList<CustomerModel>
) : RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>() {
var mClickListener: ItemClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_customer, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val customer = data[position]
holder.editName.setText(customer.name)
holder.rbDeposit.setOnCheckedChangeListener(null)
holder.rbCheque.setOnCheckedChangeListener(null)
holder.rbDeposit.isChecked = customer.depositType == "Deposit"
holder.rbCheque.isChecked = customer.depositType == "Cheque"
holder.btnRemove.setOnClickListener { mClickListener?.onItemRemoveClicked(position) }
holder.editName.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(p0: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event?.action == KeyEvent.ACTION_DOWN && event?.keyCode == KeyEvent.KEYCODE_ENTER) {
customer.name = holder.editName.text.toString()
holder.editName.clearFocus()
update(customer, position)
return true
}
return false
}
})
holder.rbDeposit.setOnCheckedChangeListener { compoundButton, b ->
customer.depositType = "Deposit"
update(customer, position)
}
holder.rbCheque.setOnCheckedChangeListener { compoundButton, b ->
customer.depositType = "Cheque"
update(customer, position)
}
}
override fun getItemCount(): Int {
return data.size
}
fun add(customer: CustomerModel) {
data.add(customer)
notifyItemInserted(data.size - 1)
}
fun update(customer: CustomerModel, position: Int) {
data[position] = customer
notifyItemChanged(position)
}
fun removeAt(position: Int) {
data.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, data.size)
}
inner class ViewHolder internal constructor(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var editName: EditText = itemView.findViewById(R.id.edit_name)
var btnRemove: ImageView = itemView.findViewById(R.id.btn_remove)
var rbDeposit: RadioButton = itemView.findViewById(R.id.rb_deposit)
var rbCheque: RadioButton = itemView.findViewById(R.id.rb_cheque)
}
interface ItemClickListener {
fun onItemRemoveClicked(position: Int)
}
}
item_customer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:id="@ id/edit_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@ id/rb_deposit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deposit type" />
<RadioButton
android:id="@ id/rb_cheque"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cheque" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@ id/btn_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.core.widget.NestedScrollView 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@ id/rv_customers"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@ id/btn_add_other_deposit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Add other deposit" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@ id/fab_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>