I am creating a simple image gallery with a next and back button with Kotlin and XML. I have the next button working perfectly. I have set up an array of images and the onClickListener loops through them. I cannot wrap my head around how to reverse the array of images. I have tried reverseOrder<>() and reverse() but am having difficulty putting the pieces together. Very new to both XML and Kotlin (and coding in general). Any advice would help, thank you!
package com.example.gallery
import android.app.ActionBar.LayoutParams
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.FrameLayout
import android.widget.ImageSwitcher
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : Activity() {
private var iS: ImageSwitcher? = null
private var btn1: Button? = null
private var btn2: Button? = null
val myGallery = intArrayOf(R.drawable.beach_house,R.drawable.cars,R.drawable.moss, R.drawable.pier, R.drawable.tree)
var i = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn1 = findViewById<View>(R.id.button) as Button
btn2 = findViewById<View>(R.id.button2) as Button
iS = findViewById<View>(R.id.imageSwitcher) as ImageSwitcher
iS!!.setFactory {
val myView = ImageView(applicationContext)
myView.scaleType = ImageView.ScaleType.FIT_CENTER
myView.layoutParams = FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT)
myView
}
btn1!!.setOnClickListener {
val animFadeIn =
AnimationUtils.loadAnimation(applicationContext, android.R.anim.fade_in)
iS!!.startAnimation(animFadeIn)
if (i == myGallery.size) {
i = 0
imageSwitcher.setImageResource(myGallery[i])
i++
} else{
imageSwitcher.setImageResource(myGallery[i])
i++
}
}
btn2!!.setOnClickListener {
val animFadeIn =
AnimationUtils.loadAnimation(applicationContext, android.R.anim.fade_in)
iS!!.startAnimation(animFadeIn)
if (i == myGallery.size) {
i = 0
imageSwitcher.setImageResource(myGallery[i])
i++
} else{
imageSwitcher.setImageResource(myGallery[i])
i++
}
}
}
}
Please login or Register to submit your answer