I am working on an XML design file for an Android application and got an issue with my ListFragment (android.support.v4.app.ListFragment).
The problem: If I add HorizontalScrollView to my XML layout (to put my TextView inside) the ContextMenu (android.view.ContextMenu) wont open by long press on one item in the list.
When I delete the HorizontalScrollViews everything works fine. That means just placing the TextView without the HorizontalScrollView.
The Log gives no error or warning. The problem should be from the XML not within the Java Code.
<!-- main root-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="@color/colorWhite">
<!-- SOME MORE STUFF .... -->
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="@+id/linearLayout1"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"
android:layout_toLeftOf="@+id/tv_activity_date">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView14"
android:src="@drawable/ic_short_text_black_18dp"
android:layout_marginRight="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="0dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="type"
android:id="@+id/tv_activity_type"
android:textColor="@color/colorAccent" />
</HorizontalScrollView>
</LinearLayout>
<!-- SOME MORE STUFF .... -->
</RelativeLayout>
So, it may be blocking the view somehow. Did someone found a solution for that? Thanks in advance ! : )
Edit: I tried putting the HorizontalScrollView in the root View and placing nothing into it, also defining no size. So its just a part of the whole layout, but with no useage. Even now, the HorizontalScrollView blocks the ListFragment items, so i cant long touch/press to open the ContextMenu. If I delete it, the problem is gone.
This is the correct answer, and works fine.
Add this code in your OnCreate()
or OnCreateView()
final HorizontalScrollView horizontalScrollView = (HorizontalScrollView)findViewById(R.id.horizontal);
registerForContextMenu(horizontalScrollView);
GestureDetector.OnGestureListener listener = new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e)
{
openContextMenu(horizontalScrollView);
Toast.makeText(MainActivity.this, "LongClick", Toast.LENGTH_SHORT).show();
}
};
final GestureDetector gestureDetector = new GestureDetector(this, listener);
horizontalScrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
});
1-Are you setting the correct view in registerforContextMenu()
?
2-If your TextView (after long press) opens context menu, maybe need to setLongClickable()
your HorizontalScrollView
. Try this answer
3-You really need HorizontalScrollView
with TextView
? Look this HorizontalScrollView references
Hope it helps!