Paghillect
Paghillect

Reputation: 848

Android Button receives OnTouch event but not OnClick

I have the following layout in which I'm trying to register an OnClickListener for the button list_item_setup_step_button_start. the Button receives touch events but no click events. any help is appreciated.

<RelativeLayout
    android:id="@+id/list_item_setup_step_layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/list_item_setup_step_label"
        android:layout_width="@dimen/list_item_setup_state_label_width"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@color/activity_setup_step_state_next_color"
        android:clickable="false"/>

    <FrameLayout
        android:id="@+id/list_item_setup_step_frame_layout_progress"
        android:layout_width="@dimen/list_item_setup_step_progress_bar_size"
        android:layout_height="match_parent">

        <com.mikhaellopez.circularprogressbar.CircularProgressBar
            android:id="@+id/list_item_setup_step_progress_bar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            app:cpb_background_progressbar_color="#FFCDD2"
            app:cpb_progressbar_color="#F44336"
            android:visibility="invisible"
            android:layout_gravity="center_vertical|center_horizontal"/>

        <Button
            android:id="@+id/list_item_setup_step_button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:padding="@dimen/text_margin_normal"
            android:background="@color/color_accent_default"
            android:textColor="@color/text_color_light"
            android:visibility="invisible"
            android:clickable="true"
            android:textSize="@dimen/text_size_normal"
            android:layout_marginLeft="@dimen/text_margin_normal"/>

    </FrameLayout>

    <LinearLayout
        android:layout_toLeftOf="@id/list_item_setup_step_label"
        android:layout_toRightOf="@id/list_item_setup_step_frame_layout_progress"
        android:layout_marginRight="@dimen/text_margin_normal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/list_item_setup_step_text_view_title"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="16sp"
            android:gravity="right|center_vertical"
            android:textColor="@color/text_color_light"/>

        <TextView
            android:id="@+id/list_item_setup_step_text_view_status"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:textColor="@color/text_color_light" />

    </LinearLayout>

</RelativeLayout>

this is the code for the listener (I have a custom ScrollView which ignores touch events so that it can only be scrolled programmatically), the 'Clicked' log never appears and the functions aren't called.

private void handleStartButton() {
    currentStepStartButton = (Button) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_button_start);
    currentStepStartButton.setTypeface(BaseActivity.getFont(this));
    final TextView status = (TextView) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_text_view_status);
    progressBar = (CircularProgressBar) stepViews[currentPhase][currentStep].findViewById(R.id.list_item_setup_step_progress_bar) ;
    progressBar.setProgress(0);
    StyleHelper.applyStyle(this, progressBar);
    status.setTypeface(BaseActivity.getFont(this));
    if(currentStep == 0 && currentPhase == 0) {
        status.setText(getString(R.string.activity_setup_press_button_to_begin));
        currentStepStartButton.setText(getString(R.string.commons_start));
    } else {
        status.setText(getString(R.string.activity_setup_press_button_to_continue));
        currentStepStartButton.setText(getString(R.string.commons_start));
    }
    currentStepStartButton.setVisibility(View.VISIBLE);
    currentStepStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.e(Constants.DEBUG_TAG, "Clicked") ;
            currentStepStartButton.setVisibility(View.INVISIBLE);
            handleCurrentStepAction();
        }
    });
}

EDIT:

The following method was being called before the method which set the listener:

private void requestPermissions() {
        int currentAPIVersion = android.os.Build.VERSION.SDK_INT ;
        if (currentAPIVersion >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    android.Manifest.permission.SEND_SMS,
                    android.Manifest.permission.RECEIVE_SMS
            }, 1);
        }
    }

Once this method was removed, the OnClickListener started working again. I don't really have any idea why. Any theories ?

Upvotes: 1

Views: 306

Answers (2)

Paghillect
Paghillect

Reputation: 848

Well I faced the same problem while working on another project and figured what was really wrong here.

The problem, as suggested in the question itself, rises from the use of requestPermissions method. Although calling this method in case the permission its trying to acquire is already granted might seem to not show a dialog over the current activity, some invisible overlay seems to be added anyway which also appears to be clickable.

The solution hence is to always check if one already has the permissions one is trying to acquire and only fire up requestPermissions in case those permissions are not granted yet.

Upvotes: 0

System developer
System developer

Reputation: 36

There seem to be some interference from your other code (which is not visible in this question) since the button click method works if the code you provided, except for those parts which are not visible, is inserted into a new project.

Java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_2);

    final Button currentStepStartButton = (Button) findViewById(R.id.list_item_setup_step_button_start);
    currentStepStartButton.setVisibility(View.VISIBLE);
    currentStepStartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("MainActivity", "Clicked");
            currentStepStartButton.setVisibility(View.INVISIBLE);
        }
    });
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:id="@+id/list_item_setup_step_layout_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/list_item_setup_step_frame_layout_progress"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/list_item_setup_step_button_start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:visibility="invisible"
            android:clickable="true"/>

    </FrameLayout>

</RelativeLayout>
</LinearLayout>

Try to temporarily disable unnecessary features for the button to work to zero in on the issue.

Upvotes: 1

Related Questions