-1

I have a TextView inside a CardView. I want some functionality to happen on click of the Textview but my android:onClick is not responding to the click as the method is not getting called. What is the issue? Help me Please.

Here is my code:

XML

 <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp">
                <LinearLayout

                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Application info"
                        android:textAlignment="center"
                        android:layout_gravity="left"
                        android:layout_margin="10dp"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ApplicationHide"
                    android:layout_width="match_parent"

                    android:layout_height="match_parent"
                    android:layout_margin="30dp"
                    android:orientation="vertical"
                    android:weightSum="11">


                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                    <EditText
                        android:focusable="false"
                        android:inputType="none"
                        android:textIsSelectable="true"
                        android:maxLines="1"
                        android:singleLine="true"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/occupation"
                        android:hint="Occupation" />
                    </android.support.design.widget.TextInputLayout>
                    <LinearLayout
                        android:id="@+id/occupiSalaryHide"
                        android:layout_width="match_parent"
                        android:visibility="gone"
                        android:orientation="vertical"
                        android:layout_height="wrap_content">
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/companyName"
                            android:layout_weight="1"
                            android:hint="Company name" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/monthlyIncome"
                            android:layout_weight="1"
                            android:hint="Gross monthly income" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/modeSalary"
                            android:layout_weight="1"
                            android:hint="Mode of salary" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/typeCompany"
                            android:layout_weight="1"
                            android:hint="Type of Company*" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/professionType"
                            android:layout_weight="1"
                            android:hint="Profession Type" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/designation"
                            android:layout_weight="1"
                            android:hint="Designation" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/noYearCurrWork"
                            android:layout_weight="1"
                            android:hint="Number of Years in Current Work" />
                        </android.support.design.widget.TextInputLayout>
                        <android.support.design.widget.TextInputLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">

                        <AutoCompleteTextView
                            android:focusable="false"
                            android:inputType="none"
                            android:textIsSelectable="true"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:id="@+id/noOfYears"
                            android:layout_weight="1"
                            android:hint="Total no. of years in work" />
                        </android.support.design.widget.TextInputLayout>

                    </LinearLayout>


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp"
                        android:gravity="center">

                        <TextView
                            android:id="@+id/applicationContinu"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center"
                            android:background="@drawable/button_border"
                            android:gravity="center"
                            android:clickable="true"
                            android:onClick="applicationValid"
                            android:padding="10dp"
                            android:text="Continue"
                            android:textColor="#fff" />
                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>

I have two more such above this but in that, the onClick() is working but not working in this. In this setOnClickListener() is working but I want to use android:onClick?

Have I done anything wrong??

1 Answers1

0

to set click on any view you can follow this.

Assume you have textview

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Application info"/>

You can set click by your java code like

public void setClick(){
    TextView textView = (TextView) findViewById(R.id.tv);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      //                ... your code for click
        }
    });
}

If you are using an activity then you can call your method by xml like

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Application info"
    android:onClick="yourMethodName..."/>

Keep in mind that xml way of click works only for activities.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212