Search code examples
androidandroid-layoutandroid-relativelayoutonclicklistenerclickable

How to make every content of a Relative Layout clickable in android


I have a layout:

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="227dp"
        android:addStatesFromChildren="true"
        android:clickable="true"
        android:duplicateParentState="false"
        android:focusable="true"
        android:id ="@+id/relLay">

        <Gallery
            android:id = "@+id/gvImage"
            android:layout_height="200dp"
            android:layout_width="match_parent"
            android:paddingRight="5dp"
            android:paddingLeft="5dp"
            android:fadingEdgeLength="2dp"
            android:clickable="true"/>


    <TextView
        android:clickable="true"
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/gvImage"
        android:layout_alignTop="@+id/gvImage"
        android:layout_alignRight="@+id/gvImage"
        android:gravity="center"
        android:text=SOMETHING"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:textStyle="bold"
        android:textColor="#FF0000"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:layout_alignParentBottom="true" />

        <ToggleButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/toggleButton"
            android:layout_marginTop="20dp"
            android:layout_marginRight="20dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:textOn=""
            android:textOff=""
            android:drawableTop="@drawable/favorite_check"
            android:gravity="center"
            android:background="@null"/>

        <TextView
            android:background="#CCCCCC"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Rate: ?"
            android:textColor="#FF0000"
            android:textStyle="bold"
            android:textSize="20dp"
            android:id="@+id/textView2"
            android:layout_alignBottom="@+id/gvImage"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>

And it is called by a fragment class,

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.search_result, container, false);
        Gallery gallery = (Gallery)view.findViewById(R.id.gvImage);
        gallery.setAdapter(new ImageAdapter(getActivity().getApplicationContext()));

        
        return view;
    }

I want to do something like, if I click any item inside of RelativeLayout of that xml file it will do something like, will go to another class or Toast text. How will I do this?


Solution

  • Implement onClickListener to each and every View.

    Simplest way will be

    <TextView
                android:background="#CCCCCC"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Rate: ?"
                android:textColor="#FF0000"
                android:textStyle="bold"
                android:textSize="20dp"
                android:id="@+id/textView2"
                android:layout_alignBottom="@+id/gvImage"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                onclick="executeTextViewClick" />
    

    And in java class declare a function as follows.

    public void executeTextViewClick(View view){
    
    //Toast here or navigate to other activity 
    
    }