1

I have simple layout with EditText and a button.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:clickable="true"
    android:focusable="true">

    <EditText
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:focusable="false"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>

</LinearLayout>

I don't want EdiText to be editable and want to handle click on complete layout

 public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.linearLayout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "click", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

but it is not working I mean There is no Toast message when I am clicking.

Vivart
  • 14,900
  • 6
  • 36
  • 74

1 Answers1

-1

as @Liem Vo said in the comments: you should add android:clickable="false" to the button, and make sure not to add a click listener in java. becuse the event is handled by the child view.

for the 'EditText' you can call the parents click listener manually:

editText.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            View parent = (View) v.getParent();
            parent.performClick();
        }
    });

please refer to this question: onClick not triggered on LinearLayout with child

java-love
  • 516
  • 1
  • 8
  • 23