Android - Checkbox not responding to clicks inside of FrameLayout

I'm working on a music player application, and I've created a FrameLayout which works as a "card," displaying information about each song (album art, title, artist, etc.) I'm trying to add checkboxes in this card, which I plan to customize to create upvote and downvote buttons. However, the checkboxes within the card do not respond to clicks. If I draw a checkbox in the main activity of the application (where I'm drawing the cards), it works fine.

Here is the onDraw() method of the card view ("MusicCardView"):

    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);


    //Draw the voting controls
    try {

        CheckBox upvoteButton = new CheckBox(getContext());
        upvoteButton.setX(20);
        upvoteButton.setTop(-60);
        upvoteButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Is the button now checked?
                Log.d("MainActivity", "upvote clicked");
            }
        });
        CheckBox downvoteButton = new CheckBox(getContext());
        downvoteButton.setX(20);
        downvoteButton.setTop(-150);
        downvoteButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // Is the button now checked?
                Log.d("MainActivity", "downvote clicked");
            }
        });

        upvoteButton.draw(canvas);
        downvoteButton.draw(canvas);

    } catch (Exception e) {
        Log.e("MusicCardView", "exception", e );
    }

}

The xml of the main activity ("PartyActivity"):

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:id="@+id/main_activity_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.musique.PartyActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/main_linear_view"
        android:layout_marginBottom="56dp">
        <com.musique.MusicCardView
            android:layout_width="match_parent"
            android:id="@+id/now_playing_view"
            android:layout_height="80dp"
            android:background="#ccc"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            app:exampleColor="#000000"
            app:songTitle="Money"
            app:artist="Pink Floyd"
            app:album="The Dark Side of the Moon"
            app:exampleDimension="18sp"
            app:exampleDrawable="@drawable/darkside"
            app:exampleString="example" />
        <Space
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:id="@+id/controller_space"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/query"
            android:layout_gravity="center"
            android:layout_marginTop="20dp" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchBtn"
            android:layout_gravity="center"
            android:layout_marginTop="20dp"
            android:text="Search"
            android:background="#5eca99"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvData"/>
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:animateLayoutChanges="true"
            android:id="@+id/main_scroll_view">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:id="@+id/scroll_child">

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



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />


</android.support.constraint.ConstraintLayout>

And an example of a MusicCardView being the draw in PartyActivity

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MusicCardView v = (MusicCardView) vi.inflate(R.layout.music_card_template, null);
v.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 210));
LinearLayout linearLayout = findViewById(R.id.scroll_child);
v.setSongAttributes(song);
new RetrieveArtTask(v).execute(song);
Log.d("PartyActivity", "SongAttributes Set");
linearLayout.addView(v);    

I've had a look at this question and this question, but had no luck with the solutions posted, and would greatly appreciate any advice you might have.

1 answer

  • answered 2018-04-17 05:13 patrick-iv

    If you draw the controls directly to the canvas they are basically just bitmaps, you need to add them to the view hierarchy (activity/fragment layout) for them to receive touch events.

    As a side note, it's a big no-no to instantiate things in onDraw, especially views.