Home > Software design >  [Android/Kotlin]SetOnClickListener not working
[Android/Kotlin]SetOnClickListener not working

Time:10-13

I found out that sometimes setOnClickListener doesn't work when I click the view. When I run the application and click the view, it has NO RESPONSE. There is no error message it just doesn't work.

In the other part of the project setOnClickListener works well and I can't find any difference. I have no idea why it is not working even though the code is same...

Here is my activity file.

class SplashActivity : AppCompatActivity() {

    // 애니메이션을 처리하기 위한 runnable 객체
    private val mRunnable : Runnable = Runnable {
        if (!isFinishing) { // 끝나지 않았을 때

            // 슬라이드업 애니메이션 실행
            slideUp(illo_logo, 500)
            fadeIn(illo_copy, 500)
            slideUp(illo_copy, 500)
            fadeIn(btn_login, 500)
            slideUp(btn_login, 500)
            fadeIn(btn_sign, 500)
            slideUp(btn_sign, 500)
        }
    }

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

        // 핸들러 이용해 0.5초 딜레이 후 mRunnable 실행
       Handler().postDelayed(mRunnable, 500)

        // 로그인 버튼
        btn_login.setOnClickListener {
            Toast.makeText(this@SplashActivity, "로그인 버튼 클릭", Toast.LENGTH_SHORT).show()
            val intent = Intent(this@SplashActivity, LoginActivity::class.java)
            startActivity(intent)
            finish()
        }

        // 가입 버튼
        btn_sign.setOnClickListener {
            Toast.makeText(this@SplashActivity, "가입 버튼 클릭", Toast.LENGTH_SHORT).show()
            val intent = Intent(this@SplashActivity, SignActivity0::class.java)
            startActivity(intent)
            finish()
        }
    }

    // 위로 올리기
    fun slideUp(view : View, time : Int) {
        val animation = TranslateAnimation(0f, 0f, 0f, -300f )   // 애니메이션 인스턴스화
        animation.duration = time.toLong()  // 애니메이션 지속시간 설정
        animation.fillAfter = true  // 애니메이션 종료 후 상태 유지
        view.startAnimation(animation)  // 애니메이션 실행
    }

    // 페이드 인
    fun fadeIn(view: View, time: Int) {
        val anim = AnimationUtils.loadAnimation(this@SplashActivity, R.anim.fade_in)    // 애니메이션 로드
        anim.duration = time.toLong()   // 애니메이션 지속시간
        anim.fillAfter = true   // 애니메이션 종료 후 상태 유지
        view.startAnimation(anim)   // 애니메이션 실행
    }
}

Here is my XML code.

<?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/illo_base_color"
    tools:context=".SplashActivity">

    <ImageView
        android:id="@ id/illo_logo"
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:layout_marginBottom="250dp"
        android:src="@drawable/app_icon_default"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@ id/illo_copy"
        android:layout_width="141dp"
        android:layout_height="26dp"
        android:layout_marginTop="4dp"
        android:layout_marginBottom="206dp"
        android:fontFamily="@font/tmoneyroundwind_extrabold"
        android:letterSpacing="0"
        android:text="우리는 illo 모임"
        android:textColor="@color/illo_light_purple"
        android:textSize="20sp"
        android:textStyle="normal"
        android:visibility="invisible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="@ id/illo_logo"
        app:layout_constraintStart_toStartOf="@ id/illo_logo"
        app:layout_constraintTop_toBottomOf="@ id/illo_logo"
        app:layout_constraintVertical_bias="0.0" />

    <RelativeLayout
        android:id="@ id/btn_login"
        android:layout_width="312dp"
        android:layout_height="48dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="150dp"
        android:layout_marginEnd="24dp"
        android:background="@drawable/button_long"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/illo_copy">

        <TextView
            style="@style/IlloKrTitle1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="로그인하기"
            android:textColor="@color/illo_white100"
            android:textSize="16dp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@ id/btn_sign"
        android:layout_width="312dp"
        android:layout_height="48dp"
        android:layout_marginStart="24dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="24dp"
        android:background="@drawable/button_long"
        android:visibility="invisible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@ id/btn_login">

        <TextView
            style="@style/IlloKrTitle1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="가입하기"
            android:textColor="@color/illo_white100"
            android:textSize="16dp" />
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

Add android:clickable = "true" and android:focusable = "true" on your widgets like:

<RelativeLayout
    android:id="@ id/btn_login"
    android:layout_width="312dp"
    android:layout_height="48dp"
    android:layout_marginStart="24dp"
    android:layout_marginTop="150dp"
    android:layout_marginEnd="24dp"

    android:clickable="true"
    android:focusable="true"

    android:background="@drawable/button_long"
    android:visibility="invisible"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/illo_copy">

Extra: add this property android:foreground="?attr/selectableItemBackground" That code add selectable animation on RelativeLayout but work better if you added on android:background property. Work with any Widget try and play nice!! ;)

CodePudding user response:

Did you forget to connect those variables? If you have added the whole activity's code than I don't think that doesn't return any error, it must return errors. Did you check logcat?

val btn_login = findViewById<Button>(R.id.btn_login)

If I am missing something than try by making btn_login clickable. Cause your btn_login isn't actually a button. It's a relativeLayout. And for some views default clicking expression is false by default that's why you must enable them.

android:clickable = "true" 

I don't remember if it's the accurate one since I am not using Android Studio right now.

CodePudding user response:

I guess u are using Kotlin Synthetic. If u are using it, try to look at the imported files. If u copied the same codes that have views in Kotlin Synthetic, it's also copied the same imported files. Just delete the imported files and change them to the right ones.

ex:

import kotlinx.android.synthetic.main."Your layout name with same codes".*

change it to

import kotlinx.android.synthetic.main.activity_splash.*

It's better to discard Kotlin Synthetic and move on to ViewBinding

  • Related