'Android OnClick on main layout does not work, is it because of MotionLayout?
I want to be able to perform onClickListner on root RelativeLayout called "rl". Not it does not work I suspect it can be because of MotionLayout as a children. How to solve that?
Here is what I have done so far, my code.
ShowNoteActivity.java
(...)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
(...)
RelativeLayout rl = findViewById(R.id.rl);
rl.setOnClickListener(view -> {
Toast.makeText(this, "abc", Toast.LENGTH_SHORT).show();
});
}
show_note_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true">
<pl.jawegiel.simplenotepad.CustomMotionLayout
android:id="@+id/motion_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
app:layoutDescription="@xml/show_note_activity_scene">
<TextView
android:id="@+id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="center"
android:text="@string/note_title"
android:textColor="?attr/myTextColor"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/note_title_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView3"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="top|left"
android:hint="@string/enter_title"
android:lines="2"
android:maxLines="10"
android:minHeight="48dp"
android:textColor="?attr/myTextColor"
android:textColorHint="?attr/myHintTextColor"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/note_title" />
<TextView
android:id="@+id/note_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/note_title_value"
android:layout_marginTop="10dp"
android:clickable="false"
android:duplicateParentState="true"
android:focusable="false"
android:gravity="center"
android:text="@string/note_desc"
android:textColor="?attr/myTextColor"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/note_title_value" />
<androidx.core.widget.NestedScrollView
android:id="@+id/nestedScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
app:layout_constraintTop_toBottomOf="@id/note_desc">
<RelativeLayout
android:id="@+id/rl2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false">
<TextView
android:id="@+id/note_desc_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:textColor="?attr/myTextColor"
android:textColorHint="?attr/myHintTextColor"
android:textSize="18sp" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</pl.jawegiel.simplenotepad.CustomMotionLayout>
</RelativeLayout>
CustomMotionLayout.java
public class CustomMotionLayout extends MotionLayout {
public CustomMotionLayout(Context context) {
super(context);
}
public CustomMotionLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomMotionLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}
show_note_activity_scene.xml
<?xml version="1.0" encoding="utf-8"?>
<MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ConstraintSet android:id="@+id/start">
<Constraint
android:id="@id/note_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<Constraint
android:id="@id/note_title_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/note_title"/>
<Constraint
android:id="@id/note_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/note_title_value"/>
</ConstraintSet>
<ConstraintSet android:id="@+id/end">
<Constraint
android:id="@id/note_title"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"/>
<Constraint
android:id="@id/note_title_value"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"/>
<Constraint
android:id="@id/note_desc"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</ConstraintSet>
<Transition
app:constraintSetEnd="@id/end"
app:constraintSetStart="@id/start"
app:duration="1000">
<OnSwipe
app:touchAnchorSide="top"
app:dragDirection="dragUp"
app:moveWhenScrollAtTop="true"
app:touchAnchorId="@id/nestedScrollView"/>
<KeyFrameSet>
</KeyFrameSet>
</Transition>
</MotionScene>
Solution 1:[1]
If you want to click on parent you should disable motionlayout transition and do your transition programmatically with one button or ... Disable motion layout:
motionLayout.getTransition(R.id.yourTransition).setEnable(false)
If you must to use onSwipe in your project you can disable transition after collapsing motionlayout and return layout to top programmatically and when expand enable motionlayout transition.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sajadab |
