Home > Mobile >  Android :switch between Layouts dynamically every 10 seconds
Android :switch between Layouts dynamically every 10 seconds

Time:07-13

android beginner here. any help is much appreciated .

I am working on an app that can do a simple experiment. the app displays images and asks users to rate it from 1 to 10. I have 1 activity and two layouts experiment begins. I have two layouts merged under FrameLayout. what I want to achieve is : '====>start experiment >show first image for 10 seconds >change layout to ratings layout>after user selects rating>loopback with different image>finish() when imagelist is empty. ' here is what I tried I have tried viewflipper but shownext() and showprevious() methods dont update values. now i am trying using layout visibility ,showing and hiding the layouts

class Presenter : AppCompatActivity() {
    //val currentTime = Calendar.getInstance().time
    private lateinit var binding: ActivityPresenterBinding
    lateinit var layout1:View
    lateinit var layout2:View
    val imgList=Constants.getImages()


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    //setContentView(R.layout.activity_presenter)

    binding = ActivityPresenterBinding.inflate(layoutInflater)
    setContentView(binding.root)

    layout1 = binding.imageView
    layout2=binding.ratingView
    
    startSlider()
    
}

private fun startSlider() {
    Handler(Looper.getMainLooper()).apply {
        //arr.shuffle()
        var index = 0
        var imageView = binding.imgPresenter
        val runnable = object : Runnable {
            override fun run() {
                imageView.setImageResource(imgList[index].image)
                layout1.visibility= View.VISIBLE
                Log.i("number ","${index}")
                postDelayed(this, 5000)
                index  
                layout1.visibility=View.GONE
                layout2.visibility=View.VISIBLE
                binding.sbSeekbar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener{
                    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

                        Toast.makeText(applicationContext, "$p1", Toast.LENGTH_LONG).show()
                        //save rating (1-10)

                    }

                    override fun onStartTrackingTouch(p0: SeekBar?) {

                    }

                    override fun onStopTrackingTouch(p0: SeekBar?) {

                    }

                })

            }


        }

        postDelayed(runnable, 1000)
    }
}

here my layout file

<FrameLayout android:id="@ id/viewFliper"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@ id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@ id/img_presenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/ic_bg"
        />

</LinearLayout>

<LinearLayout 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/ratingView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="16dp"

    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="how do you feel about this image from one to 10"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginBottom="20dp"
        />

    <SeekBar
        android:id="@ id/sb_seekbar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:paddingStart="6dp"
        android:paddingEnd="6dp"
        android:progress="3"
        android:stepSize="1" />


</LinearLayout>
this is not hiding the views after the second image what am I doing wrong?

CodePudding user response:

try this and apply viewbinding

    lateinit var  runnable: Runnable

private fun startSlider() {
    Handler(Looper.getMainLooper()).apply {

        var flag = 0
        var index = 0


        runnable = Runnable {
            if (flag == 0) {

                img_presenter.setImageResource(imgList[index])
                layout1.visibility = View.VISIBLE
                layout2.visibility = View.GONE

                postDelayed(runnable, 5000)

                flag = 1

                index  
            } else {

                layout1.visibility = View.GONE
                layout2.visibility = View.VISIBLE

                sb_seekbar.setOnSeekBarChangeListener(object :
                    SeekBar.OnSeekBarChangeListener {
                    override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {

                        //                                Toast.makeText(applicationContext, "$p1", Toast.LENGTH_LONG).show()
                        //save rating (1-10)

                    }

                    override fun onStartTrackingTouch(p0: SeekBar?) {


                    }

                    override fun onStopTrackingTouch(p0: SeekBar?) {
                        index  

                        flag = 0

                        postDelayed(runnable, 1000)
                    }
                })
            }
        }

        postDelayed(runnable, 1000)
    }
}
  • Related