LorenzoBerti LorenzoBerti - 2 months ago 27
MySQL Question

Add badge in GridView for unread notification Android

Hello I have my custom

GridViewAdapter
where I have 7 voice.

String[] description = new String[]{"test", "test1", "test2", "test3", "test4", "test5", "notification"};
int[] imageId = {
R.mipmap.test,
R.mipmap.test1,
R.mipmap.test2,
R.mipmap.test3,
R.mipmap.test4,
R.mipmap.test5,
R.mipmap.notification,
};


And in my DB (mySQL db) I have a table for notification where I know if one notification is read or not.
So i would like put a badge in Element notification with number of notification unread.

The actual layout of grid element is:

<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/image"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
/>
<TextView
android:id="@+id/description"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>


And I believe that if I add a TextView in rowLayout (Like below) could be work

<TextView
android:id="@+id/unread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>


But with this method I will see
TextView
in all element.
How can I put this textView only for one element and refresh number when the notification will add?

I see this post https://github.com/leolin310148/ShortcutBadger
and https://github.com/jgilfelt/android-viewbadger
but seems deprecated.

My
Adapter
is:

public class GridViewDashboardAdapter extends BaseAdapter {
public class Holder
{
TextView tv;
TextView notification;
ImageView img;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Holder holder = new Holder();
View rowView;
rowView = inflater.inflate(R.layout.dashboard_grid, null);
holder.tv=(TextView) rowView.findViewById(R.id.description);
holder.notification=(TextView) rowView.findViewById(R.id.description);
holder.img=(de.hdodenhof.circleimageview.CircleImageView) rowView.findViewById(R.id.image);

holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
/* pseudo code */
if(notificationUnread.size()>0){
holder.notification.setVisibility(View.VISIBLE);
}
}
}

Answer

Try to replace

android:visible="gone"

to

android:visibility="gone"

In the getView you can change visibility like this:

if (notificationUnread.size()>0){
    holder.notification.setVisibility(View.VISIBLE);
} else {
    holder.notification.setVisibility(View.GONE);
}
Comments