I am using PopupWindow with a custom view having 2 TextViews, I am trying to response against click event but click event is not triggering, following is the code that I am using
/* Get the binding object */
val binding: XYZBinding = XYZBinding.inflate(
LayoutInflater.from(applicationContext),
null,
false
)
binding.clickHandler = this
/* Set the popup menu window */
popupWindow = PopupWindow(binding.root)
popupWindow.contentView = LayoutInflater.from(applicationContext)
.inflate(R.layout.popup_menu_gang, null, false)
popupWindow.isTouchable = true
popupWindow.isFocusable = true
popupWindow.isOutsideTouchable = true
popupWindow.width = WindowManager.LayoutParams.WRAP_CONTENT
popupWindow.height = WindowManager.LayoutParams.WRAP_CONTENT
popupWindow.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
popupWindow.setOnDismissListener(PopupWindow.OnDismissListener {
window.attributes.alpha = alpha
window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
window.decorView.invalidate()
})
popupWindow.showAsDropDown(binding.layoutHeader.imageViewMenu)
I have also tried a direct onClickListener but it doesn't work as well
binding.textViewGangPopupRenameButtons.setOnClickListener(View.OnClickListener {
Toast.makeText(applicationContext, "working", Toast.LENGTH_SHORT).show()
})
Following is the xml I am using
<?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">
<data>
<import type="xyz.BaseActivity" />
<variable
name="clickHandler"
type="BaseActivity" />
</data>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:layout_marginEnd="16dp"
app:cardCornerRadius="16dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewGangPopupDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:fontFamily="#font/montserrat_medium"
android:onClick="#{(view) -> clickHandler.onClick(view)}"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:text="#string/delete"
android:textColor="#color/textColorBlue"
android:textSize="14sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textViewGangPopupRenameButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:fontFamily="#font/montserrat_medium"
android:onClick="#{(view) -> clickHandler.onClick(view)}"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="16dp"
android:text="#string/rename_buttons"
android:textColor="#color/textColorBlue"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewGangPopupDelete" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
</layout>
Set the Id of Your Text View with the PopupView.. Like this
` val layoutInflater = activity!!
.getSystemService(AppCompatActivity.LAYOUT_INFLATER_SERVICE) as
LayoutInflater
val popupView: View =
layoutInflater.inflate(R.layout.custom_grid_bottomnav, null)
val popupWindow = PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
// popupWindow.dismiss()
val tx = popupView.findViewById<TextView>(R.id.text1) //change
tx.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
//Do what you want
}
}`
Related
This code is where I am creating Alert dialog. whenever I press ok it crashes .password_dialog has 1 editText and 2 buttons.
private fun passwordCheck(position: Int){
val view=LayoutInflater.from(requireContext()).inflate(R.layout.password_dialog,null,false)
val builder=AlertDialog.Builder(requireContext())
with(builder){
setTitle("Enter your Pin")
setPositiveButton("Ok"){dialog,which->
if(pin_text.text.toString()=="1234"){
Toast.makeText(requireContext(),"Right Pin",Toast.LENGTH_LONG).show()
}
else{
pin_text.requestFocus()
pin_text.error="Incorrect"
}
}
setCancelable(false)
setNegativeButton("Cancel"){dialog,which->
dialog.dismiss()
}
setView(view)
show()
}
}
Create a layout with the view, for example, an EditText that only accepts numbers and two buttons, accept or cancel.
Then inflate the view
val view = LayoutInflater.from(context).inflate(R.id.layout, null, false)
Then with that view you get the reference of the buttons and the EditText with
val button = view.findViewById<Button>(R.id.button)
//I'm not going to declare all views, but assuming you already did.
button.setOnClickListener {
if (editText.text.toString.toInt() == 1234 //your correct pin) {
//CORRECT PIN TODO
} else {
editText.requestFocus()
editText.error = "Incorrect pin"
}
}
Then create the dialog and add that view to it
AlertDialog.Builder(context).setView(view).create().show()
Do not add the validation methods in the accept button of the dialog.
They are what are declared as follows.
setPositiveButton("Accept"){diálog, which -> }
This is the way by which you can create your own dialog with custom layout.
Implement your PIN UI in custom_dialog.xml and your logic in Kotlin file.
Here is example how you can create custom Dialog.
custom_dialog.xml
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:background="#424242">
<TextView
android:id="#+id/popup_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
android:text="Custom Dialog Box"
android:textColor="#color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<LinearLayout
android:id="#+id/linearLayoutOpt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16sp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16sp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/popup_dialog">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal">
<TextView
android:id="#+id/no_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="No"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
<Space
android:layout_width="32sp"
android:layout_height="12sp" />
<TextView
android:id="#+id/yes_opt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingTop="8dp"
android:paddingRight="8dp"
android:paddingBottom="18dp"
android:text="Yes"
android:textStyle="bold"
android:textAllCaps="false"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showDialog()
}
private fun showDialog() {
val customDialog = Dialog(this)
customDialog.setContentView(R.layout.custom_dialog)
customDialog.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
val yesBtn = customDialog.findViewById(R.id.yes_opt) as TextView
val noBtn = customDialog.findViewById(R.id.no_opt) as TextView
yesBtn.setOnClickListener {
//Do something here YOUR LOGIC
customDialog.dismiss()
}
noBtn.setOnClickListener {
customDialog.dismiss()
}
customDialog.show()
}
}
example app screenshot
So I'm fairly new to Kotlin.
I am trying to create an onClickListener on an image button to open the share interface, so that the particular video from the recyclerView can be shared via SMS, etc.
I followed various tutorials on how to do this as I am trying to execute this within a fragment, and the app just keeps crashing when I try to open the fragment in question.
I get the following error
java.lang.ClassCastException: java.lang.Integer cannot be cast to android.widget.ImageButton
Here is what my fragment code looks like:
SearchFragment.kt
class SearchFragment : Fragment(), View.OnClickListener
{
private var layoutManager: RecyclerView.LayoutManager? = null
private var adapter: RecyclerView.Adapter<ClipAdapter.ViewHolder>? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View
{
val rootView = inflater.inflate(R.layout.fragment_search, container, false)
loadData()
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
{
val shareBtn = view.findViewById<ImageButton>(R.id.button_to_share)
shareBtn.setOnClickListener(this)
}
private fun loadData()
{
val service = TwitchServiceBuilder.buildService(TwitchService::class.java)
val requestCall = service.getClips("anerdfails")
requestCall.enqueue(object : Callback<List<Clip>>
{
override fun onResponse(
call: Call<List<Clip>>,
response: Response<List<Clip>>
)
{
if (response.isSuccessful)
{
//process data
recyclerView.layoutManager = GridLayoutManager(activity, 2)
recyclerView.adapter = ClipAdapter(response.body()!!)
} else
{
//output alert
AlertDialog.Builder(activity!!)
.setTitle("API error")
.setMessage("Response, but something went wrong ${response.message()}")
.setPositiveButton(android.R.string.ok) { _, _ -> }
.setIcon(android.R.drawable.ic_dialog_alert)
.show()
}
}
override fun onFailure(call: Call<List<Clip>>, t: Throwable)
{
//process failure
AlertDialog.Builder(activity!!)
.setTitle("API error")
.setMessage("No response, and something went wrong $t")
.setPositiveButton(android.R.string.ok) { _, _ -> }
.setIcon(android.R.drawable.ic_dialog_alert)
.show()
}
})
}
override fun onClick(v: View?)
{
Toast.makeText(activity, "Its toast!", Toast.LENGTH_SHORT).show()
}
}
And here are my 2 layouts for the RecyclerView:
fragment_search.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"
android:paddingStart="15dp"
android:paddingTop="?attr/actionBarSize"
android:paddingEnd="15dp"
tools:context=".ui.search.SearchFragment">
<androidx.appcompat.widget.SearchView
android:background="#drawable/search_bar"
android:id="#+id/clipSearch"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="5dp"
android:focusable="true"
android:focusableInTouchMode="true"
app:layout_constraintBottom_toTopOf="#+id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
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_marginTop="15dp"
android:layout_marginBottom="75dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/clipSearch" />
</androidx.constraintlayout.widget.ConstraintLayout>
clip_layout.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="wrap_content"
tools:context=".ui.search.SearchFragment">
<VideoView
android:id="#+id/videoClip"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintDimensionRatio="w,2:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/txtTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/videoClip"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/txtChannel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtTitle"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/txtGame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtChannel"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/txtViews"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/txtGame"
tools:ignore="HardcodedText" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="15dp"
app:layout_constraintTop_toBottomOf="#+id/txtViews">
<ImageButton
android:id="#+id/favouriteButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:scaleType="fitCenter"
android:src="#drawable/ic_baseline_favorite_border_24" />
<ImageButton
android:id="#+id/button_to_share"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#null"
android:scaleType="fitCenter"
android:src="#drawable/ic_baseline_share_24" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Seems like a simple mistake on my part, but I'm pulling my hair out trying to work out what I've done wrong to cause the error on loading.
Any help would be appreciated.
I see you're trying to find the button "ShareBtn" inside the fragment which is totally wrong.
the "ShareBtn" doesn't belong to the fragment, it belongs to the viewHolder which you have created inside "ClipAdapter"
What you need to do is creating an interface inside "ClipAdapter" and create an object from it inside the Adapter
then call the method which is should the clickListener delegates the click to it
lastly, you should implement it inside the fragment and put whatever logic you want
This link will help you implement it
You are casting ID of view which is an Integer to ImageButton which is a View in this line
val shareBtn = R.id.button_to_share as ImageButton
You should use this instead
val shareBtn = findViewById<ImageButton>(R.id.button_to_share)
UPDATE
Also you should find views after fragment view got created. It means you should call `findViewById` inside `onViewCreated` and not inside `onCreateView`. If you try to find views before view of fragment gets created then you get `NullPointerException` since there is no view yet.
I can see a flicker within my recycler view right at the top. The flicker seems to resemble the top lines of my drawable shapes in the recycler view item.xml. I have made various adjustments to lines and other things to see if I could prevent the flicker but nothing appears to be working.
Here is my code;
Activity
<?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="wrap_content"
tools:context=".ScoreHistory">
<TextView
android:id="#+id/textView_history_title"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:gravity="center|center_horizontal|center_vertical"
android:padding="5dp"
android:text="#string/GameHistory_Title"
android:textAlignment="gravity"
android:textColor="#color/lime"
android:textStyle="bold"
app:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RV_scorehistory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView_history_title" />
</androidx.constraintlayout.widget.ConstraintLayout>
My item.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="55dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/rv_lines"
android:gravity="center_vertical">
<TextView
android:id="#+id/TV_RV_ID"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_DIFFICULTY"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/TV_RV_DIFFICULTY"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_ID"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_SUMTYPE"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="4"
app:layout_constraintStart_toEndOf="#+id/TV_RV_ID" />
<TextView
android:id="#+id/TV_RV_SUMTYPE"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_DIFFICULTY"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_OutOfText"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="3.5"
app:layout_constraintStart_toEndOf="#+id/TV_RV_DIFFICULTY" />
<TextView
android:id="#+id/TV_RV_OutOfText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:gravity="center"
android:text="#string/score_text"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_SUMTYPE"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_SCORE"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="2"
app:layout_constraintStart_toEndOf="#+id/TV_RV_SUMTYPE" />
<TextView
android:id="#+id/TV_RV_SCORE"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="6dp"
android:gravity="center"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_OutOfText"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_QCOUNT_TEXT"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="1.5"
app:layout_constraintStart_toEndOf="#+id/TV_RV_OutOfText" />
<TextView
android:id="#+id/TV_RV_QCOUNT_TEXT"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:gravity="center"
android:text="#string/questions_text"
android:textAlignment="gravity"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_SCORE"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_QCOUNT"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight=".75"
app:layout_constraintStart_toEndOf="#+id/TV_RV_SCORE" />
<TextView
android:id="#+id/TV_RV_QCOUNT"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:background="#drawable/target_icon"
android:gravity="center"
android:textAlignment="center"
android:textStyle="bold"
app:layout_constraintBaseline_toBaselineOf="#+id/TV_RV_QCOUNT_TEXT"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="1.5"
app:layout_constraintStart_toEndOf="#+id/TV_RV_QCOUNT_TEXT" />
</androidx.constraintlayout.widget.ConstraintLayout>
I set background resources here in my adapter
class ScoreHistoryAdaptor : ListAdapter<SavedScores, ScoreHistoryAdaptor.SavedScoreViewHolder>(WordsComparator()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SavedScoreViewHolder {
return SavedScoreViewHolder.create(parent)
}
override fun onBindViewHolder(holder: SavedScoreViewHolder, position: Int) {
val current = getItem(position)
holder.bind(current.id,current.difficulty,current.sumtype,current.questioncount,current.answeredcorrectly)
}
class SavedScoreViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val IDItemView: TextView = itemView.findViewById(R.id.TV_RV_ID)
private val DiffultyItemView: TextView = itemView.findViewById(R.id.TV_RV_DIFFICULTY)
private val SumTypeItemView: TextView = itemView.findViewById(R.id.TV_RV_SUMTYPE)
private val qCountItemView: TextView = itemView.findViewById(R.id.TV_RV_QCOUNT)
private val ScoreItemView: TextView = itemView.findViewById(R.id.TV_RV_SCORE)
private var DIFFCODE = ""
private var SUMTYPECODE = ""
val myContext = SumTypeItemView.context
fun bind(ID: Int?,DIFFICULTY:String?,SUMTYPE:String?,QC:Int?,SCORE:Int?) {
IDItemView.text = ID.toString()
when (DIFFICULTY) {
myContext.resources.getString(R.string.diffLVL1) -> {DIFFCODE = DIFFICULTY; DiffultyItemView.setBackgroundResource(R.drawable.diff_easy_icon)}
myContext.resources.getString(R.string.diffLVL2) -> {DIFFCODE = DIFFICULTY ; DiffultyItemView.setBackgroundResource(R.drawable.diff_normal_icon)}
myContext.resources.getString(R.string.diffLVL3) -> {DIFFCODE = DIFFICULTY; DiffultyItemView.setBackgroundResource(R.drawable.diff_hard_icon) }
myContext.resources.getString(R.string.diffLVL4) -> {DIFFCODE = DIFFICULTY ; DiffultyItemView.setBackgroundResource(R.drawable.diff_impossible_icon)}
myContext.resources.getString(R.string.random) -> {DIFFCODE = DIFFICULTY ; DiffultyItemView.setBackgroundResource(R.drawable.diff_random_icon)}
}
DiffultyItemView.text = DIFFCODE
when (SUMTYPE) {
myContext.resources.getString(R.string.catMulti) -> {SUMTYPECODE = myContext.resources.getString(R.string.multi_symbol) }
myContext.resources.getString(R.string.catAdd) -> {SUMTYPECODE = myContext.resources.getString(R.string.add_symbol)}
myContext.resources.getString(R.string.catSub) -> {SUMTYPECODE = myContext.resources.getString(R.string.sub_symbol)}
myContext.resources.getString(R.string.catDiv) -> {SUMTYPECODE = myContext.resources.getString(R.string.div_symbol)}
myContext.resources.getString(R.string.catSqrR) -> {SUMTYPECODE = myContext.resources.getString(R.string.sqr_symbol) + "n"}
myContext.resources.getString(R.string.catSqrD) -> {SUMTYPECODE = "n" + myContext.resources.getString(R.string.sqrd_symbol)}
myContext.resources.getString(R.string.catCubeR) -> {SUMTYPECODE = myContext.resources.getString(R.string.cuber_symbol) + "n"}
myContext.resources.getString(R.string.catCubeD) -> {SUMTYPECODE = "n" + myContext.resources.getString(R.string.cubed_symbol)}
myContext.resources.getString(R.string.random) -> {SUMTYPECODE = myContext.resources.getString(R.string.random)}
}
when (IsScoreLessThanTarget(SCORE!!.toInt(),QC!!.toInt())){
true ->{ScoreItemView.setBackgroundResource(R.drawable.somewrong_icon)}
false ->{ScoreItemView.setBackgroundResource(R.drawable.allcorrect_icon)}
}
SumTypeItemView.setBackgroundResource(R.drawable.sumtype_icon)
SumTypeItemView.text = SUMTYPECODE
qCountItemView.text = QC.toString()
ScoreItemView.text = SCORE.toString()
}
companion object {
fun create(parent: ViewGroup): SavedScoreViewHolder {
val view: View = LayoutInflater.from(parent.context)
.inflate(R.layout.row_item, parent, false)
return SavedScoreViewHolder(view)
}
}
}
class WordsComparator : DiffUtil.ItemCallback<SavedScores>() {
override fun areItemsTheSame(oldItem: SavedScores, newItem: SavedScores): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(oldItem: SavedScores, newItem: SavedScores): Boolean {
return oldItem == newItem
}
}
}
private fun IsScoreLessThanTarget(score:Int,target:Int): Boolean{
return score < target
}
In another view using scrollview I get the same
Your issue is arising because in your my_item.xml
<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="55dp" <---- Fixed Height
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#ae7f9c"
android:gravity="center_vertical">
and in your textviews like this one,
<TextView
android:id="#+id/TV_RV_ID"
android:layout_width="0dp"
android:layout_height="match_parent" <------ this hiding the background try using wrap_content
android:layout_margin="0dp"
android:gravity="center"
android:textAlignment="center"
app:layout_constraintEnd_toStartOf="#+id/TV_RV_DIFFICULTY"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
To solve this,
Try not giving a fixed height from my_item.xml container, use wrap_content
Add padding to the container constaintlayout
add android:layout_height="wrap_content" to your text views and center_vertical them,
then your issue will resolve, hope this helps
try not giving a fixed height to the container
Hello everyone i have little problem about shared element.
I have using shared element at activity transition.
When click to recyclerView item that view comes to front of all view.
as you can see when i click to lowermost picture
begin transition fragment --> activity
private fun itemClicked(pos: Int, view: View) {
val intent = Intent(context, DetailActivity::class.java)
val title = view.findViewById<TextView>(R.id.title)
val photo = view.findViewById<ImageView>(R.id.photo)
intent.putExtra(IMAGE_TRANSITION_NAME, photo.transitionName)
intent.putExtra(TITLE_TRANSITION_NAME, title.transitionName)
val pair1 =
Pair.create(
photo as View,
ViewCompat.getTransitionName(photo).toString()
)
val pair2 =
Pair.create(
title as View,
ViewCompat.getTransitionName(title).toString()
)
val options = activity?.let {
ActivityOptionsCompat.makeSceneTransitionAnimation(it, pair2, pair1)
}
startActivity(intent, options?.toBundle())
}
my custom view holder
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="wrap_content"
android:padding="0dp"
app:contentPaddingBottom="0dp"
app:contentPaddingLeft="0dp"
app:contentPaddingRight="0dp"
app:contentPaddingTop="0dp"
android:orientation="vertical">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatImageView
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="#+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:maxLines="2"
app:autoSizeMaxTextSize="#dimen/primary_text_medium"
app:autoSizeMinTextSize="#dimen/primary_text_small"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/photo"
tools:text="Deniz subaşı" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
I am inflating a list of layouts programmatically from a xml into another layout.
The end result looks like the following image.
The number of checkboxes is different on each run, how can i get their state and also add listeners to the image button? Ideally i want only one listener that has an id of the selected layout.
This is the xml code.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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/chk_item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/checkBox_chkitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView_chkitem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:text="TextView"
app:layout_constraintEnd_toStartOf="#+id/imageButton_chkitem"
app:layout_constraintStart_toEndOf="#+id/checkBox_chkitem"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/imageButton_chkitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/stat_notify_more" />
</android.support.constraint.ConstraintLayout>
And this is how i inflate the layouts
val checkitemlist = listOf<ChecklistItem>(ChecklistItem("Categoria", "conteudo"),
ChecklistItem("Categoria", "conteudo"),
ChecklistItem("Categoria", "conteudo"),
ChecklistItem("Categoria", "conteudo"))
for (item in checkitemlist) {
val inflater = LayoutInflater.from(context)
val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout
layout.textView_chkitem.text = item.textSimple
checklist_content_layout.addView(layout)
}
replace your code
for (item in checkitemlist) {
checklist_content_layout.removeAllViews()
val inflater = LayoutInflater.from(context)
val layout = inflater.inflate(io.ubivis.ier.R.layout.checklistitem_layout, null, false) as ConstraintLayout
layout.textView_chkitem.text = item.textSimple
checklist_content_layout.addView(layout)
}