I don't understand why "image" in .into is underlined in red.
I just need the picture to be shown by clicking on the button
enter image description here
package com.example.raspisanie
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val image = findViewById<View>(R.id.image)
val button = findViewById<View>(R.id.button)
val url = "https://vsegda-pomnim.com/uploads/posts/2022-04/1649143490_6-vsegda-pomnim-com-p-priroda-islandii-foto-8.jpg"
button.setOnClickListener {
Glide.with(this)
.load(url)
.into(image)
}
}
}
It is due to type mismatch. The type of image should be ImageView. Change val image = findViewById<View>(R.id.image) to val image = findViewById<ImageView>(R.id.image)
Related
package com.example.myandroidapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val calButtonVar = findViewById(R.id.calButton) as Button
var radiusTextvar = findViewById(R.id.radiusText) as EditText
var areaTextvar = findViewById(R.id.areaText) as TextView
calButtonVar.setOnClickListener{
var calArea: Double = 3.14 * radiusTextvar.text
areaTextvar.text = calArea.toString()
}
}
}
at calArea is where the error appears, i tried swapping var with val but it didnt work so I dont know what should I do and I also looked for similar problems but they didnt have it like me
Yes. As #CommonsWare wrote you can't just multiply editable text. use this instead:
var calArea: Double = 3.14 * radiusTextvar.text.toDouble()
if I any change in firebase like delete, update. The data in recyclerView is duplicated if any of those CRUD occur, so I added temporary swipeRefresh to refresh the activity but this solution doesn't make sense.
This image below explain when I update data in firebase and what happend in RecyclerView
MainDashBoard.kt
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ProgressBar
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
class MainDashBoard : AppCompatActivity(), OnItemPatientClickListener{
data class PatientDataItem(val patientName: String, val patientMessage: String)
private lateinit var auth: FirebaseAuth
lateinit var swipeRefreshLayout: SwipeRefreshLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_dash_board)
var database = FirebaseDatabase.getInstance().reference
var patientDataItems = ArrayList<PatientDataItem>()
val patientRecycler = findViewById<RecyclerView>(R.id.patient_recycler)
val patienDashboardprogressBar = findViewById<ProgressBar>(R.id.patientDashboardprogressBar)
val noDataMain = findViewById<TextView>(R.id.no_data_main_dashboard)
swipeRefreshLayout = findViewById(R.id.swipe)
patientRecycler.layoutManager = LinearLayoutManager(this)
patientRecycler.adapter = MainDashboardAdapter(patientDataItems, this)
auth = FirebaseAuth.getInstance()
val user = auth.currentUser
val patientsListener = object : ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val patients = p0.child("users").child(user!!.uid)
if (p0.value == null ){
noDataMain.visibility = View.VISIBLE
}else{
noDataMain.visibility = View.GONE
for (i in p0.children){
var patientName = i.key.toString()
var patientMessage = i.value.toString()
patientDataItems.add(PatientDataItem(patientName, patientMessage))
}
}
patientRecycler.scrollToPosition(patientDataItems.size-1)
patienDashboardprogressBar.visibility = View.GONE
}
override fun onCancelled(error: DatabaseError) {
println("error")
}
}
database.child("location").child("users").child(user!!.uid).addValueEventListener(patientsListener)
// database.child("location").addValueEventListener(postListener)
swipeRefreshLayout.setOnRefreshListener {
startActivity(intent);
Handler(Looper.getMainLooper()).postDelayed(Runnable {
swipeRefreshLayout.isRefreshing = false
}, 4000)
}
}
override fun onItemClick(patientDataItems: PatientDataItem) {
val patientMacAddressName = patientDataItems.patientName
val dashboardIntent = Intent(this, DashboardActivity::class.java)
dashboardIntent.putExtra("macAddressNamePatient", patientMacAddressName)
startActivity(dashboardIntent)
}
}
MainDashBoardAdapter.kt
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.example.ard_here.R
class MainDashboardAdapter(private val patientDataSet: ArrayList<MainDashBoard.PatientDataItem>,
private val onPatientClickListener: OnItemPatientClickListener): RecyclerView.Adapter<MainDashboardAdapter.PatientCustomHolder>(){
override fun getItemCount(): Int {
return patientDataSet.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PatientCustomHolder {
var layoutInflater = LayoutInflater.from(parent?.context)
var cellForRow = layoutInflater.inflate(R.layout.main_patient_layout, parent, false)
return PatientCustomHolder(cellForRow)
}
override fun onBindViewHolder(holder: PatientCustomHolder, position: Int) {
holder.bindItems(patientDataSet[position])
holder.patientLayout.setOnClickListener {
onPatientClickListener.onItemClick(patientDataSet[position])
}
}
class PatientCustomHolder(v: View): RecyclerView.ViewHolder(v){
val patientLayout: ConstraintLayout = v.findViewById(R.id.patient_layout)
val patientName: TextView = v.findViewById(R.id.patient_name)
val patientMessage : TextView = v.findViewById(R.id.patient_message)
fun bindItems(data_item: MainDashBoard.PatientDataItem){
patientName.text = data_item.patientName
patientMessage.text = data_item.patientMessage
}
}
}
OnItemPatientClickListener.kt
interface OnItemPatientClickListener {
fun onItemClick(patientDataItems: MainDashBoard.PatientDataItem)
}
clear your data container then bind it again in recyclerview.
or you have mvvm pattern, you can use live data to observe data source and if there is any changes, your activity will easily notified and make some ui changes
Since you're reading the data with addValueEventListener, which means that:
The data from the path is read from the database right away, and passed to your onDataChange.
The client the continues monitor the path, and if anything changes it calls your onDataChange again with all data at the path.
In your onDataChange you're only ever adding data to patientDataItems. That works well the first time the data is loaded, so #1 above. But if you add or change a single child node (#2 above), you get called with all data at the path again. So that's when you end up duplicating the items in the view.
The simplest solution is to clear patientDataItems whenever onDataChange get called:
override fun onDataChange(p0: DataSnapshot) {
patientDataItems.clear()
...
I'm trying to pass data from a RecyclerView that contains a list of photos to a separate Activity that should show Details about the selected photo.
since im new to Kotlin, i've tried a lot and failed to come up with a solution.. I'd be thankful if anyone has a word on this.
PhotosActivity.kt
package com.example.wallpaperapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class PhotosActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_photos)
val recyclerPhotos = findViewById<RecyclerView>(R.id.rv_photo)
recyclerPhotos.layoutManager = LinearLayoutManager(this)
recyclerPhotos.adapter = PhotosAdapter(getPhotosData(), this)
}
private fun getPhotosData(): List<PhotoModel>{
return listOf(
PhotoModel(R.drawable.photo1),
PhotoModel(R.drawable.photo2),
PhotoModel(R.drawable.photo3),
PhotoModel(R.drawable.photo4),
PhotoModel(R.drawable.photo5),
PhotoModel(R.drawable.photo6),
PhotoModel(R.drawable.photo7),
PhotoModel(R.drawable.photo8)
)
}
}
PhotosModel.kt
package com.example.wallpaperapp
import java.io.Serializable
data class PhotoModel(
val image: Int,
): Serializable
PhotosAdapter.kt
package com.example.wallpaperapp
import android.app.Activity
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
class PhotosAdapter(val listOfPhotos: List<PhotoModel>,
val context: Activity): RecyclerView.Adapter<PhotosAdapter.ViewHolder>() {
inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
val img = view.findViewById<ImageView>(R.id.img_test)
init{
itemView.setOnClickListener {
val intent = Intent(context, PhotoDetailsActivity::class.java)
intent.putExtra("PHOTO", listOfPhotos[adapterPosition])
context.startActivity(intent)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotosAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_photo, parent,false)
return ViewHolder(view)
}
override fun getItemCount(): Int {
return listOfPhotos.size
}
override fun onBindViewHolder(holder: PhotosAdapter.ViewHolder, position: Int) {
holder.img.setImageResource(listOfPhotos[position].image)
}
}
PhotoDetailsActivity.kt
package com.example.wallpaperapp
import android.app.WallpaperManager
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat.setBackground
class PhotoDetailsActivity : AppCompatActivity() {
lateinit var arrow: ImageView
lateinit var img: ImageView
lateinit var btn_set: Button
lateinit var bitmap: Bitmap
lateinit var manager: WallpaperManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_photo_details)
val selectedPhoto = intent.extras?.getSerializable("PHOTO") as PhotoModel
arrow = findViewById(R.id.img_arrow)
arrow.setOnClickListener {
val intent = Intent(this, PhotosActivity::class.java)
startActivity(intent)
}
img = findViewById(R.id.img_detail)
btn_set = findViewById(R.id.btn_wallpaper)
btn_set.setOnClickListener(View.OnClickListener {
fun setOnClickr(l: (View) -> Unit){
setBackgroundd()
}
})
}
fun setBackgroundd() {
val bitmap = BitmapFactory.decodeResource(resources, R.layout.activity_photos)
img.setImageBitmap(bitmap)
manager = WallpaperManager.getInstance(getApplicationContext())
manager.setBitmap(bitmap)
}
}
thanks in advance!
While displaying value to a textview on second activity getting error as
expecting member declaration
This is my code for Another Activity:
package com.example.trial.sudoku_solver
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Bitmap
import android.media.MediaScannerConnection
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.text.Editable
import android.util.Log
import android.view.View
import android.widget.*
import com.example.trial.sudoku_solver.R
import kotlinx.android.synthetic.main.activity_main.view.*
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*
import org.w3c.dom.Text
class AnotherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
}
var a: String = intent.getStringExtra("text")
val text1: TextView = findViewById(R.id.textview) as TextView
text1.text = intent.getStringExtra("text")
}
This is my partial code for the function in MainActivity
private fun clickText() {
val text1: EditText = findViewById(R.id.editText)
if (text1 != null) {
//Toast.makeText(this, text1.text, Toast.LENGTH_LONG).show()
var text1 = text1.editText.toString()
val intent = Intent(this, AnotherActivity::class.java)
intent.putExtra("text",text1)
startActivity(intent);
}
}
8You need to call intent, findViewById and then setText after the call to setContentView:
class AnotherActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
var a: String = intent.getStringExtra("text")
val text1: TextView = findViewById(R.id.textview) as TextView
text1.text = intent.getStringExtra("text")
}
}
The easiest way to do this :
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("ID", sId);
startActivity(intent);
Access that intent on next activity
String sId= getIntent().getStringExtra("ID");
Hope this will help you
You need to add these piece of code inside onCreate method block
You should add your executable statements inside methods.
So the code will look like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_another)
// add these statement inside onCreate block
var a: String = intent.getStringExtra("text")
val text1: TextView = findViewById(R.id.textview) as TextView
text1.text = intent.getStringExtra("text")
}
Threre is no settext method in an Activity. You should set the text string to TextView like that:
var a: String = intent.getStringExtra("text")
val text1 = findViewById(R.id.textview) as TextView
text1.text = a
In addition, when you write code in kotlin, it is not necessary to use findViewById in activities. Because of synthetic properties, the views are accessible via their ids from xml file. So, your three line of code could be replaced as below:
textview.text = intent.getStringExtra("text")
Also change the code block in MainActivity to:
private fun clickText() {
val editText1: EditText = findViewById(R.id.editText)
if (editText1 != null) {
//Toast.makeText(this, text1.text, Toast.LENGTH_LONG).show()
var text1 = editText1.text.toString()
val intent = Intent(this, AnotherActivity::class.java)
intent.putExtra("text", text1)
startActivity(intent);
}
}
I am having trouble ScrollBar it shows error when I add it in a Kotlin class. Import library is also showing an error. What should I do? Below is the code:
import android.widget.Toast
import com.github.barteksc.pdfviewer.PDFView
import com.github.barteksc.pdfviewer.ScrollBar
import kotlinx.android.synthetic.main.activity_pdf.view.*
import java.io.File
class PDF_Activity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_pdf)
val pdfView = findViewById(R.id.pdfView) as PDFView
val scrollBar = findViewById<ScrollBar>(R.id.scrollBar)
pdfView.setScrollBar(scrollBar)
scrollBar.setHorizontal(false)
val i = this.intent
val path = i.extras!!.getString("PATH")
val file = File(path!!)
if (file.canRead()) {
pdfView.fromFile(file).defaultPage(1).onLoad { nbPages -> Toast.makeText(this#PDF_Activity, nbPages.toString(), Toast.LENGTH_LONG).show() }.load()
}