Search code examples
android-studiokotlinandroid-fragmentsonclicklistener

Why does my onClickListener not work in my fragment?


I've got a bunch of onclicklisteners just like this, but this one just doesn't want to work for some reason, I'm sure I'm missing something obvious lol. All the button tryna do is logout.

I know at this point that it's not that the startActivity thats not working, rather the whole listener, as I added the Toast make text alert that isn't set off when I tap the logout button.

I've also tried "binding = FragmentProfileBinding.inflate(layoutInflater)" with "binding!!.btnLogout.setOnClickListener" but the onClickListener still isn't working or activating the make text alert when I tap logout.

But yeah any help would be greatly appreciated.

Here is my kotlin fragment code:

package com.example.nonutsinmybasket.fragments

import android.content.Intent
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.ViewGroup
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.example.nonutsinmybasket.R
import com.example.nonutsinmybasket.activity.Login
import com.example.nonutsinmybasket.databinding.FragmentProfileBinding
import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.fragment_profile.view.*

class Profile(var userId: String?, var sharedPrefs: SharedPreferences) : Fragment() {
    var binding: FragmentProfileBinding? = null
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        super.onCreate(savedInstanceState)
        val view  = inflater.inflate(R.layout.fragment_profile, container, false)
        setupLogoutButton(view, sharedPrefs)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        // find views by id
    }

    private fun setupLogoutButton(view: View, sharedPrefs: SharedPreferences) {
        view.btnLogout.setOnClickListener {
            Toast.makeText(activity,
                "Tap detected",
                Toast.LENGTH_LONG).show()
            sharedPrefs.edit().remove("Email").apply()
            sharedPrefs.edit().remove("UserId").apply()
            FirebaseAuth.getInstance().signOut()
            startActivity(Intent(context, Login::class.java))
            activity?.finish()
        }
    }
}

And here is my associated XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/bg"
    android:focusableInTouchMode="true"
    tools:context=".fragments.Profile">

    <LinearLayout
        android:id="@+id/profileFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:background="@drawable/bg_rounded_ractangle_10"
        android:backgroundTint="@color/colorPrimary"
        android:orientation="vertical"
        android:padding="@dimen/_10sdp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:clickable="true">
        <TextView
            android:id="@+id/btnLogout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:fontFamily="@font/p_bold"
            android:gravity="center"
            android:text="Logout"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/aboveFooter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_10sdp"
        android:layout_marginTop="@dimen/_10sdp"
        android:layout_marginEnd="@dimen/_10sdp"
        android:layout_marginBottom="@dimen/_10sdp"
        android:background="@drawable/bg_rounded_ractangle_10"
        android:backgroundTint="@color/colorPrimary"
        android:orientation="vertical"
        android:padding="@dimen/_10sdp"
        app:layout_constraintBottom_toTopOf="@id/profileFooter">

        <TextView
            android:id="@+id/updateInfo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:fontFamily="@font/p_bold"
            android:gravity="center"
            android:text="Update Information"
            android:textColor="@color/white"
            android:textSize="@dimen/_12sdp" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/_5sdp"
                android:layout_marginTop="@dimen/_5sdp"
                android:layout_marginRight="@dimen/_5sdp"
                android:layout_marginBottom="@dimen/_5sdp"
                android:background="@drawable/bg_rounded_ractangle_10"
                android:orientation="vertical"
                android:padding="@dimen/_10sdp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <EditText
                        android:id="@+id/test"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="#00ffffff"
                        android:fontFamily="@font/p_reguler"
                        android:hint=""
                        android:inputType="text"
                        android:textColor="@color/text1"
                        android:textColorHint="@color/text4"
                        android:textSize="@dimen/_10sdp"
                        android:visibility="gone" />

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/_10sdp"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="vertical"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/etRegisterEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Email"
                            android:inputType="text"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="horizontal"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/etConfirmPassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Password"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                        <ImageView
                            android:id="@+id/btnShowPass"
                            android:layout_width="@dimen/_25sdp"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:alpha=".5"
                            android:padding="@dimen/_2sdp"
                            android:src="@drawable/ic_eye" />

                    </LinearLayout>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/_10sdp"
                    android:orientation="horizontal">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:background="@drawable/bg_rounded_ractangle_5_stroke"
                        android:orientation="horizontal"
                        android:padding="@dimen/_10sdp">

                        <EditText
                            android:id="@+id/confirmpassword"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:background="#00ffffff"
                            android:fontFamily="@font/p_reguler"
                            android:hint="Confirm Password"
                            android:inputType="textPassword"
                            android:maxLines="1"
                            android:textColor="@color/text1"
                            android:textColorHint="@color/text4"
                            android:textSize="@dimen/_11sdp" />

                        <ImageView
                            android:id="@+id/btnShowConPass"
                            android:layout_width="@dimen/_25sdp"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:adjustViewBounds="true"
                            android:alpha=".5"
                            android:padding="@dimen/_2sdp"
                            android:src="@drawable/ic_eye" />

                    </LinearLayout>

                </LinearLayout>


            </LinearLayout>

        </LinearLayout>
    </ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>

Not sure if this helps but in when writing view.btnLogout I had to import "import kotlinx.android.synthetic.main.fragment_profile.view.*" otherwise the text was red.


Solution

  • Certified bruh moment - my scroll view was covering my buttons, knew it would be something like this