Thursday 18 December 2014

Creating notification using Notification Compact and Extended notification

Today i have use extended notification style and check how it's working .

if you want to try check below activity .




public class NotificationActivity extends ActionBarActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notification);

        initView();
    }



    private void initView(){


        Log.v("LOG_TAG","NotificationActivity-->"+ "initView(0)");

        Button mbtnGeneNotification = (Button) findViewById(R.id.btn_notification);

        Button mbtnExtendedNotification=(Button)findViewById(R.id.btn_extended);


        mbtnExtendedNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                generateExtendedNotification();
            }
        });
        mbtnGeneNotification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                generateSimpleNotification();

            }
        });


    }


    private void generateSimpleNotification(){

        NotificationCompat.Builder mNotificationBuilder=new NotificationCompat.Builder(this);

        mNotificationBuilder.setSmallIcon(R.drawable.ic_launcher);

        mNotificationBuilder.setContentText("App notification text here");

        mNotificationBuilder.setContentTitle("App notification Title here");

        mNotificationBuilder.setAutoCancel(true);

        // Creates an explicit intent for an Activity in your app

        Intent resultIntent=new Intent(this,NotificationParentActivity.class);

         // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.
        TaskStackBuilder mStackBuilder=TaskStackBuilder.create(this);
        // Adds the back stack for the Intent (but not the Intent itself)
        mStackBuilder.addParentStack(NotificationParentActivity.class);
        // Adds the Intent that starts the Activity to the top of the stack
        mStackBuilder.addNextIntent(resultIntent);
        PendingIntent mPendingIntent=mStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mNotificationBuilder.setContentIntent(mPendingIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        mNotificationManager.notify(0, mNotificationBuilder.build());

    }



    private void generateSimplenNonStackNotification(){

        NotificationCompat.Builder mNotificationBuilder=new NotificationCompat.Builder(this);

        mNotificationBuilder.setSmallIcon(R.drawable.ic_launcher);

        mNotificationBuilder.setContentText("App notification text here");

        mNotificationBuilder.setContentTitle("App notification Title here");

        mNotificationBuilder.setAutoCancel(true);

        // Creates an explicit intent for an Activity in your app

        Intent resultIntent=new Intent(this,NotificationActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.

        // Sets the Activity to start in a new, empty task
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent mPendingIntent= PendingIntent.getActivity(this,1,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

         //mStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mNotificationBuilder.setContentIntent(mPendingIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        mNotificationManager.notify(0, mNotificationBuilder.build());

    }




    private void generateExtendedNotification(){



        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Event tracker")
                .setContentText("Events received");

        NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();

        mBuilder.setAutoCancel(true);


        mBuilder.setNumber(4);

        String[] events = new String[6];


        events[0]="It's Holiday today";
        events[1]="It's Saturday today";
        events[2]="It's Sunday today";
        events[3]="It's Monday today";
        events[4]="It's Tueday today";

// Sets a title for the Inbox in expanded layout
        inboxStyle.setBigContentTitle("Event tracker details:");



        // Moves events into the expanded layout
        for (int i=0; i < events.length; i++) {

            inboxStyle.addLine(events[i]);
        }
// Moves the expanded layout object into the notification object.
        mBuilder.setStyle(inboxStyle);


        Intent resultIntent=new Intent(this,NotificationParentActivity.class);

        // The stack builder object will contain an artificial back stack for the
        // started Activity.
        // This ensures that navigating backward from the Activity leads out of
        // your application to the Home screen.

        // Sets the Activity to start in a new, empty task
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

        PendingIntent mPendingIntent= PendingIntent.getActivity(this,2,resultIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        //mStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(mPendingIntent);

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(
                        Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
        mNotificationManager.notify(2, mBuilder.build());



    }


    @Override
    protected void onNewIntent(Intent intent) {

        Log.i("LOG_TAG","onNewIntent------>");

        super.onNewIntent(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_notification, menu);
        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) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}





Below is my manifest file:



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.himosoft.notificationtype" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".NotificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".NotificationParentActivity"
            android:label="@string/title_activity_notification_parent"
            android:parentActivityName=".NotificationActivity"
            >

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".NotificationActivity"
                >


            </meta-data>
        </activity>

    </application>

</manifest>

Wednesday 17 December 2014

Android L like Drawer navigation animation and it's using Toolbar for top Navigation

I have created one demo app from Sliding tab demo with android L like animation of arrow.

you can check my git for complete code and comment over there is you have any issue .I have build that project using android studio so you need to have it for run and test.

https://github.com/Himanshu4003/SlidingDrawerWithTabs

Here is look of app screen:







Happy Coding