Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting progress bar visibility to View.GONE makes the whole notification invisible
    primarykey
    data
    text
    <p>I create a notification from my custom layout with a progress bar. When the task is finished I make the progress bar invisible, this is working fine with Android 2.3.3 and newer versions. In older version, whole notification becomes invisible, no icon, no text, no nothing, but the "Clear" button becomes activated which means there is a notification.</p> <p>How I created the notification and updated using <code>NotificationCompat</code> from Android Support Library;</p> <p>Following variables are declared in AsyncTask class as private members;</p> <pre><code>private NotificationCompat.Builder nBuilder; private RemoteViews contentView; private Notification n; private int nId; //Notification ID </code></pre> <p>AsyncTask Functions;</p> <p>When download start</p> <pre><code>protected void onPreExecute() { super.onPreExecute(); contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); contentView.setTextViewText(R.id.notification_title, nTitle); contentView.setProgressBar(R.id.notification_progressbar, 100, 0, false); contentView.setImageViewResource(R.id.notification_image, R.drawable.ic_launcher); contentView.setTextViewText( R.id.notification_progresstext, "" + df.format(total) + " MB / " + df.format((float) fileLength) + " MB"); nBuilder = new NotificationCompat.Builder(getApplicationContext()); nBuilder.setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()).setTicker(mp3Name); if (Build.VERSION.SDK_INT &gt;= 11) { // sets FLAG_ONGOING_EVENT nBuilder.setOngoing(true).setContent(contentView) .setAutoCancel(false); n = nBuilder.build(); } else { // required, otherwise will crash with an IllegalArgException Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); PendingIntent pend = PendingIntent.getActivity( getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); nBuilder.setContentIntent(pend); n = nBuilder.build(); n.contentView = contentView; // since flags are ignored in earlier version set them directly n.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR; } nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nManager.notify(nId, n); Toast.makeText(getApplicationContext(), R.string.download_started, Toast.LENGTH_LONG).show(); } </code></pre> <p>On progress update</p> <pre><code>protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); if (progress[0] != currentProgress) { if (progress[0] &gt;= 100) { // PROGRESS 100 nManager.cancel(nId); nBuilder = new NotificationCompat.Builder( getApplicationContext()); nBuilder.setSmallIcon(R.drawable.ic_launcher) .setWhen(System.currentTimeMillis()) .setTicker(mp3Name); contentView.setProgressBar(R.id.notification_progressbar, 100, progress[0], false); contentView.setTextViewText( R.id.notification_progresstext, "" + df.format(total / 1048576f) + " MB / " + df.format(fileLength / 1048576f) + " MB"); contentView.setViewVisibility( R.id.notification_progressbar, View.GONE); Intent intent = new Intent( android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.parse("file://" + fullFilePath), "audio/mpeg"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); PendingIntent pi = PendingIntent.getActivity( getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); nBuilder.setContentIntent(pi); if (Build.VERSION.SDK_INT &gt;= 11) { nBuilder.setOngoing(false).setContent(contentView) .setAutoCancel(true); n = nBuilder.build(); } else { n = nBuilder.build(); n.contentView = contentView; n.flags = Notification.FLAG_AUTO_CANCEL; } nManager.notify(nId, n); } else { // PROGRESS &lt; 100 contentView.setProgressBar(R.id.notification_progressbar, 100, progress[0], false); contentView.setTextViewText( R.id.notification_progresstext, "" + df.format(total / 1048576f) + " MB / " + df.format(fileLength / 1048576f) + " MB"); if (Build.VERSION.SDK_INT &gt;= 11) { nBuilder.setContent(contentView); n = nBuilder.build(); } else { n = nBuilder.build(); n.contentView = contentView; } nManager.notify(nId, n); } // nManager.notify(nId, n); currentProgress = progress[0]; } } </code></pre> <p>I have to remove <code>contentView.setViewVisibility(R.id.notification_progressbar, View.GONE);</code>, then it looks ugly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload