0

I am working on a fragment in which I need to open another activity when the user clicks a certain region of that fragment. The part of opening the activity is already taken care of, but i really have no idea how to detect the touching in that certain area of the fragment, specifically because it is not a rectangle relative to the screen, it has to be relative to it's fragment since you can scroll the fragments up and down.

The area of that fragment has things behind, let's say as an example an imageView and a textView, which may change at certain moments. I don't want to click the TextView or ImageView and go to the same intent for opeing the activity, I want the entire region to be clickable.

One thing i thought about was to set all those "things" that go behind that area inside a relative layout and somehow make that relative layout touchable, or detect the touch on any part of that layout, I really don't know how to proceed from here. Any help will be really apreciated.

Here is an example of what I need https://i.imgur.com/kmZ6R0H.jpg

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
tools:context="com.example.roo.proyi.alarm_fragment"
>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:includeFontPadding="false"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="08:00"
    android:textSize="70dp"
    android:id="@+id/time_view"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/vertical_separator"
    android:layout_toStartOf="@+id/vertical_separator" />

<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="S"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_sunday"
    android:gravity="center_horizontal"
    android:clickable="true"
    android:onClick=""
    android:layout_below="@+id/time_view"
    android:layout_alignLeft="@+id/time_view"
    android:layout_alignStart="@+id/time_view" />

<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="M"
    android:layout_alignLeft="@id/check_sunday"
    android:layout_marginLeft="26dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_monday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_sunday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />
<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="T"
    android:layout_alignLeft="@id/check_monday"
    android:layout_marginLeft="25dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_tuesday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_monday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />
<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="W"
    android:layout_alignLeft="@id/check_tuesday"
    android:layout_marginLeft="28dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_wednesday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_monday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />
<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="T"
    android:layout_alignLeft="@id/check_wednesday"
    android:layout_marginLeft="26dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_thursday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_monday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />
<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="F"
    android:layout_alignLeft="@id/check_thursday"
    android:layout_marginLeft="20dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_friday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_monday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />
<TextView
    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="S"
    android:layout_alignLeft="@id/check_friday"
    android:layout_marginLeft="21dp"
    android:textSize="30dp"
    android:textColor="#FFFFFF"
    android:id="@+id/check_saturday"
    android:layout_alignTop="@+id/check_sunday"
    android:layout_toRightOf="@+id/check_monday"
    android:gravity="center_horizontal"
    android:clickable="true"
    />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="80dp"
    android:id="@+id/vertical_separator"
    android:src="@drawable/imageview_vertical_separator"
    android:layout_centerVertical="true"
    android:layout_marginRight="7dp"
    android:layout_marginLeft="7dp"
    android:layout_toLeftOf="@+id/image_bed"
    android:layout_toStartOf="@+id/image_bed" />

<ImageView
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:id="@+id/image_bed"
    android:src="@drawable/imageview_bed"
    android:tint="@android:color/white"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/sleep_amount"
    android:layout_toStartOf="@+id/sleep_amount" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="2"
    android:textSize="45dp"
    android:id="@+id/sleep_amount"
    android:layout_alignBottom="@+id/image_bed"
    android:layout_alignParentRight="true"
    android:layout_marginRight="10dp"
    android:layout_alignParentEnd="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textSize="17dp"
    android:text="10"
    android:id="@+id/sleep_time"
    android:layout_marginTop="7dp"
    android:layout_below="@+id/image_bed"
    android:layout_alignLeft="@+id/image_bed"
    android:layout_alignStart="@+id/image_bed" />

<TextView
    android:layout_width="wrap_content"
    android:textSize="17dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Minutes"
    android:id="@+id/minutes"
    android:layout_alignTop="@id/sleep_time"
    android:layout_below="@+id/sleep_amount"
    android:layout_alignRight="@+id/sleep_amount"
    android:layout_alignEnd="@+id/sleep_amount" />

<Button
    android:layout_width="50dp"
    android:layout_height="52dp"
    android:background="@drawable/button_toggle_alarm_switch_on"
    android:id="@+id/button_alarm_onoff"
    android:layout_centerVertical="true"
    android:layout_marginRight="10dp"
    android:layout_toLeftOf="@+id/time_view" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:alpha="0"
    android:id="@+id/settings_region"
    android:layout_alignParentTop="true"
    android:layout_alignBottom="@+id/check_saturday"
    android:layout_alignParentRight="true"
    android:layout_alignLeft="@+id/vertical_separator" />

Rodrigo Miranda
  • 113
  • 3
  • 13

2 Answers2

1

You can wrap your TextView and ImageView in a layout (in you fragment xml) and then set OnClick or OnTouch Listener on this whole layout.

mikeD
  • 481
  • 3
  • 5
0

You can add a LinearLayout or some other layout to cover the area you want to make clickable. The you can set the layout's clickable property to true and add an action listener to the layout.

Check this question for more reference.

Community
  • 1
  • 1
Shabbir Ahmad
  • 226
  • 2
  • 8
  • I was able to achieve the objective by creating a button in the xml that covers the entire region over the other views and has a transparency of 100%, i can launch the other activity. But I don't know if that may be the best practice, what do you think? because messing up with the current layout would make me change a lot of alignments already made – Rodrigo Miranda Oct 31 '15 at 21:20
  • Okay let me explain, fragments are like activities excluding a couple of things. Fragments have their own layout like an activity and their own life cycle. Now as you can define the layout for the fragment like a LinearLayout or RelativeLayout and then add the child views like Buttons or TextViews to it. This was you will maintain the hierarchy and achieve what you want. By using a full size 100% transparent button you are hiding the child views and that is not a good approach. – Shabbir Ahmad Nov 01 '15 at 11:07
  • I know child views are visible but basically they are under the transparent button. – Shabbir Ahmad Nov 01 '15 at 11:08
  • In your case dont change any thing just make your RelativeLayout of the fragment clickable and add a listener to it. – Shabbir Ahmad Nov 01 '15 at 11:09