Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener not fired on ConstraintLayout

On my android app project i have to make a button with a ProgressBar on background and two TextView.

I make a first attempt like this:

    <android.support.constraint.ConstraintLayout
        android:id="@+id/keyboard_touch_1"
        android:layout_width="60dp"
        android:layout_height="85dp"
        android:layout_marginBottom="150dp"
        android:layout_marginStart="10dp"
        android:focusable="true"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <ProgressBar
            android:id="@+id/keyboard_touch_1_progress_bar"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="false"
            android:max="100"
            android:progress="50"
            android:progressDrawable="@drawable/button_progress_bar_default"
            android:clickable="false"
            android:focusable="false" />

        <TextView
            android:id="@+id/keyboard_touch_1_score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="2"
            android:textColor="@color/colorAccent"
            android:textSize="11dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="3"
            android:clickable="false"
            android:focusable="false" />

        <TextView
            android:id="@+id/keyboard_touch_1_letter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_marginTop="13dp"
            android:text="A"
            android:textColor="@color/colorAccent"
            android:textSize="45dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:clickable="false"
            android:focusable="false" />

    </android.support.constraint.ConstraintLayout>

It looks good but when i attempt to add an OnClickListener it doesn't work correctly.

keyboard_touch_1.setOnClickListener {
    Toast.makeText(this, "IT WORKS !!!", Toast.LENGTH_SHORT).show()
}

The OnClickListener is not fired and i have no idea of why. It's probably very simple but i doesn't understand why.

Thanks in advance.

like image 685
Valentin Michalak Avatar asked Feb 11 '18 12:02

Valentin Michalak


3 Answers

As I cannot comment due to my low rep I will have to post an answer.

Based on the code you have provided via your GitHub link you are actually trying to set the content view twice.

Firstly you are calling setContentView(R.layout.activity_game) within your onCreate() method and then DataBindingUtil.setContentView(this, R.layout.activity_game) within your onResume() method.

Please try deleting setContentView(R.layout.activity_game) and then move your data binding setup code to your onCreate method(). You can then access your constraint layout view via your "binding" object as a property.

For example:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = GameViewModel()
        val binding: ActivityGameBinding = DataBindingUtil.setContentView(this, R.layout.activity_game)
        binding.viewModel = viewModel

        binding.keyboardTouch1.setOnClickListener {
            Toast.makeText(this, "IT WORKS !!!", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.startUpdate()
    }

    override fun onPause() {
        super.onPause()
        viewModel.stopUpdate()
    }
like image 156
RhysP Avatar answered Nov 06 '22 07:11

RhysP


Remove

android:clickable="false"

From Docs

Defines whether this view reacts to click events

and move keyboard_touch_1.setOnClickListener {...} at the end of onResume because DataBindingUtil.setContentView will reset the previously set layout (setContentView) therefore you have a new layout with new views.

Note : you are using data-binding along with normal initialization setContentView(R.layout.activity_game) technique so the optimal way would be to use

Event Handling via data binding

like image 27
Pavneet_Singh Avatar answered Nov 06 '22 07:11

Pavneet_Singh


maybe your layout is include label with id, this id override origin id.

like image 1
act262 Avatar answered Nov 06 '22 08:11

act262