Search code examples
androidnotificationspositiontoast

Changing position of a notification


I have two apps on different devices - Rider & Driver. I am sending a notification from the Driver app to the Rider app once the driver accepts the request:

private void acceptBooking(String customerId) {

    Token token = new Token(customerId);

    Map<String, String> content = new HashMap<>();
    content.put("title", "Accept");
    content.put("message", "Your request is accepted, Please make your payment!");


    DataMessage dataMessage = new DataMessage(token.getToken(), content);

    mFCMService.sendMessage(dataMessage)
        .enqueue(new Callback<FCMResponse>() {
            @Override
            public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                if (response.body().success == 1) {
                    Toast.makeText(CustomerCall.this, "Ride Accepted",
                            Toast.LENGTH_SHORT).show();
                    finish();
                }
            }

            @Override
            public void onFailure(Call<FCMResponse> call, Throwable t) {}
        });
}

Once acceptBooking() is run, the Driver receives a toast "Ride Accepted" and the Rider receives a toast "Your request is accepted, Please make your payment!"

What I need to do is position the toast in the Rider App in the mid centre of the screen, change the colour and the background colour.

I have tried the following suggestion, but doesn't seem to work with this type of notification.


Solution

  • Try this. Add an xml file like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/custom_toast_container"
    android:background="@drawable/complete_round_light_green_fill">
    
    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginHorizontal="24dp"
        android:layout_marginVertical="15dp"
        android:layout_gravity="center_horizontal"
        android:fontFamily="@font/ubuntu"
        android:textSize="14sp"
        android:lineSpacingExtra="7sp"
        android:textColor="@color/white"
        tools:text="abcd"
        />
    
    </LinearLayout>
    

    Add this in the Java Code. As you can see, we can add the background, set gravity to the toast:

    LayoutInflater inflater = LayoutInflater.from(this);
        View layout = inflater.inflate(R.layout.layout_toast_green, null);
    
        LinearLayout customContainer = (LinearLayout) layout.findViewById(R.id.custom_toast_container);
        customContainer.setBackgroundResource(R.drawable.complete_round_mgred_fill);
        TextView text = (TextView) layout.findViewById(R.id.message);
        text.setText("Your request is accepted, Please make your payment!");
    
        Toast toast = new Toast(AppController.getContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.setView(layout);
        toast.show();
    

    Xml code for complete_round_light_green_fill:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#78cd96" />
            <corners android:radius="100dip"/>
        </shape>
    
    </item>
    
    </layer-list>