Bargain23
Bargain23

Reputation: 1973

Buttons on RelativeLayout cannot be clicked

I have a RelativeLayout saveCancelBar that contains two buttons also contained in RelativeLayouts. saveCancelBar will only be displayed if the EditText pageTitle is clicked or in focus. For some reason, nothing happens when I click any of the buttons, and the onClick events (displaying a Toast, hiding the soft keyboard, and hiding the EditText cursor) are not triggered when I click them.
enter image description here

The buttons I'm referring to are the "Cancel" and "Save Title" and saveCancelBar is the layout that encloses the two. I did make sure to set android:clickable="true". This is the XML code for the activity:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="missingPrefix"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context="app.travelers.activity.AddPhotoActivity">

    <LinearLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/back_arrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/arrow_left"
            android:textSize="20sp"
            android:clickable="true"
            android:textColor="@color/black" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_below="@id/toolbar"
        android:orientation="vertical">

        <EditText
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="36dp"
            android:background="@color/transparent"
            android:maxLength="64"
            android:privateImeOptions="nm"
            android:inputType="textNoSuggestions|textMultiLine"
            android:cursorVisible="false"
            app:autoSizeTextType="uniform"
            app:autoSizeMinTextSize="16sp"
            app:autoSizeMaxTextSize="24sp"
            app:autoSizeStepGranularity="4sp"
            android:textColor="@color/grey700"/>

        <View
            android:layout_width="100dp"
            android:layout_height="2dp"
            android:layout_marginTop="3dp"
            android:background="@color/grey700"
            />

        <LinearLayout
            android:id="@+id/sort"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/header"
            android:layout_marginTop="20dp"
            android:clickable="true"
            android:gravity="bottom"
            android:orientation="horizontal"
            android:visibility="gone">

            <TextView
                android:id="@+id/sort_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="3dp"
                android:text="List View"
                android:textColor="@color/grey700"
                android:textSize="14sp"/>

            <TextView
                android:id="@+id/sort_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/chevron_circle_down"
                android:textColor="@color/grey700"
                android:textSize="14sp"/>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/linearLayout">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/add_photo_layout"
            android:layout_marginTop="150dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                android:layout_centerInParent="true">

                <TextView
                    android:id="@+id/placeholder"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="40dp"
                    android:layout_marginEnd="40dp"
                    android:layout_marginBottom="30dp"
                    android:background="@null"
                    android:minLines="0"
                    android:text="Add your first travel moment to your story"
                    android:textAlignment="center"
                    android:textColor="@color/black"
                    android:textSize="20sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:gravity="center">

                    <LinearLayout
                        android:id="@+id/capture_photo_button"
                        android:layout_marginBottom="15dp"
                        android:paddingStart="5dp"
                        android:paddingEnd="5dp"
                        android:paddingTop="10dp"
                        android:paddingBottom="10dp"
                        android:layout_width="170dp"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:gravity="center|center_vertical"
                        android:background="@drawable/green_pill_thick">

                        <TextView
                            android:id="@+id/capture_photo_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textSize="18sp"
                            android:text="@string/camera"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:textColor="@color/green500"
                            android:layout_marginEnd="5dp"/>

                        <TextView
                            android:id="@+id/capture_photo_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Capture Moment"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:textColor="@color/green500"
                            android:textAlignment="center"
                            android:textSize="14sp"/>

                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/add_photo_button"
                        android:paddingTop="10dp"
                        android:paddingBottom="10dp"
                        android:paddingStart="5dp"
                        android:paddingEnd="5dp"
                        android:layout_width="170dp"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:gravity="center|center_vertical"
                        android:background="@drawable/blue_pill_thick">

                        <TextView
                            android:id="@+id/add_photo_icon"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textSize="18sp"
                            android:text="@string/photo"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:textColor="@color/blue500"
                            android:layout_marginEnd="5dp"/>

                        <TextView
                            android:id="@+id/add_photo_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Add from Gallery"
                            android:layout_gravity="center_horizontal|center_vertical"
                            android:textColor="@color/blue500"
                            android:textAlignment="center"
                            android:textSize="14sp"/>

                    </LinearLayout>

                </LinearLayout>

            </LinearLayout>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/list_view_layout"
            android:visibility="gone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="150dp"
            android:paddingBottom="50dp">

            <ListView
                android:id="@+id/list_view_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stackFromBottom="true"
                android:transcriptMode="alwaysScroll">

            </ListView>

        </RelativeLayout>

    </LinearLayout>


    <!--Navbar-->
    <LinearLayout
        android:id="@+id/photo_story_navbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@drawable/navbar"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="right"
            android:paddingStart="15dp"
            android:orientation="horizontal"
            android:layout_marginEnd="10dp">

            <!-- Take Photo Button -->
            <LinearLayout
                android:id="@+id/nav_capture_button"
                android:layout_width="84dp"
                android:layout_height="32dp"
                android:paddingStart="10dp"
                android:orientation="horizontal"
                android:background="@drawable/green_pill_button"
                android:layout_gravity="center_vertical"
                android:gravity="center|center_vertical"
                android:paddingEnd="10dp"
                android:clickable="true">

                <TextView
                    android:id="@+id/nav_capture_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:text="@string/camera"
                    android:textColor="@color/green500"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:paddingEnd="5dp"/>

                <TextView
                    android:id="@+id/nav_capture_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Capture"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:textColor="@color/green500"
                    android:textAlignment="center"
                    android:textSize="12sp"/>

            </LinearLayout>

            <!-- Add from Gallery Button -->
            <LinearLayout
                android:id="@+id/nav_gallery_button"
                android:layout_width="84dp"
                android:layout_height="32dp"
                android:paddingStart="10dp"
                android:layout_marginLeft="5dp"
                android:orientation="horizontal"
                android:background="@drawable/blue_pill"
                android:layout_gravity="center_vertical"
                android:gravity="center|center_vertical"
                android:paddingEnd="10dp"
                android:clickable="true">

                <TextView
                    android:id="@+id/nav_gallery_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:text="@string/photo"
                    android:textColor="@color/blue500"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:paddingEnd="5dp"/>

                <TextView
                    android:id="@+id/nav_gallery_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Gallery"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:textColor="@color/blue500"
                    android:textAlignment="center"
                    android:textSize="12sp"/>

            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:gravity="right"
            android:paddingEnd="15dp"
            android:orientation="horizontal">

            <!-- Review button -->
            <LinearLayout
                android:id="@+id/nav_review_button"
                android:layout_width="84dp"
                android:layout_height="32dp"
                android:paddingStart="10dp"
                android:orientation="horizontal"
                android:background="@drawable/red_500_pill"
                android:layout_gravity="right"
                android:gravity="center|center_vertical"
                android:paddingEnd="10dp"
                android:clickable="true">

                <TextView
                    android:id="@+id/nav_review_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Review"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:textColor="@color/red500"
                    android:textAlignment="center"
                    android:textSize="12sp"/>

                <TextView
                    android:id="@+id/nav_review_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="14sp"
                    android:text="@string/angle_double_right"
                    android:textColor="@color/red500"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:paddingStart="5dp"/>

            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

    <RelativeLayout
        android:id="@+id/save_cancel_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/linearLayout"
        android:layout_alignParentStart="true"
        android:background="@drawable/black_border_top">

        <RelativeLayout
            android:id="@+id/save_title_btn"
            android:clickable="true"
            android:layout_width="84dp"
            android:layout_height="32dp"
            android:layout_alignParentRight="true"
            android:background="@drawable/blue_pill"
            android:layout_marginRight="15dp"
            android:layout_marginTop="10dp">

            <TextView
                android:id="@+id/save_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:layout_gravity="fill"
                android:text="Save Title"
                android:textSize="12sp"
                android:textColor="#2196F3"/>

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/cancel_title_btn"
            android:layout_width="84dp"
            android:layout_height="32dp"
            android:clickable="true"
            android:layout_alignParentRight="true"
            android:background="@drawable/grey_700_pill"
            android:layout_marginRight="105dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp">

            <TextView
                android:id="@+id/cancel_title_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Cancel"
                android:layout_gravity="fill"
                android:textSize="12sp"
                android:textColor="#616161" />

        </RelativeLayout>

    </RelativeLayout>


</RelativeLayout>

To be specific, these are my onClick listeners:

saveButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if(!TextUtils.isEmpty(pageTitle.getText().toString())) {
            pageTitle.clearFocus();
            getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            saveCancelBar.clearAnimation();
            saveCancelBar.setVisibility(View.GONE);
            Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();

        } else {
            Toast.makeText(AddPhotoActivity.this, "Title is too short.", Toast.LENGTH_SHORT).show();
        }
    }

});

cancelButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        pageTitle.clearFocus();
        getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        saveCancelBar.clearAnimation();
        saveCancelBar.setVisibility(View.GONE);
    }
});

EDIT: I modified the if condition for saveButton's onClickListener and added the .show() method to the toasts as per suggestions by @Nilesh and @Exigente05. I also added pageTitle.clearFocus(). The updates to the code managed to display the toasts and hide the EditText cursor, but saveCancelBar and the soft keyboard are still present.

Upvotes: 0

Views: 181

Answers (3)

Exigente05
Exigente05

Reputation: 2211

Most funny findings I am going to answer.

You know what?

Everything is OK only you need to add .show() in the end of that two particular Toast.makeText. See carefully, in line number 220 and 223 you forgot to call .show()

enter image description here

Is not it funny?

Another thing pageTitle.getText() != null will always be true. You need to check text length or emptyText to get accurate result.

To hide keyboard and clear focus change code inside saveButton.setOnClickListener

    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(pageTitle.getText() != null) {
//              pageTitle.clearFocus();
                View view = ClassName.this.getCurrentFocus();
                if (view != null) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                pageTitle.clearFocus();
                saveCancelBar.clearAnimation();
                saveCancelBar.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(), "Title is too short.", Toast.LENGTH_SHORT).show();
            }
        }

    });

Also put same mechanism inside cancelButton.setOnClickListener

Upvotes: 0

AskNilesh
AskNilesh

Reputation: 69699

use this

if(!TextUtils.isEmpty(pageTitle.getText().toString())) {
       getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
       saveCancelBar.setVisibility(View.GONE);
        Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT).show();

 } else {
        Toast.makeText(AddPhotoActivity.this, "Title is too Short.", Toast.LENGTH_SHORT).show();
 }

instead of this

if(pageTitle.getText() != null) {
       getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_HIDDEN);
       saveCancelBar.setVisibility(View.GONE);
       Toast.makeText(AddPhotoActivity.this, "Title is successfully saved", Toast.LENGTH_SHORT);

} else {
       Toast.makeText(AddPhotoActivity.this, "Title is too short.", Toast.LENGTH_SHORT);
 }

Upvotes: 1

Kapil
Kapil

Reputation: 11

if(pageTitle.getText() != null) 

Have you checked your this condition?

Upvotes: 0

Related Questions