Android: onClick on LinearLayout with TextView and Button

I have a snippet that uses the following XML layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card"
    android:clickable="true"
    android:onClick="editActions"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        style="@style/CardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:text="@string/title_workstation" />

    <Button
        android:id="@+id/factory_button_edit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:duplicateParentState="true"
        android:text="@string/label_edit" />

</LinearLayout>

As you can see, I have the onClick parameter set to LinearLayout. Now TextViewit works correctly on the entire empty area. Only in Buttonit does not call the onClick method that I set.

This is normal? What do I need to do so that the onClick method is called everywhere on LinearLayout?

+5
source share
9 answers

add onClick Action for your button in code for example:

Button btn = findViewById(R.id.factory_button_edit);
btn.setOnClickListener(button_click);

      OnClickListener button_click = new OnClickListener() {

        @Override
        public void onClick(View v) {
            editActions;
        }
    };

or xml add

android:onClick="editActions"

on the button

+6
source

, LinearLayout:

android:onClick="editActions" 
android:clickable="true"

, xml:

public void editActions(View view){

           //something TODO

    }
+9

, TouchDelegate:

Android TouchDelegate, , . , , . .

+4

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/card"
    android:clickable="true"
    android:onClick="editActions"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        style="@style/CardTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/title_workstation" />

    <Button
        android:id="@+id/factory_button_edit"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickMe"
        android:text="@string/label_edit" />

</LinearLayout>

    public void editActions(View v) {
        Toast.makeText(this, "Layout", Toast.LENGTH_SHORT).show();
    }

    public void clickMe(View v) {
        Toast.makeText(this, "clickMe", Toast.LENGTH_SHORT).show();
    }
+2

, , , - :

android:clickable="false"

, , setOnClickListener , ,

:

<LinearLayout  
      android:id="@+id/parent"  
      android:layout_width="match_parent"  
      android:layout_height="wrap_content"  
      android:orientation="horizontal">  
      <ImageButton  
           android:clickable="false"  
           android:layout_width="40dp"  
           android:layout_height="40dp"  
           android:text="Cinema"  
           android:textSize="13sp"  
           android:layout_marginRight="10dp" />  
      <Button  
           android:clickable="false"  
           android:layout_width="match_parent"  
           android:layout_height="40dp"  
           android:text="Check in"  
           android:textSize="13sp"/>  
 </LinearLayout> 
+2

, :

 android:clickable="false"

.

+2

, :

android:duplicateParentState="true"
0
View.OnClickListener myViewHandler = new View.OnClickListener() {
        public void onClick(View v) {
                RelativeLayout pressedRelViewx = (RelativeLayout) v;
                TextView pressedTextView = (TextView) pressedRelViewx.getChildAt(0);
                String text = pressedTextView.getText().toString();
                Toast.makeText(getApplicationContext() , "My Text "+ text , Toast.LENGTH_LONG).show() ;
        }
    };


Relative_Layout = (RelativeLayout)findViewById(R.id.my_container);
Relative_Layout.setOnClickListener(myViewHandler);
0

, , ,

OnClick Listener [Android].

LinearLayout mLinearLayout = findViewById(R.id.your_linear_layout_name);

mLinearLayout.setonClickListener(new View.onClickListener(){
   @Overide
   public void onClick(View v){
     //do want to do here

    }

});

:

You cannot execute the OnClick Listener for a button when the button is inside a line layout that is already listening onClick, and if this is done, the first parent (Linear Layout) is clicked. It’s better not to do pre-nested onClick listeners.

0
source

All Articles