Dinh Quang Tuan
Dinh Quang Tuan

Reputation: 574

Android default notification margin of Xiaomi Phone

I am using a Custom Notification and finding a way to set its margin/padding based on the phone's default value.

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

I found some answers here: Android default notification margin

I tested on Virtual Devices and some phone models ( Samsung, Oppo, Pixel) , the method seems to work well except for Xiaomi Phone, the padding is very big, maybe 5-6 times bigger than other phones.I think there is a problem with the setViewPadding method since the value int padding = context.getResources().getDimensionPixelSize(identifier); is not so much different from other phones. Please let me know if you have any idea and where can I find the android dimen xml file in system folder?

 RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.my_notification_content_layout);
try {
    // We are assuming start and end are same. If we want to be nitty, we will get them separately, and then check which to use for left and which for righht.
    int identifier = context.getResources().getIdentifier("notification_content_margin_start", "dimen", "android");
    if (identifier != 0) {
        int padding = context.getResources().getDimensionPixelSize(identifier);
        contentView.setViewPadding(R.id.notify_layout, padding, 0, padding, 0);
        Log.d("setViewPadding", "Setting padding to: " + padding);
    } else {
        Log.d("setViewPadding", "Failed to find padding");
    }
} catch (Exception e) {
    Log.d("setViewPadding", "Failed to set padding");
}

Upvotes: 0

Views: 863

Answers (1)

alex.vin
alex.vin

Reputation: 36

I have exactly same issue and was hoping someone would answer. Meanwhile, though not a direct solution, the following may nevertheless help.

Instead of using a fully customized layout, define DecoratedCustomView as follows:

builder.setStyle(new NotificationCompat.DecoratedCustomViewStyle());

What this does is it keeps system-default header of the notification, including app icon, app name, and default margins (Tested on MIUI 12). You can still set your custom layout as you would normally do:

builder.setCustomContentView(notificationLayout);

The advantage of this method is that also gets a 1:1 system header (even on MIUI), while keeping custom main layout, which is hard to do with fully custom layout. It also eliminates need to check default notification padding. Further things to keep in mind:

  1. This works only for API24+. Below API24, old notification style was used (big full height icon) so this will compile, but won't work like you would expect it to.
  2. Make sure you remove all padding from your layout, as well as custom set app icon and app title, as these will be defined for you.
  3. Although this way you cannot change notification title (app name), it may be very handy to add subtitle as follows: builder.setSubText("Secondary Title"); See example in this post

Example how the transformation looks like.

This is fully customized layout (bare in mind, displays perfectly on literally any device, but MIUI).

Screenshot

Here, DecoratedCustomView is used. You can see that the header is perfectly aligned (hotspot notification for comparison), and the only difference is that app name isn't colored anymore.

Screenshot

Upvotes: 2

Related Questions