Abhishek Charismatic
Abhishek Charismatic

Reputation: 366

Tried everything but relative layout click is not working at all

This is my layout where I want select_back relative layout to have click listener. But It's not working at all. I could not figure out the issue.

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
android:background="@color/white">

<RelativeLayout
    android:id="@+id/select_back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="40dp"
    android:layout_marginTop="20dp"
    android:background="@color/white"
    android:clickable="true"
    android:focusable="true">


    <androidx.appcompat.widget.AppCompatTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:drawableStart="@drawable/left_arrow"
        android:drawablePadding="10dp"
        android:focusable="false"
        android:fontFamily="sans-serif"
        android:text="@string/village_text"
        android:textColor="@color/colorPrimaryDark"
        android:textSize="15sp"
        android:textStyle="normal">


    </androidx.appcompat.widget.AppCompatTextView>

    </RelativeLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="20dp"
    android:fillViewport="false">


    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="30dp"
        android:layout_marginRight="20dp"
        android:orientation="vertical"
        android:paddingBottom="20dp">


        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="50dp"
            android:src="@drawable/village_logo" />


        <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="40dp"
            android:fontFamily="sans-serif"
            android:text="@string/village_title"
            android:textColor="@color/title_colr"
            android:textSize="20sp"
            android:textStyle="bold" />


        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:backgroundTint="@color/edittext_tint"
            android:focusable="true"
            android:fontFamily="sans-serif"
            android:gravity="start"
            android:hint="@string/select_state"
            android:inputType="text"
            android:padding="20dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/textview_black"
            android:textColorHint="@color/textview_black"
            android:textCursorDrawable="@null"
            android:textSize="14sp"
            android:textStyle="normal" />


        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/district"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/edittext_tint"
            android:fontFamily="sans-serif"
            android:gravity="start"
            android:hint="@string/select_district"
            android:inputType="text"
            android:padding="20dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/textview_black"
            android:textColorHint="@color/textview_black"
            android:textCursorDrawable="@null"
            android:textSize="14sp"
            android:textStyle="normal" />


        <androidx.appcompat.widget.AppCompatEditText
            android:id="@+id/block"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/edittext_tint"
            android:fontFamily="sans-serif"
            android:gravity="start"
            android:hint="@string/select_block"
            android:inputType="text"
            android:padding="20dp"
            android:paddingStart="10dp"
            android:paddingEnd="10dp"
            android:textColor="@color/textview_black"
            android:textColorHint="@color/textview_black"
            android:textCursorDrawable="@null"
            android:textSize="14sp"
            android:textStyle="normal" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/village"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/edittext_tint"
                android:fontFamily="sans-serif"
                android:hint="@string/select_village"
                android:inputType="text"
                android:padding="20dp"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:textColor="@color/textview_black"
                android:textColorHint="@color/textview_black"
                android:textCursorDrawable="@null"
                android:textSize="14sp"
                android:textStyle="normal">

                <requestFocus />
            </androidx.appcompat.widget.AppCompatEditText>

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/speech"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:layout_marginRight="20dp"
                android:adjustViewBounds="true"
                android:clickable="true"
                android:foreground="?attr/selectableItemBackground"
                android:paddingBottom="5dp"
                android:src="@drawable/mic" />


        </RelativeLayout>


        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/next"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="50dp"
            android:background="@drawable/button_background"
            android:fontFamily="sans-serif"
            android:foreground="?attr/selectableItemBackground"
            android:padding="10dp"
            android:text="Next"
            android:textColor="@color/white"
            android:textSize="17sp"
            android:textStyle="normal" />


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="30dp"
                android:layout_marginBottom="10dp"
                android:clickable="true"
                android:focusable="true"
                android:fontFamily="sans-serif"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="bottom"
                android:text="@string/already_account"
                android:textColor="@color/textview_black"
                android:textSize="14sp"
                android:textStyle="normal" />

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="30dp"
                android:layout_marginBottom="10dp"
                android:clickable="true"
                android:focusable="true"
                android:fontFamily="sans-serif"
                android:foreground="?attr/selectableItemBackground"
                android:gravity="bottom"
                android:text="@string/login_text"
                android:textColor="@color/colorPrimaryDark"
                android:textSize="14sp"
                android:textStyle="bold" />


        </LinearLayout>


    </LinearLayout>
      </ScrollView>
    </RelativeLayout>

This is my Onclicklistener

 private RelativeLayout select_back;
 select_back = findViewById(R.id.select_back);



 select_back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(RegisterActivity.this, 
  LanuageActivity.class);
            startActivity(intent);
            overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        }
    });

I am trying to figure out what is wrong with it but not getting any clue. Could you please help me in figuring out the issue? this is my whole layout part.

Upvotes: 0

Views: 378

Answers (3)

Abhishek Charismatic
Abhishek Charismatic

Reputation: 366

So I came to know that my scroll view layout was overlapping my Relative Layout. I put this Relative Layout inside Scrollview and now it's working like a charm.

Upvotes: 0

Ankit
Ankit

Reputation: 1068

Your scrollview overlap the relativelayout so you cannot get click event, just change the sequence and it will works.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white">

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:fillViewport="false">


    <LinearLayout
            android:id="@+id/linear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:layout_marginTop="30dp"
            android:layout_marginRight="20dp"
            android:orientation="vertical"
            android:paddingBottom="20dp">


        <androidx.appcompat.widget.AppCompatImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="50dp"
                android:src="@drawable/village_logo" />


        <androidx.appcompat.widget.AppCompatTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:fontFamily="sans-serif"
                android:text="@string/village_title"
                android:textColor="@color/title_colr"
                android:textSize="20sp"
                android:textStyle="bold" />


        <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/state"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:backgroundTint="@color/edittext_tint"
                android:focusable="true"
                android:fontFamily="sans-serif"
                android:gravity="start"
                android:hint="@string/select_state"
                android:inputType="text"
                android:padding="20dp"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:textColor="@color/textview_black"
                android:textColorHint="@color/textview_black"
                android:textCursorDrawable="@null"
                android:textSize="14sp"
                android:textStyle="normal" />


        <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/district"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/edittext_tint"
                android:fontFamily="sans-serif"
                android:gravity="start"
                android:hint="@string/select_district"
                android:inputType="text"
                android:padding="20dp"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:textColor="@color/textview_black"
                android:textColorHint="@color/textview_black"
                android:textCursorDrawable="@null"
                android:textSize="14sp"
                android:textStyle="normal" />


        <androidx.appcompat.widget.AppCompatEditText
                android:id="@+id/block"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:backgroundTint="@color/edittext_tint"
                android:fontFamily="sans-serif"
                android:gravity="start"
                android:hint="@string/select_block"
                android:inputType="text"
                android:padding="20dp"
                android:paddingStart="10dp"
                android:paddingEnd="10dp"
                android:textColor="@color/textview_black"
                android:textColorHint="@color/textview_black"
                android:textCursorDrawable="@null"
                android:textSize="14sp"
                android:textStyle="normal" />

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatEditText
                    android:id="@+id/village"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/edittext_tint"
                    android:fontFamily="sans-serif"
                    android:hint="@string/select_village"
                    android:inputType="text"
                    android:padding="20dp"
                    android:paddingStart="10dp"
                    android:paddingEnd="10dp"
                    android:textColor="@color/textview_black"
                    android:textColorHint="@color/textview_black"
                    android:textCursorDrawable="@null"
                    android:textSize="14sp"
                    android:textStyle="normal">

                <requestFocus />
            </androidx.appcompat.widget.AppCompatEditText>

            <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/speech"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="20dp"
                    android:adjustViewBounds="true"
                    android:clickable="true"
                    android:foreground="?attr/selectableItemBackground"
                    android:paddingBottom="5dp"
                    android:src="@drawable/mic" />


        </RelativeLayout>


        <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/next"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="50dp"
                android:background="@drawable/button_background"
                android:fontFamily="sans-serif"
                android:foreground="?attr/selectableItemBackground"
                android:padding="10dp"
                android:text="Next"
                android:textColor="@color/white"
                android:textSize="17sp"
                android:textStyle="normal" />


        <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="horizontal">

            <androidx.appcompat.widget.AppCompatTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginTop="30dp"
                    android:layout_marginBottom="10dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:fontFamily="sans-serif"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="bottom"
                    android:text="@string/already_account"
                    android:textColor="@color/textview_black"
                    android:textSize="14sp"
                    android:textStyle="normal" />

            <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/login"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="30dp"
                    android:layout_marginBottom="10dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:fontFamily="sans-serif"
                    android:foreground="?attr/selectableItemBackground"
                    android:gravity="bottom"
                    android:text="@string/login_text"
                    android:textColor="@color/colorPrimaryDark"
                    android:textSize="14sp"
                    android:textStyle="bold" />


        </LinearLayout>


    </LinearLayout>
</ScrollView>
<RelativeLayout
        android:id="@+id/select_back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="40dp"
        android:layout_marginTop="20dp"
        android:background="@color/white"
        android:clickable="true"
        android:focusable="true">


    <androidx.appcompat.widget.AppCompatTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="false"
            android:drawableStart="@drawable/left_arrow"
            android:drawablePadding="10dp"
            android:focusable="false"
            android:fontFamily="sans-serif"
            android:text="@string/village_text"
            android:textColor="@color/colorPrimaryDark"
            android:textSize="15sp"
            android:textStyle="normal">


    </androidx.appcompat.widget.AppCompatTextView>

</RelativeLayout>

Upvotes: 1

Jullian Arnold
Jullian Arnold

Reputation: 53

Thats not a good thing to assign setOnClickListener into a Parent class in xml, but if you want to make it work you need to declare the private RelativeLayout select_back; as global place it on the top of protected void onCreate(Bundle savedInstanceState)

Upvotes: 0

Related Questions