How to disable dot button in android Kotlin - kotlin

I am new to Android Studio - Kotlin, I am building an app with numbers and math operators with decimal point(.dot button)In my math calculator app, if press the dot button, the button should be disabled or not entered for the second time. I want to disable or stop printing dot button(.) if pressed more than once.
Here is my code
Acitivity.kt file
package com.tripbegins.calculator
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import com.tripbegins.calculator.R.id.*
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
var emptyText = true
fun numberEvents(view: View) {
var checkButton:Boolean = false
if(emptyText){
viewResult.setText("")
}
emptyText = false
var button = view as Button
var isClicked = viewResult.text.toString()
when (button.id) {
buttonOne.id -> isClicked += "1"
buttonTwo.id -> isClicked += "2"
buttonThree.id -> isClicked += "3"
dotButton.id-> isClicked+="."
}
viewResult.setText(isClicked)
}
var operation ="+"
var oldValues:String? = null
fun mathOperation(view: View){
var mathButton = view as Button
var isClicked = viewResult.text.toString()
when(mathButton.id){
plusButton.id-> {
operation = "+"
}
minusButton.id->{
operation="-"
}
mulButton.id->{
operation="*"
}
}
oldValues = viewResult.text.toString()
emptyText = true
}
fun Calculate(view: View){
var newValues = viewResult.text.toString()
var calulateButton:Double? = null
when(operation){
"+"-> { calulateButton = oldValues!!.toDouble() + newValues.toDouble() }
"-"-> { calulateButton = oldValues!!.toDouble() - newValues.toDouble() }
"*"-> { calulateButton = oldValues!!.toDouble() * newValues.toDouble() }
}
viewResult.setText(calulateButton.toString())
emptyText=true
}
fun clearFunction(view: View){
viewResult.setText("")
}
}
activity.xml file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tripbegins.calculator.MainActivity">
<EditText
android:id="#+id/viewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:ems="10"
android:gravity="bottom|right"
android:inputType="textPersonName"
android:text="0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/buttonTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="76dp"
android:onClick="numberEvents"
android:text="2"
app:layout_constraintStart_toEndOf="#+id/buttonOne"
app:layout_constraintTop_toBottomOf="#+id/viewResult" />
<Button
android:id="#+id/plusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="38dp"
android:onClick="mathOperation"
android:text="+"
app:layout_constraintBaseline_toBaselineOf="#+id/minusButton"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/minusButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:layout_marginTop="33dp"
android:onClick="mathOperation"
android:text="-"
app:layout_constraintStart_toEndOf="#+id/plusButton"
app:layout_constraintTop_toBottomOf="#+id/buttonTwo" />
<Button
android:id="#+id/mulButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:onClick="mathOperation"
android:text="*"
app:layout_constraintBaseline_toBaselineOf="#+id/minusButton"
app:layout_constraintStart_toEndOf="#+id/minusButton" />
<Button
android:id="#+id/calButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:onClick="Calculate"
android:text="Calculate"
app:layout_constraintStart_toStartOf="#+id/minusButton"
app:layout_constraintTop_toBottomOf="#+id/minusButton" />
<Button
android:id="#+id/buttonThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="26dp"
android:onClick="numberEvents"
android:text="3"
app:layout_constraintBaseline_toBaselineOf="#+id/buttonTwo"
app:layout_constraintStart_toEndOf="#+id/buttonTwo" />
<Button
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:onClick="numberEvents"
android:text="1"
app:layout_constraintBaseline_toBaselineOf="#+id/buttonTwo"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/dotButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="22dp"
android:onClick="numberEvents"
android:text="."
app:layout_constraintBaseline_toBaselineOf="#+id/calButton"
app:layout_constraintEnd_toStartOf="#+id/calButton" />
<Button
android:id="#+id/clearButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="11dp"
android:onClick="clearFunction"
android:text="Clear"
app:layout_constraintBaseline_toBaselineOf="#+id/calButton"
app:layout_constraintStart_toEndOf="#+id/calButton" />
</android.support.constraint.ConstraintLayout>
My expectation of the result
Valid entry -> 3. (.dot button should not be entered for the second time)
Invalid entry -> 3... (currently i can able to enter multiple .dot buttons)
Dot button should not pressed twice. It should be active only once.
Thanks
Mohammed

so you want to disable the button that has the name dotButton after the user clicks it for the first time :
to disable a button, use:
dotButton.isClickable=false
if you want the button to become clickable again(like when the user deletes the dot that he already pressed) you can use:
dotButton.isClickable=true
Edit:
modify your activity code like that :
when (button.id) {
buttonOne.id -> isClicked += "1"
buttonTwo.id -> isClicked += "2"
buttonThree.id -> isClicked += "3"
dotButton.id-> {
isClicked+="."
dotButton.isClickable=false//**add this line here**
}
}
fun clearFunction(view: View){
viewResult.setText("")
dotButton.isClickable=true//**add this line here**
}

Related

The RecyclerView doesn't retrieve any data from Firebase and doesn't show any data

I just started working with Android Studio, and I've been trying to figure out what's wrong with this code for a few days now. When I run the program it shows nothing, and also there is no errors from Android Studio. I read the posts on the subject, but unfortunately I found nothing appropriate.
This is my activity class.
package ie.wit.donationx.activities
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.firebase.database.*
import ie.wit.donationx.adapters.EventsAdapter
import ie.wit.donationx.databinding.ActivityEventsFeedBinding
import ie.wit.donationx.models.EventData
class EventsFeed : AppCompatActivity() {
lateinit var mDataBase: DatabaseReference
private lateinit var eventList:ArrayList<EventData>
private lateinit var mAdapter: EventsAdapter
private lateinit var binding: ActivityEventsFeedBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_events_feed)
binding = ActivityEventsFeedBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
eventList = ArrayList()
mAdapter = EventsAdapter(this,eventList)
var linearLayoutManager = LinearLayoutManager(this)
binding.recyclerEvents.layoutManager = linearLayoutManager
binding.recyclerEvents.setHasFixedSize(true)
binding.recyclerEvents.adapter = mAdapter
getEventsData()
}
private fun getEventsData() {
mDataBase = FirebaseDatabase.getInstance().getReference("Events")
mDataBase.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()) {
for (eventSnapshot in snapshot.children) {
val event = eventSnapshot.getValue(EventData::class.java)
eventList.add(event!!)
}
binding.recyclerEvents.adapter = mAdapter
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
This is my Adapter
package ie.wit.donationx.adapters
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import ie.wit.donationx.R
import ie.wit.donationx.databinding.ItemListBinding
import ie.wit.donationx.models.EventData
class EventsAdapter(
var c: Context, var eventList: ArrayList<EventData>)
: RecyclerView.Adapter<EventsAdapter.EventViewHolder>() {
inner class EventViewHolder(var v: ItemListBinding): RecyclerView.ViewHolder(v.root){}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): EventViewHolder {
val infilter = LayoutInflater.from(parent.context)
val v = DataBindingUtil.inflate<ItemListBinding>(
infilter,R.layout.item_list,parent,
false
)
return EventViewHolder(v)
}
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
val newList = eventList[position]
holder.v.isEvent = eventList[position]
holder.adapterPosition
}
override fun getItemCount(): Int {
return eventList.size
}
}
XML recyclerView
<?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=".activities.EventsFeed">
<com.google.android.material.imageview.ShapeableImageView
android:id="#+id/main_shapeable"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:background="#color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/main_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:ems="10"
android:hint="search"
android:inputType="textPersonName"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="#+id/main_shapeable"
app:layout_constraintEnd_toEndOf="#+id/main_shapeable"
app:layout_constraintStart_toStartOf="#+id/main_shapeable"
app:layout_constraintTop_toTopOf="#+id/main_shapeable" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerEvents"
tools:listitem="#layout/item_list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/main_shapeable"/>
</androidx.constraintlayout.widget.ConstraintLayout>
XML file for items in recyclerView
<?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>
<variable
name="isEvent"
type="ie.wit.donationx.models.EventData" />
</data>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_margin="10dp"
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/eventImg"
android:imageUrl="#{isEvent.image}"
android:scaleType="centerCrop"
android:layout_width="140dp"
android:layout_height="140dp"/>
<LinearLayout
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/eventTitle"
android:textColor="#color/black"
android:textSize="20sp"
android:gravity="center"
android:textStyle="bold|normal"
android:layout_gravity="center"
android:text="#{isEvent.evenTitle}"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/eventInfo"
android:textColor="#android:color/darker_gray"
android:textSize="15sp"
android:gravity="center"
android:textStyle="bold|normal"
android:layout_gravity="center"
android:text="#{isEvent.info}"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<View
android:background="#android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</layout>
Data file
package ie.wit.donationx.models
class EventData {
var evenTitle: String? = null
var info: String? = null
var image: String? = null
constructor(){}
constructor(
evenTitle:String?,
info:String?,
image:String?
){
this.evenTitle = evenTitle
this.info = info
this.image = image
}
}
Firebase DB structure
When adding new list to the RecyclerView Adapter you need to call NotifyDataSetChanged()
Also, i advise to remove the list from the constructor of the adapter, and make a public function inside the adapter class that will update the private list created in the class.
private var items: MutableList<EventData> = mutableListOf()
fun setItems(newItemsList : List<EventData>){
eventList.clear()
eventList.addAll(newItemsList)
notifyDataSetChanged()
}
If you only update part of the list, you can create a separate function for that and use the more efficient notifyItemRangeInserted(), notifyItemInserted() or notifyItemChanged() \ notifyItemRangeChanged
You don't need to setAdaper again after getting EventsData
Just replace this line in getEventsData() :
binding.recyclerEvents.adapter = mAdapter
With :
if (eventList.size > 0) {
mAdapter.notifyDataSetChanged()
}
Edit:
You should not to add
android:imageUrl="#{isEvent.image}" in the ImageView,
you can use library to download the images from URL Like:
Picasso.
or
Fresco.
For Example using Picasso in your adapter:
override fun onBindViewHolder(holder: EventViewHolder, position: Int) {
val newList = eventList[position]
holder.v.isEvent = eventList[position]
holder.adapterPosition
// here to download image for URL
Picasso.get().load(eventList[position].image)
.into(holder.v.eventImg)
}
Don't forget to add dependencies
implementation 'com.squareup.picasso:picasso:2.8'

Buttons will not show on displayed fragment

I am currently creating a chat application for a project in Android Studio using Kotlin, Firebase Cloud Storage and Cloud Firestore, and Glide, and I am following a few tutorials to do so. Everything was fine until I created a fragment to handle the user's account settings, such as name, bio, etc. The fragment appears to work and the edit text fields and profile picture can all be edited and interacted with, but neither of the buttons for saving and signing out seem to be displayed, and I can't figure out what the issue is exactly. As a result, I can't actually test the code until I can these buttons to be displayed. Any ideas?
This is what the fragment is supposed to look like, according to the layout: https://i.stack.imgur.com/0qR0C.png
This is what the fragment looks like when the application is actually running: https://i.stack.imgur.com/vAgLG.png
This is what the fragment layout looks like in code:
<?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"
tools:context=".fragment.MyAccountFragment">
<ImageView
android:id="#+id/imageView_profile_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="155dp"
android:layout_marginTop="50dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="#drawable/ic_baseline_account_circle_24" />
<EditText
android:id="#+id/editText_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="225dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:hint="Your Name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView_profile_picture"
app:layout_constraintVertical_bias="0.025" />
<EditText
android:id="#+id/editText_bio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="275dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:hint="Your Bio"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/imageView_profile_picture"
app:layout_constraintVertical_bias="0.025" />
<Button
android:id="#+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="550dp"
android:backgroundTint="?attr/colorAccent"
android:text="Save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.971"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982"
tools:visibility="visible" />
<Button
android:id="#+id/btn_sign_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="310dp"
android:layout_marginTop="550dp"
android:backgroundTint="?attr/colorPrimaryDark"
android:text="Sign Out"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.028"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.982"
tools:visibility="visible" />
</FrameLayout>
This is the code for the fragment itself:
class MyAccountFragment : Fragment() {
// Request code:
private val RC_SELECT_IMAGE = 2
// Variable for the selected image:
private lateinit var selectedImageBytes: ByteArray
// Picture change flag:
private var pictureJustChanged = false
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_my_account, container, false)
view.apply {
findViewById<ImageView>(R.id.imageView_profile_picture).setOnClickListener{
val intent = Intent().apply {
type = "image/*"
action = Intent.ACTION_GET_CONTENT
putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/jpeg", "image/png"))
}
startActivityForResult(Intent.createChooser(intent, "Select Image"), RC_SELECT_IMAGE)
}
findViewById<Button>(R.id.btn_save).setOnClickListener{
if(::selectedImageBytes.isInitialized){
CloudStorageUtil.uploadProfilePicture(selectedImageBytes) { imagePath ->
CloudFirestoreUtil.updateCurrentUser(findViewById<EditText>(R.id.editText_name).text.toString(),
findViewById<EditText>(R.id.editText_bio).text.toString(),
imagePath)
}
}
else {
CloudFirestoreUtil.updateCurrentUser(findViewById<EditText>(R.id.editText_name).text.toString(),
findViewById<EditText>(R.id.editText_bio).text.toString(),
null)
}
}
findViewById<Button>(R.id.btn_sign_out).setOnClickListener {
FirebaseAuth.getInstance().signOut()
val intent = Intent(this#MyAccountFragment.context, LoginOrRegisterActivity::class.java)
intent.flags = (Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}
return view
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?){
if(requestCode == RC_SELECT_IMAGE
&& resultCode == Activity.RESULT_OK
&& data != null
&& data.data != null) {
val selectedImagePath = data.data
val selectedImageBmp = MediaStore.Images.Media.getBitmap(activity?.contentResolver, selectedImagePath)
val outputStream = ByteArrayOutputStream()
selectedImageBmp.compress(Bitmap.CompressFormat.JPEG, 90, outputStream)
selectedImageBytes = outputStream.toByteArray()
GlideApp.with(this).load(selectedImageBytes).into(view?.findViewById(R.id.imageView_profile_picture))
pictureJustChanged = true
}
}
override fun onStart() {
super.onStart()
CloudFirestoreUtil.getCurrentUser { user ->
if(this#MyAccountFragment.isVisible){
if (user != null) {
view?.findViewById<EditText>(R.id.editText_name)?.setText(user.name)
view?.findViewById<EditText>(R.id.editText_bio)?.setText(user.bio)
if(!pictureJustChanged && user.profilePicturePath != null){
GlideApp.with(this)
.load(CloudStorageUtil.pathToReference(user.profilePicturePath))
.placeholder(R.drawable.ic_baseline_account_circle_24)
.into(view?.findViewById(R.id.imageView_profile_picture))
}
}
}
}
}
}

How to setOnSeekBarChangeListener in a Kotlin Fragment

I am currently working on a mobile application in my uni classes that utilizes a seekBar to let the user decide to use a timer for a quiz. This application uses main to host all of the fragments. currently I just want the textbox to display where the user scrolled the seek bar to but am struggling to find a solution. any advice would be greatly appreciated. this is the code I have within the
TitleFragment.kt:
package com.example.android.guesstheword.screens.title
import android.os.Bundle
import android.view.DragEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.databinding.adapters.SeekBarBindingAdapter
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.example.android.guesstheword.R
import com.example.android.guesstheword.databinding.TitleFragmentBinding
import kotlinx.android.synthetic.main.title_fragment.*
/**
* Fragment for the starting or title screen of the app
*/
class TitleFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View {
// Inflate the layout for this fragment
val binding: TitleFragmentBinding = DataBindingUtil.inflate(
inflater, R.layout.title_fragment, container, false)
binding.timerSwitch.setOnClickListener {
if(binding.timerSwitch.isChecked){
binding.timerBar.visibility = View.VISIBLE
}
else{
binding.timerBar.visibility = View.INVISIBLE
}
}
val seekBar = binding.timerBar
seekBar.setOnSeekBarChangeListener(binding)
binding.playGameButton.setOnClickListener {
findNavController().navigate(TitleFragmentDirections.actionTitleToGame())
}
return binding.root
}
}
private fun SeekBar.setOnSeekBarChangeListener(binding: TitleFragmentBinding) {
val seconds = binding.timerSeconds
seconds.visibility = View.VISIBLE
seconds.text = binding.timerBar.progress.toString()
}
title_fragment.xml:
<?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">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/title_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".screens.title.TitleFragment">
<TextView
android:id="#+id/get_ready_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:fontFamily="sans-serif"
android:text="#string/get_ready"
android:textColor="#color/black_text_color"
android:textSize="14sp"
android:textStyle="normal"
app:layout_constraintBottom_toTopOf="#+id/title_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_chainStyle="packed" />
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:fontFamily="sans-serif"
android:text="#string/title_text"
android:textColor="#color/black_text_color"
android:textSize="34sp"
android:textStyle="normal"
app:layout_constraintBottom_toTopOf="#+id/play_game_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/get_ready_text"
app:layout_constraintVertical_chainStyle="packed" />
<Button
android:id="#+id/play_game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="#string/play_button"
android:theme="#style/GoButton"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Switch
android:id="#+id/timer_switch"
android:layout_width="95dp"
android:layout_height="52dp"
android:text="Timer"
android:min="5"
android:max="360"
app:layout_constraintBottom_toTopOf="#+id/play_game_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_text"
app:layout_constraintVertical_bias="0.064"/>
<SeekBar
android:id="#+id/timerBar"
android:layout_width="132dp"
android:layout_height="85dp"
android:clickable="false"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/play_game_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timer_switch"
app:layout_constraintVertical_bias="0.144" />
<TextView
android:id="#+id/timer_seconds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/play_game_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timerBar"
app:layout_constraintVertical_bias="0.17" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
You created an extension function called setOnSeekBarChangeListener which does not actually set it.
In order to set the listener, you need to do something like this:
binding.timerBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
// here, you react to the value being set in seekBar
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
// you can probably leave this empty
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
// you can probably leave this empty
}
})
But keep in mind that this is NOT the extension function you created.

After second click on Fragment, recycler view disappears

When I Click on Home navigation fragment(there is recyclerview) recycler view disappears with error message: E/RecyclerView: No adapter attached; skipping layout
when the app launchs it appears but when i move to another fragment and return again to home fragment, recycler view just disappears. even If I dont move on another fragment and click on home fragment, it still disappears.
What could be the problem and what can I do to fix it?
My codes:
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
android:paddingBottom="65dp"
app:reverseLayout="false" />
</LinearLayout>
Posts.kt
data class Posts(
var id: Int,
var imageUrl: String,
var title: String,)
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private lateinit var recyclerViewPersonAdapter: RecyclerViewPersonAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val navView = findViewById<BottomNavigationView>(R.id.bottomNavMenu)
val controller = findNavController(R.id.nav_host_fragment)
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.homeFragment,
R.id.addFragment,
R.id.userFragment
)
)
setupActionBarWithNavController(controller, appBarConfiguration)
navView.setupWithNavController(controller)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = RecyclerViewPersonAdapter(getData())
}
private fun getData() : List<Posts>{
val list = ArrayList<Posts>()
list.add(
Posts(
1,
"https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
"Best news in the world\nfor you"
)
)
list.add(
Posts(
2,
"https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
"very very gooood\nnews for you"
)
)
list.add(
Posts(
3,
"https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
"very very gooood\nnews for you"
)
)
list.add(
Posts(
4,
"https://www.myanbiz-consulting.com/wp-content/uploads/2019/04/4214892-news-images.jpg",
"very very gooood\nnews for you"
)
)
return list
}
}
RecyclerViewPersonAdapter.kt
class RecyclerViewPersonAdapter(private val list: List<Posts>) : RecyclerView.Adapter<RecyclerViewPersonAdapter.PersonViewHolder>() {
class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val imageView: ImageView
val textView: TextView
init {
imageView = itemView.findViewById(R.id.imageView)
textView = itemView.findViewById(R.id.textView)
}
fun setData(posts: Posts){
Glide.with(itemView.context)
.load(posts.imageUrl)
.into(imageView)
textView.text = posts.title
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PersonViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.news_post, parent, false)
return PersonViewHolder(itemView)
}
override fun onBindViewHolder(holder: PersonViewHolder, position: Int) {
val posts = list[position]
holder.setData(posts)
}
override fun getItemCount(): Int {
return list.size
}
}
news_post.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="400dp"
android:layout_height="100dp"
android:background="#color/teal_200"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
android:src="#drawable/img" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="8sp"
android:text="Best news in the world\nfor you"
android:textColor="#color/black"
android:textSize="16sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_gravity="center"
android:src="#drawable/ic_baseline_thumb_up_alt_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:lineSpacingExtra="7sp"
android:text="2,687"
android:textColor="#333333"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="22dp"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:layout_width="14dp"
android:layout_height="14dp"
android:layout_gravity="center"
android:src="#drawable/ic_baseline_remove_red_eye_24" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:lineSpacingExtra="7sp"
android:text="32,577"
android:textColor="#333333"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Your RecyclerView is in fragment_home.xml.
But you declare it in MainActivity.
Just do that in your HomeFragment instead of MainActivity.
class HomeFragment : Fragment() {
private lateinit var recyclerView: RecyclerView
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
recyclerView = view.findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(requireContext())
recyclerView.adapter = RecyclerViewPersonAdapter(getData())
...

How to handle multiple items in RecyclerView

I have four radio button inside the radio group in recyclerview. when i click first position of radio button it reflect on eighth position of radio button. How can i resolve this? If no radio button clicked the group will disable. how can i do this also? when i click the submit button calculate how many radio button clicked?
Xml :
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txt_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:maxLines="5"
android:text="#string/question_1"
android:textColor="#000"
android:textSize="16sp"
android:textStyle="bold" />
<RadioGroup
android:id="#+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp">
<RadioButton
android:id="#+id/radio_option_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="#+id/radio_option_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="#+id/radio_option_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
<RadioButton
android:id="#+id/radio_option_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3" />
</RadioGroup>
</LinearLayout>
</android.support.v7.widget.CardView>
Adapter :
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.RecyclerVH> {
private Context mCtx;
private List<TestResult> testList;
public TestAdapter(Context mCtx, List<TestResult> testList) {
this.mCtx = mCtx;
this.testList = testList;
}
#NonNull
#Override
public RecyclerVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mCtx);
View view = layoutInflater.inflate(R.layout.list_test, parent, false);
return new RecyclerVH(view);
}
#Override
public void onBindViewHolder(#NonNull final RecyclerVH holder, final int position) {
TestResult test = testList.get(position);
String question = position + 1 + " " + test.getQuestion();
holder.question.setText(question);
holder.option_1.setText(test.getChoice0());
holder.option_2.setText(test.getChoice1());
holder.option_3.setText(test.getChoice2());
holder.option_4.setText(test.getChoice3());
// Log.d("Test",position+"");
}
#Override
public int getItemCount() {
return testList.size();
}
public void setList(ArrayList<TestResult> list) {
this.testList = list;
}
class RecyclerVH extends RecyclerView.ViewHolder {
TextView question;
RadioButton option_1;
RadioButton option_2;
RadioButton option_3;
RadioButton option_4;
RadioGroup group;
RecyclerVH(#NonNull View itemView) {
super(itemView);
question = itemView.findViewById(R.id.txt_question);
option_1 = itemView.findViewById(R.id.radio_option_1);
option_2 = itemView.findViewById(R.id.radio_option_2);
option_3 = itemView.findViewById(R.id.radio_option_3);
option_4 = itemView.findViewById(R.id.radio_option_4);
group = itemView.findViewById(R.id.radio_group);
}
}
}
add this in your adapter, it will handle item according to respective position.
#Override
public int getItemViewType(int position) {
return position;
}