Change Notification Layout
First create an xml for your notification.
custom_notification.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp" >
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
style="Custom Notification Title" />
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
style="Custom Notification Text" />
</RelativeLayout>
Now the java code:
public class MainActivity extends Activity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, "Custom Notification", when);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
notification.contentView = contentView;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_NO_CLEAR; //Do not clear the notification
notification.defaults |= Notification.DEFAULT_LIGHTS; // LED
notification.defaults |= Notification.DEFAULT_VIBRATE; //Vibration
notification.defaults |= Notification.DEFAULT_SOUND; // Sound
mNotificationManager.notify(1, notification);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Hope this thing works for you.
Edit: You can also visit if you come across problem like this.
Also, you can visit here for more information.
Edit April 26 2016 You can use NotificationCompat.Builder
for creating Notification
instance as below:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(icon)
.setContent(contentView)
.setContentTitle("Custom Notification")
.setWhen(when);
...
mNotificationManager.notify(1, notificationBuilder.build());
How to create a Custom Notification Layout in android?
I used BitTextStyle()
to add highlighted text in notification.
return new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mono)
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(icon)
.setColor(ContextCompat.getColor(context, R.color.notification_color))
.setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(message).setSummaryText("#hashtag"))
.setShowWhen(true)
.setAutoCancel(true);
Custom notification layouts and text colors
Solution by Malcolm works fine with API>=9. Here's the solution for older API:
The trick is to create the standard notification object and then traverse the default contentView
created by Notification.setLatestEventInfo(...)
. When you find the right TextView, just get the tv.getTextColors().getDefaultColor()
.
Here's the code that extracts the default text color and text size (in scaled density pixels - sp).
private Integer notification_text_color = null;
private float notification_text_size = 11;
private final String COLOR_SEARCH_RECURSE_TIP = "SOME_SAMPLE_TEXT";
private boolean recurseGroup(ViewGroup gp)
{
final int count = gp.getChildCount();
for (int i = 0; i < count; ++i)
{
if (gp.getChildAt(i) instanceof TextView)
{
final TextView text = (TextView) gp.getChildAt(i);
final String szText = text.getText().toString();
if (COLOR_SEARCH_RECURSE_TIP.equals(szText))
{
notification_text_color = text.getTextColors().getDefaultColor();
notification_text_size = text.getTextSize();
DisplayMetrics metrics = new DisplayMetrics();
WindowManager systemWM = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
systemWM.getDefaultDisplay().getMetrics(metrics);
notification_text_size /= metrics.scaledDensity;
return true;
}
}
else if (gp.getChildAt(i) instanceof ViewGroup)
return recurseGroup((ViewGroup) gp.getChildAt(i));
}
return false;
}
private void extractColors()
{
if (notification_text_color != null)
return;
try
{
Notification ntf = new Notification();
ntf.setLatestEventInfo(this, COLOR_SEARCH_RECURSE_TIP, "Utest", null);
LinearLayout group = new LinearLayout(this);
ViewGroup event = (ViewGroup) ntf.contentView.apply(this, group);
recurseGroup(event);
group.removeAllViews();
}
catch (Exception e)
{
notification_text_color = android.R.color.black;
}
}
Call extractColors
ie. in onCreate() of your service. Then when you're creating the custom notification, the color and text size you want are in notification_text_color
and notification_text_size
:
Notification notification = new Notification();
RemoteViews notification_view = new RemoteViews(getPackageName(), R.layout.notification);
notification_view.setTextColor(R.id.label, notification_text_color);
notification_view.setFloat(R.id.label, "setTextSize", notification_text_size);
How to set custom layout and font in Exoplayer notification?
As said on the repo you need to extends the PlayerNotificationManager
to do that you need to create this class and copy everything inside your project, and then add the layout you want to use as described in this link
You need to add your layout in the builder
here
Changing the default size of the notification (Android)
You can't change the height your notification gets. The height of a notification is set by the system and you need to fit your contents into that space. If you need more space, you'll have to set a "big" content view (setCustomBigContentView) and try to get it to be expanded (use PRIORITY_HIGH, but this is no guarantee).
I don't know what kind of phone you have, but it seems to be rendering your layout larger than my phone does. The same layout code on my phone fits just fine.
I tried to force the buttons larger and at 70dp the layout just forced the lower button to be smaller so that it would fit.
However, I have seen a LinearLayout with items that get lost because they extend past the bottom of the notification area, which seems more like what you are seeing on your phone.
Maybe you can explicitly set your font size/button height to ensure everything fits?
Related Topics
How to Change Spinner Text Size and Text Color
Show Image View from File Path
Viewpager.Setoffscreenpagelimit(0) Doesn't Work as Expected
How to Add a Jar in External Libraries in Android Studio
Android: Retrieving Shared Preferences of Other Application
Android.Content.Activitynotfoundexception:
How to Record a Video in My Android App
Android Pick Images from Gallery
Onitemclicklistener Not Working in Listview
How to Programmatically Add Views to Views
How to Get Uri from an Asset File
How to Check Grants Permissions At Run-Time
Can Not Find a View With Findviewbyid()