0

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.

Janvi Vyas
  • 732
  • 5
  • 16

2 Answers2

0

Why don't you use both the ClickListener on your relative layout, One long and one regular.

layout.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

   Toast.makeText(HomeSafeActivity.this, "Normal Press", Toast.LENGTH_LONG).show();

});

layout.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

        return true;
    }
});
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Shafin Raza
  • 21
  • 1
  • 8
  • Thank's for your reply, I just want textview handle long click, parent layout handle single click, when i click on textview or on layout, layout handle click event – user9709179 Apr 27 '18 at 09:19
0

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>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thanks, is there any way to override textview or layout and use event dispatcher or intercept touch event to do this? I want to write custom view to make this more common. – user9709179 Apr 27 '18 at 09:29