Android How to used Actionbar notification count with Server

Hii everyone in this Android tutorial, I am sharing Android How to used Actionbar notification count with Server. In these Android action bar tutorial, you can learn how to add Icons in action bar. perform an action on Button click like an activity and Inactive Your profile. Show an Android action bar notification with count. In android apps whenever you have select items functionality and CTA(Call to Action) button in ActionBar. Then showing a count of the number of selected items on CTA Icon Button makes a great UX.

Basic about Action Bar:

It mainly contains four functional areas. Action Buttons, App Icon, View Control (app title), Action Overflow
Here we are going to show how to implement ActionBar with. Alert dialog, Setting, And Status Online offline message by clicking on widget in ActionBar.

Step 1: Android Menu XML  in res-menu-menuItem.

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context="in.rocareindia.com.rocareindiaems.MainActivity">

    <item
        android:id="@+id/action_Attend"
        app:actionLayout="@layout/attend"
        app:showAsAction="always"
        android:title="Notifications" />
    <item
        android:id="@+id/action_notification"
        app:actionLayout="@layout/notification"
        app:showAsAction="always"
        android:title="Notifications" />
    <item
        android:id="@+id/action_Update"
        android:orderInCategory="100"
        android:title="Update"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_Rating"
        android:orderInCategory="100"
        android:title="Rating"
        app:showAsAction="never" />

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

Step 2: Create a notification.xml  XML file in Layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:id="@+id/badge_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="10"
        android:padding="3dp"
        android:layout_marginLeft="20dp"
        android:gravity="center"
        android:textColor="#FFF"
        android:textSize="11sp"
        android:background="@drawable/notification_count" />

    <com.joanzapata.iconify.widget.IconButton
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:textColor="@color/text"
        android:background="@drawable/ic_notifications_white_24dp"
        android:id="@+id/badge_icon_button"/>
</RelativeLayout>

Step 3: Android in drawable folder Create a count.xml file in drawable.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:useLevel="false"
    android:thickness="9dp"
    android:innerRadius="0dp"
    >

    <solid
        android:color="#F00"
        />
    <stroke
        android:width="1dip"
        android:color="#FFF" />

    <padding
        android:top="2dp"
        android:bottom="2dp"/>

</shape>

Step 4:  Android Main Activity Layout main.xml add these code.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="@color/colorAccent"
        android:background="@drawable/navi2"
        app:itemIconTint="@color/colorAccent"
        app:headerLayout="@layout/nav_header_main"
        android:fitsSystemWindows="true"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

Step 5: Create a app_bar_main. XML Layout .

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
    tools:context="in.rocareindia.com.rocareindiaems.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|start"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@drawable/ic_call_white_24dp" />

</android.support.design.widget.CoordinatorLayout>

Step 6:  Android Java Class Add these Code MainActivity.java.

In java class, you can add the onCreateOptionsMenu(Menu menu)    method to add the icons and perform the action on click on item. In this Android tutorial, Android How to used Actionbar notification count with Server. To hit the API on item Click.

 

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
    public static final String KEY_Email = "user_name";
    public static final String KEY_Status="status";
   
    TextView itemMessagesBadgeTextView;
    TextView itemMessagesBadgeTextView1,NotificationTextView;
    String Status;
    public static String EMAIL;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Emp_Status();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    final View notificaitons = menu.findItem(R.id.action_notification).getActionView();
    itemMessagesBadgeTextView = (TextView) notificaitons.findViewById(R.id.badge_textView);
    IconButton iconButtonMessages = (IconButton) notificaitons.findViewById(R.id.badge_icon_button);
    iconButtonMessages.setTextColor(getResources().getColor(R.color.text));
    iconButtonMessages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent =new Intent(MainActivity.this,Notification.class);
            startActivity(intent);

        }
    });
    final View notificaitons1 = menu.findItem(R.id.action_Attend).getActionView();
    itemMessagesBadgeTextView1 = (TextView) notificaitons1.findViewById(R.id.badge_textView_Color);
    NotificationTextView=(TextView)notificaitons1.findViewById(R.id.badge_Status);
    itemMessagesBadgeTextView1.setVisibility(View.GONE); //
    NotificationTextView.setVisibility(View.GONE);
    notificaitons1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ContextThemeWrapper ctw = new ContextThemeWrapper( MainActivity.this, R.style.Theme_AlertDialog);
            final android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(ctw);
            alertDialogBuilder.setTitle("Attendance");
            alertDialogBuilder.setMessage("Welcome to RO Care India Sales take your attention Change your status  ");
            alertDialogBuilder.setPositiveButton("InActive", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    Status="0";
                    Emp_Attandance();
                }
            });
            alertDialogBuilder.setNegativeButton("Active", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Status="1";
                    Emp_Attandance();
                }
            });
            alertDialogBuilder.show();

        }
    });

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        SessionManagement sessionManagement=new SessionManagement();
        sessionManagement.Logout(MainActivity.this);
        Intent intent=new Intent(MainActivity.this,LogIn.class);
        startActivity(intent);
        finish();
        return true;
    }if(id == R.id.action_Rating){
        try {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("market://details?id=" + this.getPackageName())));
        } catch (android.content.ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
        }

    }if(id == R.id.action_Update){
        try {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("market://details?id=" + this.getPackageName())));
        } catch (android.content.ActivityNotFoundException e) {
            startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://play.google.com/store/apps/details?id=" + this.getPackageName())));
        }

    }

    return super.onOptionsItemSelected(item);
}

 

 

 

You may also like...