How to create an inbox style (with unread count) widget for android?

前端 未结 3 2023
借酒劲吻你
借酒劲吻你 2021-01-07 11:31

I am trying to create a widget for my android app. I want it to be a single cell but with an annotation in the top right. Similar to popular widgets which display an unread

相关标签:
3条回答
  • 2021-01-07 12:12

    Im not sure whether you are asking how to build a widget in general or just for the small unread indication. There are some tutorials on the net, how to build a widget in general. For example: this one or the official documentation.

    Once you mastered that, the small indicator becomes a minor issue, you just have to include it in your layout. When updating the widget, you just update the view that indicates the count or set its visibility to invisible when the count is 0.

    An example layout for this case would be

      <FrameLayout> 
                <RelativeLayout> <ImageView /> </RelativeLayout>
                <RelativeLayout> <TextView /> </RelativeLayout>
      </FrameLayout>
    

    Here the widget consists of one ImageView, that you usually center via its surrounding RelativeLayout. The Textview in the second RelativeLayout can be your unread indicator. Position it in the corner like you would in a RelativeLayout and style it (via android:background="@drawable/my_indicator_background" or something along those lines).

    And thats it. :)

    0 讨论(0)
  • 2021-01-07 12:18

    Implemented in one of my project here is Link for the layout I implemented.

    And I would suggest to read about Relative Layout, here is link.

    Hope this help.

    0 讨论(0)
  • 2021-01-07 12:36

    A widget like this is basically an standard RemoteViews implementation: http://developer.android.com/guide/topics/appwidgets/index.html is the guide for the widget. You need an XML layout that draws the icon as the widget background and overlay it with an image in the corner if the the count is greater than 0. You can use a TextEdit view with a background image and the number of images in the center of the background as the count.

    When you need to update it you can create a new RemoteViews instance from your layout file, set or hide the TextEdit based on the count and then call http://developer.android.com/reference/android/appwidget/AppWidgetManager.html#updateAppWidget%28int,%20android.widget.RemoteViews%29 to update the widget.

    0 讨论(0)
提交回复
热议问题