Search code examples
androidapiuber-api

Uber RequestButton onClickListener


I am using Uber android-api in my project. On click of RequestButton it navigates the user to Uber App but I want to process some data before it navigates to Uber app. So for that, I want onClickListener of RequestButton.

I have already tried giving onClickListener directly on the object of requestButton then I get the event but it does not navigate to Uber App.


requestButton.setOnClickListener(new View.OnClickListener() 
{
      @Override 
       public void onClick(View v) {

       }
});              

Solution

  • To be able to call your own custom code before the RequestButton is clicked, i.e. it's OnClickListener callback method is called, you have to change the code of the RequestButton inside the rides-android-sdk.

    Then in your activity you have to call the setCustomPreOnClickListener on the RequestButton object:

    public void rideRequestButtonWithCustomPreOnClickListener() {
            RequestButton uberButtonBlack = (RequestButton) findViewById(R.id.uber_button_black);
            RequestButton uberButtonWhite = (RequestButton) findViewById(R.id.uber_button_white);
    
            RideParameters rideParameters = new RideParameters.Builder()
                    .setProductId(UBERX_PRODUCT_ID)
                    .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
                    .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
                    .build();
    
            uberButtonBlack.setRideParameters(rideParameters);
            uberButtonWhite.setRideParameters(rideParameters);
    
            uberButtonBlack.setCustomPreOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Custom code for black button called before UberRequestButton's onClick() listener");
                }
            });
    
            uberButtonWhite.setCustomPreOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.d(TAG, "Custom code for white button called before UberRequestButton's onClick() listener");
                }
            });
        }
    

    Another approach is to create a custom class:

    package com.uber.sdk.android.rides.samples;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.View;
    
    import com.uber.sdk.android.rides.RequestButton;
    
    public class CustomUberRequestButton extends RequestButton implements View.OnClickListener {
        private static final String TAG = CustomUberRequestButton.class.getSimpleName();
    
        public CustomUberRequestButton(Context context) {
            super(context);
        }
    
        public CustomUberRequestButton(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public CustomUberRequestButton(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void onClick(View v) {
            Log.d(TAG, "Custom code before calling UberRequestButton's onClick() listener");
    
            // Explicitly need to call UberRequestButton onClick() listener
            super.onClick(v);
        }
    }
    

    and you use it like this:

    /**
     * Please make sure you replace com.uber.sdk.android.rides.RequestButton
     * with com.uber.sdk.android.rides.samples.CustomUberRequestButton inside
     * the res/layout/activity_sample.xml file, otherwise you will get a ClassCastException.
     */
    public void rideRequestButtonWithCustomSubclassOnClickListener() {
        CustomUberRequestButton uberButtonBlack = (CustomUberRequestButton) findViewById(R.id.uber_button_black);
        CustomUberRequestButton uberButtonWhite = (CustomUberRequestButton) findViewById(R.id.uber_button_white);
    
        RideParameters rideParameters = new RideParameters.Builder()
                .setProductId(UBERX_PRODUCT_ID)
                .setPickupLocation(PICKUP_LAT, PICKUP_LONG, PICKUP_NICK, PICKUP_ADDR)
                .setDropoffLocation(DROPOFF_LAT, DROPOFF_LONG, DROPOFF_NICK, DROPOFF_ADDR)
                .build();
    
        uberButtonBlack.setRideParameters(rideParameters);
        uberButtonWhite.setRideParameters(rideParameters);
    }
    

    With the custom class you can also set a custom preOnClickListener if you want to. See the rideRequestButtonWithCustomSubclassAndPreOnClickListener method in the above commits.