Search code examples
androidtouch-eventhorizontalscrollview

How to handle onClick on a RelativeLayout that is part of HorizontalScrollView


I have RelativeLayouts in HorizontalScrollView. When a user clicks on one of the layouts (or any of its childs) I need to change the background color of the certain RelativeLayout and get its ID for further process. I saw this might be done with DuplicateParentState or with onTouchEvent. Which way is recommended?

My XML file after editing the XML file as proposed:

<RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
            <youme.appyoume.com.ExDialog>

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="20dp"
                android:clickable="true"
                android:duplicateParentState="true"
                android:onClick="onClick"
                android:src="@drawable/ic_launcher" />

            <EditText
                android:id="@+id/TextView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:duplicateParentState="true"
                android:ems="10"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:inputType="textMultiLine"
                android:text="text" >

                <requestFocus />
            </EditText>
            </youme.appyoume.com.ExDialog>
        </RelativeLayout>

Thanks, Simon


Solution

  • If you want to use the custom class youve created in your xml you just use the entire root name of the object. So for instance if you have a class in

    com.your.package.customButton
    

    you would write in your xml

    <RelativeLayout
       ...layoutstuff config>
    
      <com.your.package.customButton
        ...customButton layout config/>
    
    </RelativeLayout>
    

    if your subclassing a relativelayout you would use it like this

    <youme.appyoume.com.ExDialog
        ...layout setup stuff>
    
        <Button
         ...button setup stuff
        />
    
     </youme.appyoume.com.ExDialog>