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.
Remove
android:clickable="false"
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