How to Show Text in Android System Status Bar

How do I show text in android system status bar

I do found a solution, the keyword is overlay with a floating window.

int statusBarHeight = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) statusBarHeight = getResources().getDimensionPixelSize(resourceId);

final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
statusBarHeight,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, // Allows the view to be on top of the StatusBar
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, // Keeps the button presses from going to the background window and Draws over status bar
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.TOP | Gravity.CENTER;

LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.TRANSPARENT);
LinearLayout.LayoutParams layoutParameteres = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
ll.setLayoutParams(layoutParameteres);

TextView tv = new TextView(this);
ViewGroup.LayoutParams tvParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
tv.setLayoutParams(tvParameters);
tv.setTextColor(Color.WHITE);
tv.setGravity(Gravity.CENTER);
tv.setText("123");
ll.addView(tv);

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
windowManager.addView(ll, parameters);

Display Notification Text in Status Bar - Android

I need to add .setTicker() to the second code snippet to have text display in the Android device status bar

How to show text in the status bar?

Use setNumber() with your Notification.Builder or NotificationCompat.Builder.

Android: Adding text from textView to status bar

Yes, you can

private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_status;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name); // Here you can pass the value of your TextView
Intent notificationIntent = new Intent(context, SplashScreen.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}

How to set text on status bar?

You need to use a Status Bar Notification.

//Create the Notification
Notification notification = new Notification(
android.R.drawable.stat_sys_warning, //Icon to use
"Hello World!", //Text
System.currentTimeMillis() //When to display - i.e. now
);

//Create a PendingIntent to do something when the user clicks on the Notification
//Normally this would be something in your own app
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.stackoverflow.com"));
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

//Add the detail to the Notification
notification.setLatestEventInfo(
getApplicationContext(),
"Stack Overflow", //Title of detail view
"This will launch Stack Overflow", //Text on detail view
pi
);

//Display the Notification
NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(ID_HELLO_WORLD, notification); //ID_HELLO_WORLD is a int ID


Related Topics



Leave a reply



Submit