androidlong-click

TextView handle LongClick, parent handle textview's click


I have a RelativeLayout with a TextView child, when set long click listener for text view, and set click listener for parent layout as below:

textview.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                Toast.makeText(getContext(), "longclick", Toast.LENGTH_SHORT).show();
                return true;
            }
        });

layout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getContext(), "layout onclick", Toast.LENGTH_SHORT).show();
            }
        });

How do i handle layout's click listener when click on textview ? I also use GestureDetector to deal with, but ondown event must return true, this cause parent view can not handle onclick listener.


Solution

  • You need to also set ClickListener() to your textview

    check the below tested example it will work for u

    Try this

    public class MainActivity extends AppCompatActivity {
    
    
        TextView mTitle;
        LinearLayout toolbarIcon;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mTitle = (TextView) findViewById(R.id.toolbar_title);
            toolbarIcon = (LinearLayout) findViewById(R.id.toolbarIcon);
            mTitle.setText("Nilesh Rathod");
    
    
            mTitle.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    View parent = (View) v.getParent();
                    parent.performClick();
                }
            });
    
            mTitle.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(MainActivity.this, "mTitle Clicked", Toast.LENGTH_SHORT).show();
                    return true;
                }
            });
    
            toolbarIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(MainActivity.this, "toolbarIcon Clicked", Toast.LENGTH_SHORT).show();
                }
            });
        }
    
    }
    

    LAYOUT

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbarIcon"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="vertical">
    
    
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:textStyle="bold" />
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="Nilesh"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="Nilesh"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="Nilesh"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:text="Nilesh"
            android:textStyle="bold" />
    
    
    </LinearLayout>