I need to make "a shopping cart icon in action bar like in attached image with text on icon with count of items which user added to cart"
Asked
Active
Viewed 822 times
-1
-
2Do you have any question? – Franklin Hirata Aug 29 '17 at 22:36
-
Have you attempt or try anything ? – Satan Pandeya Aug 30 '17 at 02:46
-
someone give me this link https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc its solved my problem at all – Mahmoud Ahmed Aug 30 '17 at 14:43
2 Answers
0
Given the conditions of the feature I believe you need to implement something something like the layout:
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:layout_marginRight="10dp"
android:gravity="right|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/main_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
app:srcCompat="@drawable/heart_icon" />
<ImageView
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="-20dp"
app:srcCompat="@drawable/number_icon" />
</LinearLayout>

VK.N
- 193
- 1
- 2
- 12
0
okey then you need to create your icon menu bar manually to have full control on it.
firstly in activity.xml
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" >
<!-- set any fixed size for 'width' but DO NOT set wrap_content-->
<RelativeLayout
android:layout_width="48dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="[YOUR ICON]"/>
<!-- you can set any attributes you want (background-fixed position .. etc)
and you can also remove alignParentBottom , alignParentEnd ..
i just add it to be clear -->
<TextView
android:id="@+id/notification_text_view"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
then you can access to them directly if you use activity .. if you use fragment then do these steps.
in Fragment Class : add setter for both image_icon and text_notification
private ImageView iconImageView;
private TextView textNotificationView;
public void setIconImageView(ImageView iconImageView){
this.iconImageView= iconImageView;
}
public void setTextNotificationView(TextView textNotificationView){
this.textNotificationView= textNotificationView;
}
then you need to set your fragment manually. In Activity Class
if(savedInstanceState == null) { // for rotation
[FragmentClass] fragment = new [FragmentClass]();
fragment.setIconImageView( (ImageView)findViewById(R.id.child_list_search_edit_view))
fragment.setTextNotificationView( (TextView)findViewById(notification_text_view))
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, fragment, [Fragment TAG]).commit();
}else{
[FragmentClass] childActivityFragment =([FragmentClass]) getSupportFragmentManager()
.findFragmentByTag([Fragment TAG]);
fragment.setIconImageView( (ImageView)findViewById(R.id.child_list_search_edit_view))
fragment.setTextNotificationView( (TextView)findViewById(notification_text_view))
}
Now you can add your notifications as you want.

Mohamed Nagy Mostafa
- 343
- 3
- 15
-
Thanks , i have tried this solution and works good for me https://stackoverflow.com/questions/35009812/how-to-add-badges-on-toolbar-menuitem-icons?fref=gc – Mahmoud Ahmed Aug 30 '17 at 14:47