Sunday, January 31, 2016

Android Material Design Demo | Android Lolypop Material Design Example | Material Design Collaps Layout | Material Design SnackBar

Hello Friends,
Here a useful example for android material design, important steps are given below-

1)Create a new android project using visual studio.
2)open your build.gradle file from src folder and import below dependencies-
compile 'com.android.support:design:22.2.1'compile 'com.android.support:cardview-v7:22.2.1'
 
  
3) And now import full code from GIT-HUB and modify according to your use-

https://github.com/manishsri01/MaterialDesign

 




Important Code:
1)Calling fragments from navigation drawer-
@Overridepublic void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments  
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    switch (position) {

        case 0:
            FloatingFragment homeFragment = new FloatingFragment();
            transaction.replace(R.id.container, homeFragment);
            transaction.commit();
            break;
        case 1:
            SnackBarFragment snackbar = new SnackBarFragment();
            transaction.replace(R.id.container, snackbar);
            transaction.commit();
            break;
        case 2:
            CollapsFragment collasps = new CollapsFragment();
            transaction.replace(R.id.container, collasps);
            transaction.commit();
            break;

    }
}
 
2)Ssnackbar action-
 
package com.android.materialdesign.fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.materialdesign.R;

public class SnackBarFragment extends Fragment {

   @Override   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub      super.onCreate(savedInstanceState);
   }

   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {

      final View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_snackbar,
            container, false);

      FloatingActionButton fab = (FloatingActionButton)v.findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
         @Override         public void onClick(View view) {
            Snackbar.make(v.findViewById(R.id.coordinatorLayout), "I am a Snackbar", 
                 Snackbar.LENGTH_LONG).setAction("Action", new View.OnClickListener() {
               @Override               public void onClick(View v) {
                  Toast.makeText(getActivity(), "Snackbar Action", Toast.LENGTH_LONG).show();
               }
            }).show();
         }
      });

      return  v;
   }

}


 3)Floating point-

package com.android.materialdesign.fragment;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.android.materialdesign.R;

public class FloatingFragment extends Fragment {

   @Override   public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub      super.onCreate(savedInstanceState);
   }

   @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {

      View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_floating,
            container, false);
      FloatingActionButton fab = (FloatingActionButton)v.findViewById(R.id.fab);

      fab.setOnClickListener(new View.OnClickListener() {
         @Override         public void onClick(View view) {
            Toast.makeText(getActivity(),"work in progress..",Toast.LENGTH_SHORT).show();
         }
      });

      return  v;
   }

}


DOWNLOAD ZIP CODE

Thanks,
Manish