notifications - How do I show text in android system status bar -


i'm trying develop app android nougat , want show info/text in status bar generating android service routine. problem is, don't know how show text in status bar.

i added sample image show mean (red circle). know possible, because saw in battery monitor app play store.

sample

i tried notificationcombat.builder, think not right way. maybe overlay is, after searching didn't find something.

can show me how or give me hint, please?

edit: here test code notificationcompat.builder

mainactivity.java

import android.app.notification; import android.app.notificationmanager; import android.os.bundle; import android.support.v4.app.notificationcompat; import android.support.v7.app.appcompatactivity; import android.support.v7.widget.toolbar;  public class mainactivity extends appcompatactivity {     private final int notification_id = 10;      @override     protected void oncreate (bundle savedinstancestate)     {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);          notificationcompat.builder mbuilder = new notificationcompat.builder(this);         mbuilder.setcontenttitle("value");         mbuilder.setcontenttext("123");         mbuilder.setsmallicon(r.mipmap.ic_launcher);         mbuilder.setongoing(true);         mbuilder.setautocancel(false);          //intent resultintent = new intent(this, mainactivity.class);         //pendingintent resultpendingintent = pendingintent.getactivity(this, 0, resultintent, pendingintent.flag_update_current);         //mbuilder.setcontentintent(resultpendingintent);          notification notification = mbuilder.build();         notification.flags |= notification.flag_no_clear | notification.flag_ongoing_event;          notificationmanager mnotifymgr = (notificationmanager) getsystemservice(notification_service);         mnotifymgr.notify(notification_id, notification);     } } 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <relativelayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitssystemwindows="true"     tools:context=".mainactivity">      <android.support.design.widget.coordinatorlayout         android:layout_width="match_parent"         android:layout_height="match_parent">          <android.support.design.widget.appbarlayout android:layout_height="wrap_content"                                                     android:layout_width="match_parent"                                                     android:theme="@style/apptheme.appbaroverlay">              <android.support.v7.widget.toolbar android:id="@+id/toolbar"                                                android:layout_width="match_parent"                                                android:layout_height="wrap_content"                                                android:background="#000000"                                                app:popuptheme="@style/apptheme.popupoverlay" />          </android.support.design.widget.appbarlayout>          <linearlayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_alignparentleft="true"             android:layout_alignparenttop="true"             android:orientation="vertical"             app:layout_behavior="@string/appbar_scrolling_view_behavior"             android:weightsum="100" >              <textview                 android:id="@+id/tv_value"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"                 android:text="hello world!"                 app:layout_constraintbottom_tobottomof="parent"                 app:layout_constraintleft_toleftof="parent"                 app:layout_constraintright_torightof="parent"                 app:layout_constrainttop_totopof="parent"/>          </linearlayout>      </android.support.design.widget.coordinatorlayout>  </relativelayout> 

result:

result 1

result 2

you can find answer in doc, here: https://developer.android.com/reference/android/support/v4/app/notificationcompat.html

edit:: well, answer in doc. however, after bit of research , digging, seems though consensus amongst community not possible application. specific icons can placed on right side of status bar (i.e. clock, weather, system info, etc...).

i'm sorry there isn't more exciting answer, @ least can stop stressing out why can't figure out.

edit 2:: apparently, pre-lollipop devices had access private apis allowed work system icons (again, think alarm icon). afterward, apis removed. stackoverflow post goes on whole situation pretty extensively.

edit 3:: if can live placing icon on left side of status bar can convert text bitmap this:

 textview textview = new textview(activity.getcontext());  textview.settext("hello world");  textview.setdrawingcacheenabled(true);  textview.destroydrawingcache();  textview.builddrawingcache();  bitmap bitmap = gettransparentbitmapcopy(textview.getdrawingcache());  private bitmap gettransparentbitmapcopy(bitmap source) {      int width = source.getwidth();      int height = source.getheight();      bitmap copy = bitmap.createbitmap(width, height, bitmap.config.argb_8888);     int[] pixels = new int[width * height];     source.getpixels(pixels, 0, width, 0, 0, width, height);     copy.setpixels(pixels, 0, width, 0, 0, width, height);     return copy;  } 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -