Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make Relative layout onclick work?

I have a Relative layout as:

    <RelativeLayout
        android:id="@+id/iPay_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="horizontal">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="· Use iPay"
            android:textColor="#686A86"
            android:textSize="18sp" />


        <Button
            android:layout_width="50dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/i_pay"
            android:textAllCaps="false"
            android:textColor="#ffffff" />

    </RelativeLayout>

I want my relative layout to perform onCLickListener but does not work. I am able to do the setonclicklistener but it works only with the textview part and not for the image.

    btniPay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //for ipay

        }
    });

This works for the text part only but not for the image. Any idea what I might be missing.

like image 589
bhaskar Avatar asked Feb 08 '23 02:02

bhaskar


2 Answers

Alternatively, you can add android:clickable="false" in your button xml attribute.

<RelativeLayout
    android:id="@+id/iPay_btn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:orientation="horizontal">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="· Use iPay"
        android:textColor="#686A86"
        android:textSize="18sp" />


    <Button
        android:layout_width="50dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        android:background="@drawable/i_pay"
        android:textAllCaps="false"
        android:clickable="false"
        android:textColor="#ffffff" />

</RelativeLayout>
like image 150
VoidExplorer Avatar answered Feb 09 '23 15:02

VoidExplorer


may be you can find some good example thats use with imageview and buttons but the easy way i did it by using adding TextView instead of images and buttons.

<RelativeLayout
            android:id="@+id/layout1"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:clickable="true"
            android:gravity="right">

            <TextView
                android:id="@+id/englishheading1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/urduheading1"
                android:text="English text"
                android:textColor="#ffffff"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/urduheading1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:text="urdu text1"
                android:textColor="#ffffff"
                android:textSize="16sp" />

            <View
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:layout_alignParentBottom="true"
                android:layout_marginTop="8dp"
                android:background="#ffffff" />
        </RelativeLayout>

Here is java code to perform onClick.

RelativeLayout layout1 = (RelativeLayout) view.findViewById(R.id.layout1);
    layout1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // do something
        }
});
like image 35
Nowshad Avatar answered Feb 09 '23 15:02

Nowshad