RatingBar onClick

Viewed 28404

I have a ListView that uses different XML files to create Views and make items out of. One of these XML files contains a RatingBar. Everything displays and looks excellent.

I'm trying to attach an onClick handler to the RatingBar to launch a new Activity. My RatingBar is of style ?android:attr/ratingBarStyleSmall; so it's just an indicator (I want the small RatingBar click to take the user to an Activity where they can do various ratings).

My problem is the onClick handler for the RatingBar never gets executed. What makes it more interesting is that I've used the same code to make a LinearLayout clickable and it works fine. Could anyone tell me why?

My Adapter's getView looks as such:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    int type = getItemViewType(position);

    // get the View for this list item
    View v = convertView;
    if (v == null) {
        LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        switch (type) {
            // ...
            case TYPE_LOOKUP:
                v = vi.inflate(R.layout.layout_itemlist_itemlookup, parent, false);
                LinearLayout vLookup = (LinearLayout)v.findViewById(R.id.itemlist_lookup);
                if (vStore != null) {
                    vStore.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER WORKS FINE
                                Intent intentLaunchLookup = new Intent(ActivityItemList.this, ActivityLookup.class);
                                startActivity(intentLaunchLookup);
                            }
                        });
                }
                break;
            case TYPE_SEPARATOR:
                v = vi.inflate(R.layout.layout_itemlist_itemseparator, parent, false);
                RatingBar r = (RatingBar)v.findViewById(R.id.itemlist_rating);
                if (r != null) {
                    r.setOnClickListener(new View.OnClickListener() {
                            public void onClick(View v) {
                                // THIS HANDLER DOES NOT GET EXECUTED (r IS NOT NULL; SO THIS SHOULD HAVE BEEN CREATED)
                                Intent intentLaunchRating = new Intent(ActivityItemList.this, ActivityRating.class);
                                startActivity(intentLaunchRating);
                            }
                        });
                }
                break;
            // ...
        }
    }
    // …

  // return the created view
    return v;
}
6 Answers

This will listen to click on rating and won't do any effect on rating change as well

<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.AppCompatRatingBar
        android:id="@+id/rating_bar_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_16"
        android:maxHeight="@dimen/dp_23"
        android:minHeight="@dimen/dp_23"
        android:progressDrawable="@drawable/custom_rating_selector"
        android:stepSize="0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_find_provider_subheader" />


    <TextView
        android:id="@+id/tv_current_rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        app:layout_constraintBottom_toBottomOf="@+id/rating_bar_current_rating"
        app:layout_constraintEnd_toEndOf="@+id/tv_current_rating"
        app:layout_constraintStart_toStartOf="@+id/rating_bar_current_rating"
        app:layout_constraintTop_toTopOf="@+id/rating_bar_current_rating" />

</androidx.constraintlayout.widget.ConstraintLayout>

Just add a textview layout on top of ratingbar and listen to click listener of textview layout. Both views inside of Constraint layout.

in java code

Textview textview = findViewById(R.id.tv_current_rating)
textview.setOnClickListener(this)

@override
public void onClick(View v) {
  while (v.id) {
   case R.id.tv_current_rating :
   // TODO : This will show toast on click event 
   Toast.makeText(getApplicationContext(),
               "This a toast message",
               Toast.LENGTH_LONG)
     .show();
   break;
  }
}
Related