skip to Main Content

I’m trying to implement a "Terms & Conditions" screen for an app that pops up when the user opens the app for the first time. When they accept the terms and conditions, they continue to the app. If they decline, the app closes automatically. I added a TextView to a ScrollView and 2 buttons. When the user scrolls all the way down to the bottom, the 2 buttons get enabled and they can choose what to do next.

package com.example.termsandconditionsview

import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import android.widget.Button
import android.widget.ScrollView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {

    private var svMain: ScrollView? = null
    private lateinit var buttonAccept: Button
    private lateinit var buttonDecline: Button

    private lateinit var sharedPref: SharedPreferences

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

        svMain = findViewById(R.id.scrollView2)
        svMain?.viewTreeObserver?.addOnScrollChangedListener(this)
        buttonAccept = findViewById(R.id.button_accept)
        buttonDecline = findViewById(R.id.button_decline)

        sharedPref = getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE)

        val termsAccepted = sharedPref.getBoolean("terms_accepted", false)
        if (termsAccepted) {
            setContentView(R.layout.activity_main)
        } else {
            setContentView(R.layout.terms_and_conditions_view)
        }
    }

    override fun onScrollChanged() {
        svMain?.let { scrollView ->
            val view = scrollView.getChildAt(scrollView.childCount - 1) as View
            val bottomDetector = view.bottom - (scrollView.height + scrollView.scrollY)

            if (bottomDetector <= 0) {
                buttonAccept.isEnabled = true
                buttonDecline.isEnabled = true
            } else {
                buttonAccept.isEnabled = false
                buttonDecline.isEnabled = false
            }
            buttonAccept.setOnClickListener {
                // Update the termsAccepted value to true in SharedPreferences
                val editor = sharedPref.edit()
                editor.putBoolean("terms_accepted", true)
                editor.apply()

                val textAcceptTac = getString(R.string.txt_accept_tac)
                val duration = Toast.LENGTH_LONG
                val toastAccept = Toast.makeText(this, textAcceptTac, duration)
                toastAccept.show()

                setContentView(R.layout.activity_main)
                // Proceed with the app as the user has accepted the terms&conditions
            }
            buttonDecline.setOnClickListener{
                val textDeclineTac = getString(R.string.txt_decline_tac)
                val duration = Toast.LENGTH_LONG
                val toastDecline = Toast.makeText(this, textDeclineTac, duration)
                toastDecline.show()
                // Close app when user clicks the "Decline" button.
                finish()
            }
        }
    }

    override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
        TODO("Not yet implemented")
    }
}

The problem now is that there is something wrong with my preferences part. Before i added that in, the button disabling/enabling worked, aswell as the On.Click event listeners. After adding the code for the preferences, nothing works anymore.

I don’t know what the problem is. Can someone please help me with this? Thanks! 🙂

2

Answers


  1. Chosen as BEST ANSWER

    As Hezy Ziv said, I'm setting the content view twice, which makes no sense.

    Here's the solution I came up with, which works now the way I intended it to work:

        override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        sharedPref = getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE)
    
        val termsAccepted = sharedPref.getBoolean("terms_accepted", false)
        if (termsAccepted) {
            setContentView(R.layout.activity_main)
        } else {
            setContentView(R.layout.terms_and_conditions_view)
            svMain = findViewById(R.id.scrollView2)
            svMain?.viewTreeObserver?.addOnScrollChangedListener(this)
            buttonAccept = findViewById(R.id.button_accept)
            buttonDecline = findViewById(R.id.button_decline)
        }
    }
    

  2. you are setting the setContentView() twice. This means that all the views you initialized before that line will be null.

    separate method to initialize the layout views and listeners
    
    class MainActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {
    
        private var svMain: ScrollView? = null
        private lateinit var buttonAccept: Button
        private lateinit var buttonDecline: Button
        private lateinit var sharedPref: SharedPreferences
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            sharedPref = getSharedPreferences("myAppPrefs", Context.MODE_PRIVATE)
            val termsAccepted = sharedPref.getBoolean("terms_accepted", false)
    
            if (termsAccepted) {
                setContentView(R.layout.activity_main)
            } else {
                setContentView(R.layout.terms_and_conditions_view)
                initializeViewsAndListeners()
            }
        }
    
        private fun initializeViewsAndListeners() {
            svMain = findViewById(R.id.scrollView2)
            svMain?.viewTreeObserver?.addOnScrollChangedListener(this)
            buttonAccept = findViewById(R.id.button_accept)
            buttonDecline = findViewById(R.id.button_decline)
    
            buttonAccept.setOnClickListener {
                // Update the termsAccepted value to true in SharedPreferences
                val editor = sharedPref.edit()
                editor.putBoolean("terms_accepted", true)
                editor.apply()
    
                val textAcceptTac = getString(R.string.txt_accept_tac)
                Toast.makeText(this, textAcceptTac, Toast.LENGTH_LONG).show()
    
                setContentView(R.layout.activity_main)
            }
    
            buttonDecline.setOnClickListener {
                val textDeclineTac = getString(R.string.txt_decline_tac)
                Toast.makeText(this, textDeclineTac, Toast.LENGTH_LONG).show()
                finish()
            }
        }
    
        override fun onScrollChanged() {
            svMain?.let { scrollView ->
                val view = scrollView.getChildAt(scrollView.childCount - 1) as View
                val bottomDetector = view.bottom - (scrollView.height + scrollView.scrollY)
    
                if (bottomDetector <= 0) {
                    buttonAccept.isEnabled = true
                    buttonDecline.isEnabled = true
                } else {
                    buttonAccept.isEnabled = false
                    buttonDecline.isEnabled = false
                }
            }
        }
    
        override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
            TODO("Not yet implemented")
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search