Expand and collapse a colapsingToolBar with a button in Kotlin - android

I want to expand and collapse a CollapsingToolBar in android studio
when i click a button.
I want it collapsed by dafault. Im doing something wrong and i dont know what, thanks for the help.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//maybe this doesnt go here, im trying to make it collapse by default
var start = appbar.setExpanded(false)
button.setOnClickListener {
if(start == appbar.setExpanded(false)){
start = appbar.setExpanded(true)
}else{
start = appbar.setExpanded(false)
}
}
}

Try this:
class MainActivity : AppCompatActivity() {
private var isAppbarExpanded = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
if(!isAppbarExpanded){
appbar.setExpanded(true)
isAppbarExpanded = true
}else{
appbar.setExpanded(false)
isAppbarExpanded = false
}
}
}

Related

How to start another activity in function? Kotlin

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
}
}
fun switchActivity(){
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
I want to start an activity from a function and not from the main activity..
I can start an activity from main class but in function, the code doesnt work..
Please help.. I'm new to kotlin android programming..
You can also try like this
class MainActivity : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nextActivity(this, MainActivity2())
}
private fun nextActivity(context: Context, activity: Activity) {
startActivity(Intent(context, activity::class.java))
}
}
Just use it
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
switchActivity()
}
}
fun switchActivity(){
val navigate = Intent(this#MainActivity, Activity2::class.java)
startActivity(navigate)
}
Actually i don't get your question clearly. But i'll try to answer your question based on my understanding of yours.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
switchActivity()
// if you want to pass the class through the paramater,
// can try this
// TODO: Another Way
// switchActivityCustom(Activity2::class.java)
}
}
fun switchActivity(){
val navigate = Intent(this, Activity2::class.java)
startActivity(navigate)
}
// TODO: Another Function
// fun switchActivityCustom(destination: Class<*>,){
// val navigate = Intent(this, destination)
// startActivity(navigate)
// }
}
And the last one is, put your function inside the class (in this case: MainActivity Class)
If your function is declared at the top level, without a reference to this, you'd need to pass an instance of Activity to your function:
fun switchActivity(activity: Activity) {
val navigate = Intent(activity, Activity2::class.java)
activity.startActivity(navigate)
}

How to get all ImageButton ID's on Android with Kotlin

I have ImageButtons
and i send a Toast from override fun onCreate(savedInstanceState: Bundle?)
when its clicked successful.
I want loop trough all ImageButtons to add the setOnClickListener to them.
this works:
works without a loop:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<ImageButton>(R.id.r1col1).setOnClickListener {
toastContentDescription(it)
}
}
private fun toastContentDescription(it: View) {
val contentDescription = it.contentDescription
val myToast = Toast.makeText(applicationContext, contentDescription, Toast.LENGTH_SHORT)
myToast.show()
}
}
works not, not start anymore
I found a example for Android Java Buttons here: How to get all Buttons ID's in one time on Android
So i modified my code to following. But then the app not start anymore (gives me no errors).
i guess i have to get the id first and then the problem is solved.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for (i in 0..4) {
val id: Int = resources.getIdentifier("R.id.r1col$i", "id", this.packageName)
findViewById<ImageButton>(id).setOnClickListener {
toastContentDescription(it)
}
}
}
private fun toastContentDescription(it: View) {
val contentDescription = it.contentDescription
val myToast = Toast.makeText(applicationContext, contentDescription, Toast.LENGTH_SHORT)
myToast.show()
}
}
Is there any way to set the setOnClickListener to all ImageButtons in a loop (etc.) Code?
Remove the "R.id." prefix from the string you’re passing.

Android Studio: How do you make a method when pressing a button with Kotlin?

I've tried making the below method for a button on Android Studio, and when I run, the app crashes.
MainActivity : AppCompatActivity() {
val button = findViewById(R.id.button) as Button
var count = 0
val textView = findViewById(R.id.textView) as TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener{
buttonAction()
}
}
fun buttonAction(){
count = count+1
textView.text = count.toString()
}
}
I am well aware that in order for the button to work, you need to put stuff into the "button.setOnClickListener" part (see below). However, I want to make sure that my code is as neat and clean as possible by calling the method inside the "button.setOnClickListener" rather than put every stuff into that part.
MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById(R.id.button) as Button
var count = 0
val textView = findViewById(R.id.textView) as TextView
button.setOnClickListener{
count = count+1
textView.text = count.toString()
}
}
}
Is there any fixes for the 1st code, or would I have to settle for the 2nd code?
class MainActivity : AppCompatActivity() {
private var count = 0
private var button: Button? = null
private var textView: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button = findViewById(R.id.button)
textView = findViewById(R.id.textView)
button?.setOnClickListener {
buttonAction()
}
}
private fun buttonAction() {
count += 1
textView?.text = count.toString()
}
}
The reason for your app crash is that you're trying to assign views(button and textView) that do not (yet) exist by calling them before onCreate.
val button = findViewById(R.id.button) as Button
val textView = findViewById(R.id.textView) as TextView
override fun onCreate(savedInstanceState: Bundle?) { ...
Just put this lines inside onCreate after setContentView(R.layout.activity_main).
You can think of it as trying to shake your friend's hand before meeting him.
And about making code clean, I wouldn't worry about it too much, your second solution is good enough for a beginner.
Not sure if you're aware of Android Basics in Kotlin course, but it can help you a lot if you follow it step by step.
And just to give you an answer, here is a code
class MainActivity : AppCompatActivity() {
var count = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById(R.id.button) as Button
val textView = findViewById(R.id.textView) as TextView
button.setOnClickListener{
buttonAction()
textView.text = count.toString()
}
}
fun buttonAction(){
count += 1
}
}
It's not the smoothest solution, but as i said, it's good enough

How do I switch image and text back and forth in tapping the button?

The first time I tap the button, both the image1 and text1 changed to image2 & text2. How do I change both the image and text back to 1st set the 2nd time I tap the button and change them to 2nd set the 3rd time I tap the button ?
Here is my current codes:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
fun eating(view: View?) {
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
displayImage(R.drawable.after_cookie)
}
private fun displayMessage(message: String) {
val afterEatingText = findViewById<View>(R.id.before_eating) as TextView
afterEatingText.text = message
}
private fun displayImage(imagesource: Int){
val afterEatingImage: ImageView = findViewById<View>(R.id.before_cookie) as ImageView
afterEatingImage.setImageResource(imagesource)
}
}
var isClick:Boolean=false
var afterEatingText :TextView?=null
var afterEatingImage:ImageView?=null
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
afterEatingImage= findViewById<ImageView>(R.id.before_cookie) // find your id you can also direct call with its ids in kotlin
afterEatingText = findViewById<ImageView>(R.id.before_eating)
}
fun eating(view: View?) {
if(isClick){
isClick=true
// set your first image and text
}else {
isClick=false
val afterEatingText:String="I am so full!!"
displayMessage(afterEatingText)
displayImage(R.drawable.after_cookie)
}}
private fun displayMessage(message: String) {
afterEatingText.text = message
}
private fun displayImage(imagesource: Int){
afterEatingImage.setImageResource(imagesource)
}
}

Can't assign values outside onTouchListener

when a item is pressed I want to modify a value declared outside the onCreate method. But when the item is pressed, the onTouchListener does the other functions except modify that value. I've tried to write the same instruction to modify the value outside the onTouchListener method and it worked. This is my code:
class MainActivity : AppCompatActivity() {
var ciao: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var listenerUser = View.OnTouchListener(function = { view, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
this.ciao = "ciao"
}
true
})
}
override fun onResume() {
super.onResume()
if (ciao != null) {
println(ciao!!)
}
}
}
When my application resumes the var ciao is null, but if I put this.ciao = "ciao" outside the listener the var isn't null.
You need to set your OnTouchListener for the view that should react:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listener = View.OnTouchListener(function = {view, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
this.ciao = "ciao"
}
true
})
clickedView.setOnTouchListener(listener)
}
If you just want to detect a click you are probably better off using a OnClickListener like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
clickedView.setOnClickListener(_ ->
this.ciao = "ciao"
)
}

Categories

Resources