Tuesday, 23 August 2016

Android viewpager with Circle indicator


ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we’ll implement a ViewPager that swipes through three views with different images and texts.
Android ViewPager widget is found in the support library and it allows the user to swipe left or right to see an entirely new screen.

Today we’re implementing a ViewPager by using Views and PagerAdapter.







Go to Build.Gradle
------------------------
dependencies {
 compile 'me.relex:circleindicator:1.2.1@aar'
}

activity_main.xml
------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
tools:context="com.sgp.anil.viewpager.MainActivity">

    <
FrameLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent">

        <
android.support.v4.view.ViewPager
           
android:id="@+id/viewpager"
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:scrollbars="none">
        </
android.support.v4.view.ViewPager>

        <
me.relex.circleindicator.CircleIndicator
            
android:id="@+id/indicator"
           
android:layout_width="match_parent"
           
android:layout_height="25dp"
           
android:layout_gravity="center_horizontal|bottom"
           
android:background="#000" />
    </
FrameLayout>
</
LinearLayout>

pager_list.xml
----------------------
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent">
    <
ImageView
       
android:id="@+id/large_image"
        
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:scaleType="fitXY"
       
android:src="@drawable/araku"
        
/>
</RelativeLayout>

Nature.Java
--------------------
package com.sgp.anil.viewpager;

/**
 * Created by Anil on 23-08-2016.
 */
public class Nature {
   
private int imageId;

   
public Nature(int imageId) {
       
this.imageId = imageId;
    }

   
public int getImageId() {
       
return imageId;
    }

   
public void setImageId(int imageId) {
       
this.imageId = imageId;
    }

}

CustomPageAdapter.java
------------------------------
package com.sgp.anil.viewpager;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by Anil on 23-08-2016.
 */
public class CustomPageAdapter extends PagerAdapter {
   
private Context context;
   
private List<Nature> dataObjectList;
   
private LayoutInflater layoutInflater;

   
public CustomPageAdapter(Context context, List<Nature> dataObjectList) {
       
this.context = context;
       
this.layoutInflater = (LayoutInflater) this.context.getSystemService(this.context.LAYOUT_INFLATER_SERVICE);
       
this.dataObjectList = dataObjectList;
    }

   
@Override
   
public int getCount() {
       
return dataObjectList.size();
    }

   
@Override
   
public boolean isViewFromObject(View view, Object object) {
       
return view == ((View) object);
    }

   
@Override
   
public Object instantiateItem(ViewGroup container, int position) {
        View view =
this.layoutInflater.inflate(R.layout.pager_list, container, false);
        ImageView displayImage = (ImageView) view.findViewById(R.id.
large_image);
        displayImage.setImageResource(
this.dataObjectList.get(position).getImageId());
        container.addView(view);
       
return view;
    }

   
@Override
   
public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}

MainActivity,Java
------------------------------
package com.sgp.anil.viewpager;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import me.relex.circleindicator.CircleIndicator;

public class MainActivity extends AppCompatActivity {
   
private ViewPager viewPager;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        List<Nature> getData = dataSource();
       
viewPager = (ViewPager) findViewById(R.id.viewpager);
        CustomPageAdapter mCustomPagerAdapter =
new CustomPageAdapter(MainActivity.this, getData);
       
viewPager.setAdapter(mCustomPagerAdapter);
        CircleIndicator indicator = (CircleIndicator) findViewById(R.id.
indicator);
        indicator.setViewPager(
viewPager);
    }

   
private List<Nature> dataSource() {
        List<Nature> data =
new ArrayList<Nature>();
        data.add(
new Nature(R.drawable.araku));
        data.add(
new Nature(R.drawable.araku2));
        data.add(
new Nature(R.drawable.araku3));
        data.add(
new Nature(R.drawable.araku4));
       
return data;
    }
}

Wednesday, 17 August 2016

Android Camera Video Capture

In this example capturing video by inbuild camera in android and save captured video on sdcard. Using camera intent to record video and after record the video again come back on activity and show captured video path on activity.








activity_main.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
android:orientation="vertical"
   
tools:context="com.sgp.anil.statusbarnotification.MainActivity">


    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Video capture"
       
android:onClick="vcapture"
       
android:id="@+id/button_videocap"
       
android:layout_gravity="center_horizontal" />

    <
VideoView
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
        
android:id="@+id/videoView" />
</
LinearLayout>
MainActivity.Java
package com.sgp.anil.cameravideocapture;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
    VideoView
videoview;
    Uri
uriVideo = null;

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

       
videoview = (VideoView) findViewById(R.id.videoView);
    }

   
   
public void vcapture(View v) {
        Intent int_video_camera =
new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(int_video_camera,
1);
    }

   
@Override
   
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       
uriVideo = data.getData();
        MediaController media_Controller =
new MediaController(MainActivity.this);
        videoview.setMediaController(media_Controller);
       
videoview.setVideoURI(uriVideo);
       
videoview.start();
       
super.onActivityResult(requestCode, resultCode, data);
    }
}






Monday, 8 August 2016

LoadMore RecyclerView with progress bar showing at bottom


I am trying to get Endless Recycler View with progress bar showing at bottom when you are loading data from web service. But faced different problems.
Finally found a solution at this link. May be someone needs this.




build.gradle
---------------------
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:cardview-v7:24.0.0'

activity_main.xml
---------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
   
android:orientation="vertical"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:context="sgp.anil.com.firebaseexample.MainActivity"
   
tools:showIn="@layout/activity_main">

    <
android.support.v7.widget.RecyclerView
       
android:id="@+id/my_recycler_view"
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:scrollbars="vertical" />

    <
TextView
       
android:id="@+id/empty_view"
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:gravity="center"
       
android:text="No Records"
       
android:visibility="gone" />
</
LinearLayout>
 list_row.xml
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:card_view="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="wrap_content"
   
android:orientation="horizontal"
   
card_view:cardCornerRadius="5dp"
   
card_view:cardUseCompatPadding="true">

    <
RelativeLayout
       
android:layout_width="match_parent"
       
android:layout_height="match_parent"
       
android:background="?android:selectableItemBackground">

        <
TextView
           
android:id="@+id/tvName"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_margin="5dp"
           
android:text="Name"
           
android:textColor="@android:color/black"
           
android:textSize="18sp" />

        <
TextView
           
android:id="@+id/tvEmailId"
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:layout_below="@+id/tvName"
           
android:layout_margin="5dp"
           
android:text="Email Id"
           
android:textColor="@android:color/black"
           
android:textSize="12sp" />
    </
RelativeLayout>

</
android.support.v7.widget.CardView>
 progressbar.xml
-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical" >

    <
ProgressBar
       
android:id="@+id/progressBar1"
       
android:layout_width="wrap_content"
       
android:layout_gravity="center_horizontal"
       
android:layout_height="wrap_content" />

</
LinearLayout>
 Student.Java
-------------------------
package sgp.anil.com.loadmore.utils;

import java.io.Serializable;

/**
 * Created by Anil on 05-08-2016.
 */
public class Student implements Serializable {

   
private static final long serialVersionUID = 1L;

   
private String name;

    
private String emailId;

   
public Student() {

    }

   
public Student(String name, String emailId) {
       
this.name = name;
       
this.emailId = emailId;
    }

   
public String getName() {
       
return name;
    }

   
public void setName(String name) {
       
this.name = name;
    }

   
public String getEmailId() {
       
return emailId;
    }

   
public void setEmailId(String emailId) {
       
this.emailId = emailId;
    }
}
 OnLoadMoreListener.Java
--------------------------------
package sgp.anil.com.loadmore.utils;

/**
 * Created by Anil on 05-08-2016.
*/

public interface OnLoadMoreListener {

    void onLoadMore();
}

DataAdapter.Java
-------------------------
package sgp.anil.com.loadmore.adapters;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import sgp.anil.com.loadmore.R;
import sgp.anil.com.loadmore.utils.OnLoadMoreListener;
import sgp.anil.com.loadmore.utils.Student;

/**
 * Created by Anil on 05-08-2016.
 */
public class DataAdapter extends RecyclerView.Adapter {
   
private final int VIEW_ITEM = 1;
   
private final int VIEW_PROG = 0;

   
private List<Student> studentList;

   
// The minimum amount of items to have below your current scroll position
    // before loading more.
   
private int visibleThreshold = 5;
   
private int lastVisibleItem, totalItemCount;
   
private boolean loading;
   
private OnLoadMoreListener onLoadMoreListener;


   
public DataAdapter(List<Student> students, RecyclerView recyclerView) {
       
studentList = students;

       
if (recyclerView.getLayoutManager() instanceof LinearLayoutManager) {

           
final LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView
                    .getLayoutManager();


            recyclerView
                    .addOnScrollListener(
new RecyclerView.OnScrollListener() {
                       
@Override
                       
public void onScrolled(RecyclerView recyclerView,
                                              
int dx, int dy) {
                           
super.onScrolled(recyclerView, dx, dy);

                           
totalItemCount = linearLayoutManager.getItemCount();
                            
lastVisibleItem = linearLayoutManager
                                   
.findLastVisibleItemPosition();
                           
if (!loading
                                   
&& totalItemCount <= (lastVisibleItem + visibleThreshold)) {
                                
// End has been reached
                                // Do something
                               
if (onLoadMoreListener != null) {
                                   
onLoadMoreListener.onLoadMore();
                                }
                               
loading = true;
                            }
                        }
                    });
        }
    }

   
@Override
   
public int getItemViewType(int position) {
       
return studentList.get(position) != null ? VIEW_ITEM : VIEW_PROG;
    }

   
@Override
   
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                     
int viewType) {
        RecyclerView.ViewHolder vh;
       
if (viewType == VIEW_ITEM) {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.
list_row, parent, false);

            vh =
new StudentViewHolder(v);
        }
else {
            View v = LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.
progressbar_item, parent, false);

            vh =
new ProgressViewHolder(v);
        }
       
return vh;
    }

   
@Override
   
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       
if (holder instanceof StudentViewHolder) {

            Student singleStudent = (Student)
studentList.get(position);

            ((StudentViewHolder) holder).
tvName.setText(singleStudent.getName());

            ((StudentViewHolder) holder).
tvEmailId.setText(singleStudent.getEmailId());

            ((StudentViewHolder) holder).
student = singleStudent;

        }
else {
            ((ProgressViewHolder) holder).
progressBar.setIndeterminate(true);
        }
    }

   
public void setLoaded() {
       
loading = false;
    }

   
@Override
   
public int getItemCount() {
       
return studentList.size();
    }

   
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
       
this.onLoadMoreListener = onLoadMoreListener;
    }


   
//
   
public static class StudentViewHolder extends RecyclerView.ViewHolder {
       
public TextView tvName;

       
public TextView tvEmailId;

       
public Student student;

       
public StudentViewHolder(View v) {
           
super(v);
           
tvName = (TextView) v.findViewById(R.id.tvName);

           
tvEmailId = (TextView) v.findViewById(R.id.tvEmailId);

            v.setOnClickListener(
new View.OnClickListener() {

               
@Override
               
public void onClick(View v) {
                    Toast.makeText(v.getContext(),
                           
"OnClick :" + student.getName() + " \n " + student.getEmailId(),
                            Toast.
LENGTH_SHORT).show();

                }
            });
        }
    }

   
public static class ProgressViewHolder extends RecyclerView.ViewHolder {
       
public ProgressBar progressBar;

       
public ProgressViewHolder(View v) {
           
super(v);
           
progressBar = (ProgressBar) v.findViewById(R.id.progressBar1);
        }
    }
}
 MainActivity.Java
--------------------------
package sgp.anil.com.loadmore;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import sgp.anil.com.firebaseexample.adapters.DataAdapter;
import sgp.anil.com.firebaseexample.utils.OnLoadMoreListener;
import sgp.anil.com.firebaseexample.utils.Student;

public class MainActivity extends AppCompatActivity {

   
private TextView tvEmptyView;
   
private RecyclerView mRecyclerView;
   
private DataAdapter mAdapter;
   
private LinearLayoutManager mLayoutManager;

   
private List<Student> studentList;
   
   
protected Handler handler;

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);

      
tvEmptyView = (TextView) findViewById(R.id.empty_view);
       
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
       
studentList = new ArrayList<Student>();
       
handler = new Handler();
       
if (toolbar != null) {
            setSupportActionBar(toolbar);
            getSupportActionBar().setTitle(
"SGP Students");

        }
        loadData();

       
// use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
       
mRecyclerView.setHasFixedSize(true);

       
mLayoutManager = new LinearLayoutManager(this);

       
// use a linear layout manager
       
mRecyclerView.setLayoutManager(mLayoutManager);

       
// create an Object for Adapter
       
mAdapter = new DataAdapter(studentList, mRecyclerView);

       
// set the adapter object to the Recyclerview
       
mRecyclerView.setAdapter(mAdapter);
        
//  mAdapter.notifyDataSetChanged();


       
if (studentList.isEmpty()) {
           
mRecyclerView.setVisibility(View.GONE);
           
tvEmptyView.setVisibility(View.VISIBLE);

        }
else {
           
mRecyclerView.setVisibility(View.VISIBLE);
           
tvEmptyView.setVisibility(View.GONE);
        }

       
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
           
@Override
           
public void onLoadMore() {
               
//add null , so the adapter will check view_type and show progress bar at bottom
               
studentList.add(null);
               
mAdapter.notifyItemInserted(studentList.size() - 1);

               
handler.postDelayed(new Runnable() {
                   
@Override
                   
public void run() {
                       
//   remove progress item
                       
studentList.remove(studentList.size() - 1);
                       
mAdapter.notifyItemRemoved(studentList.size());
                       
//add items one by one
                        
int start = studentList.size();
                       
int end = start + 20;

                       
for (int i = start + 1; i <= end; i++) {
                           
studentList.add(new Student("Student " + i, "AndroidStudent" + i + "@gmail.com"));
                           
mAdapter.notifyItemInserted(studentList.size());
                        }
                       
mAdapter.setLoaded();
                       
//or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
                   
}
                },
2000);

            }
        });

    }


   
// load initial data
   
private void loadData() {

       
for (int i = 1; i <= 20; i++) {
           
studentList.add(new Student("Student " + i, "androidstudent" + i + "@gmail.com"));

        }


    }

}



Encryption Decryption Example Android Using AES Alogirthm

activity_main.xml ------------------------- <?xml version="1.0" encoding="utf-8"?> <LinearLayou...