Tuesday 7 July 2015

andorid material design with design support library

Android design support library give us many material design components that are support from android 2.1 and higher devices.
I am using android studio for creating app using design support library so if you don't have android studio then get it from Download studio.
Below are new components provided by design support lib.
  1.  AppBarLayout
  2.  CollapsingToolbarLayout
  3.  FloatingActionButton
  4.  NavigationView
  5.  SnackBar
  6.  TabLayout
  7.  TextInputLayout
You must take care below thing while you want your view to be support from andorid 2.1 and above .
android.support.design.widget.*
In stead of * your view will come.
AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures.
Children should provide their desired scrolling behavior through setScrollFlags(int) and the associated layout xml attribute: app:layout_scrollFlags.
This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.
AppBarLayout also requires a separate scrolling sibling in order to know when to scroll. The binding is done through the AppBarLayout.ScrollingViewBehavior behavior class, meaning that you should set your scrolling view's behavior to be an instance of AppBarLayout.ScrollingViewBehavior. A string resource containing the full class name is available.
 


 
    
 

 

 
    
 
    
 

 

You can see google guide for using AppBarLayout  from Google Design Spec AppBar and example use in my app at github.
CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout. CollapsingToolbarLayout contains the following features:
CollpasingToolbarLayout is use full when you want your top header content to be collapse from RecycleView or  NestedScrollView .
NOTE: Scrolling Technique will not work with Listview use RecycleView or NestedScrollView instead of it.
You can see in top image where i have use floating button over RecycleView and below is xml layout that used for it.

 

 
 
    
 
    
 
        
 
 
        
 
        
 
         
 
 
 
    
 
 
    
 
 

 

 

 
 
It will give design output like below one
HomeScreen
Represents a standard navigation menu for application. The menu contents can be populated by a menu resource file.
NavigationView is typically placed inside a DrawerLayout.

 
    
 
        
 
 
        
 
        
        
 
    
 
 
    
 
 

It will look like below screen :
NavigationView
We need to put Tablayout inside AppBarLayout like below :


 
            
 
 
            
 
            
 
             
 
 
 
        

Then in java code you can add Tabs
mTabLayout=(TabLayout)findViewById(com.himotech.matrialdesign.R.id.tab_layout);

/*mTabLayout.addTab(mTabLayout.newTab().setCustomView(R.layout.custom_tab_layout));
 
mTabLayout.addTab(mTabLayout.newTab().setCustomView(R.layout.custom_tab_layout));
 
mTabLayout.addTab(mTabLayout.newTab().setCustomView(R.layout.custom_tab_layout));*/
 
mTabLayout.addTab(mTabLayout.newTab().setText("Tab One"));
 
mTabLayout.addTab(mTabLayout.newTab().setText("Tab Two"));
 
mTabLayout.addTab(mTabLayout.newTab().setText("Tab Third"));
TabLayout
You can check exiting project from Github

Friday 12 June 2015

Material design with use of android design support library

Google recently on 25 May 2015 post update in support library which have few major components which are useful for creating app which look like more materials .

You can check this official blog http://android-developers.blogspot.in/2015/05/android-design-support-library.html.

From that i have created a demo that showing uses of this library .

You can find it on https://github.com/Himanshu4003/MaterialDesign

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

Friday 22 August 2014

Let ' s check android L ' s new UI RecyclerView

As per Google Document RecyclerView is a more advanced and flexible version of ListView.

This widget is a container for large sets of views that can be recycled and scrolled very efficiently. Use the RecyclerView widget when you have lists with elements that change dynamically.

The RecyclerView has been developed with extensibility in mind, so it is possible to create any kind of layout you can think of, but not without a little pain-in-the-ass dose. This is Android, so things are never easy.
If you want to use a RecyclerView, you will need to feel comfortable with three elements:
- RecyclerView.Adapter
- LayoutManager
- ItemAnimator
  • A layout manager for positioning items
  • Default animations for common item operations


RecyclerView is easy to use, because it provides:
You also have the flexibility to define custom layout managers and animations for this widget.
To use the RecyclerView widget, you have to specify an adapter and a layout manager. To create an adapter, you extend the RecyclerView.Adapter class. The details of the implementation depend on the specifics of your dataset and the type of views. For more information, see the examples below.

We can use RecyclerView from Api version 7 in android .

Let me make my first list with RecyclerView and share my code with you all.


Below is my Activity class which use RecycleView to show list items.

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class RecycleActivity extends ActionBarActivity {

   
    private RecyclerView mRecyclerView;
   
    private ListAdapter mListAdapter;
   
   
    private ArrayList<String> mArrayList=new ArrayList<String>();
   
   
    private LayoutManager mLayoutManager;
   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle);
      
        initView();
      
    }
   
   
    private void initView(){
      
        mRecyclerView=(RecyclerView)findViewById(R.id.recycle_view);
      
        mRecyclerView.setHasFixedSize(true);
      
        mLayoutManager=new LinearLayoutManager(this);
      
        mRecyclerView.setLayoutManager(mLayoutManager);
      
        mArrayList.clear();
      
        for (int i=0;i<30;i++){
          
            mArrayList.add("Listview Item  at Position "+ i);
          
        }
      
      
        mListAdapter=new ListAdapter(mArrayList);
      
        mRecyclerView.setAdapter(mListAdapter);
      
      
    }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.recycle, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


Here is my Adapter Class to bind data in to RecycleView


import java.util.ArrayList;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ListAdapter extends RecyclerView.Adapter<ViewHolder>{
   
    private ArrayList<String> mArrayData;
   
   
    public ListAdapter(ArrayList<String> mArrayListUpdate){
       
        this.mArrayData=mArrayListUpdate;
       
    }
   
    public static class ViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder{
       
        public ViewHolder(View view) {
            super(view);
            // TODO Auto-generated constructor stub
           
            mRlRowView=(RelativeLayout) view;
           
            mTxtView=(TextView) mRlRowView.findViewById(R.id.txt_title);
           
            mImgView=(ImageView)mRlRowView.findViewById(R.id.img_row);
           
            mCheckBox=(CheckBox)mRlRowView.findViewById(R.id.checkbox_item);
           
        }

        CheckBox mCheckBox;
       
        ImageView mImgView;
       
        TextView mTxtView;
       
        RelativeLayout mRlRowView;
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return mArrayData.size();
       
    }
   
   
     // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(
            android.support.v7.widget.RecyclerView.ViewHolder viewHolder, final int position) {
        // TODO Auto-generated method stub
       
        Log.i("LOG_TAG", "onBindViewHolder-->"+ position);
       
        ViewHolder mViewHolder=((ViewHolder)viewHolder);
       
        mViewHolder.mTxtView.setText(mArrayData.get(position));
       
        mViewHolder.mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
           
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
               
                Log.w("LOG_TAG", "onCheckedChanged is"+ isChecked + "Position is"+ position);
               
            }
        });
   
    }

    @Override
    public android.support.v7.widget.RecyclerView.ViewHolder onCreateViewHolder(
            ViewGroup viewGroup, int viewType) {
        // TODO Auto-generated method stub
        // create a new view

        Log.i("LOG_TAG", "onCreateViewHolder-->");
        View mView=LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row_item, viewGroup, false);
       
         // set the view's size, margins, paddings and layout parameters

        ViewHolder mViewHolder=new ViewHolder(mView);
       
       
        return mViewHolder;
    }
   }


Here is download link for my created Demo :
https://drive.google.com/file/d/0B-8An4Rd1nmKcU93VHU1Y1FWTG8/view?usp=sharing
It's Goog Drive link and from top there is download icons from there you can easily download whole code.

 

Friday 16 May 2014

What mean by Activities, tasks, and intents in Android ?


Activities In Android:

In Android, an activity is an application component that defines a screen of information and all of the associated actions the user can perform. Your app is a collection of activities, consisting of both the activities you create and those you re-use from other apps.

 

Tasks:

A task is the sequence of activities a user follows to accomplish a goal. A single task can make use of activities from just one app, or may draw on activities from a number of different apps.

 

Intents:

An intent is a mechanism for one app to signal it would like another app's assistance in performing an action. An app's activities can indicate which intents they can respond to. For common intents such as "Share", the user may have many apps installed that can fulfill that request.