RSS

Implement Endless scrolling using StaggeredLayoutManager – Android

You can use one of two methods in the StaggeredGridLayoutManager:

  1. mLayoutManager.findFirstVisibleItemPositions
  2. mLayoutManager.findFirstCompletelyVisibleItemPositions

Pass an empty int array that will get initialized with the positions and use the one that makes sense for you.

private boolean loading = true;
private int pastVisibleItems, visibleItemCount, totalItemCount;

mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener({
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

        visibleItemCount = mLayoutManager.getChildCount();
        totalItemCount = mLayoutManager.getItemCount();
        int[] firstVisibleItems = null;
        firstVisibleItems = mLayoutManager.findFirstVisibleItemPositions(firstVisibleItems);
        if(firstVisibleItems != null && firstVisibleItems.length > 0) {
            pastVisibleItems = firstVisibleItems[0];
        }

        if (loading) {
            if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                loading = false;
                Log.d("tag", "LOAD NEXT ITEM");
            }
        }
    }
});
 
Leave a comment

Posted by on October 25, 2016 in Android Source Code

 

Divider in RecyclerView with ItemDecoration

 

device-2016-10-12-1614071. Create class DividerItemDecoration.java

package com.nikshit.Utilities;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class DividerItemDecoration extends RecyclerView.ItemDecoration{
    private Drawable mDivider;
    public DividerItemDecoration(Drawable divider) {
        mDivider = divider;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);

        if (parent.getChildAdapterPosition(view) == 0) {
            return;
        }

        outRect.top = mDivider.getIntrinsicHeight();
    }

    @Override
    public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {
        int dividerLeft = parent.getPaddingLeft();
        int dividerRight = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount - 1; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int dividerTop = child.getBottom() + params.bottomMargin;
            int dividerBottom = dividerTop + mDivider.getIntrinsicHeight();

            mDivider.setBounds(dividerLeft, dividerTop, dividerRight, dividerBottom);
            mDivider.draw(canvas);
        }
    }
}

2. Create divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:height=".5dip" />
    <solid android:color="@android:color/darker_gray" />

</shape>

3. Add lines below where you have defined RecyclerView

  RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecoration(getResources().getDrawable(R.drawable.divider));
                mRVFishPrice.addItemDecoration(dividerItemDecoration);
 
Leave a comment

Posted by on October 12, 2016 in Android Source Code

 

Remove app from recent apps programmatically

Add New Activity:

public class ExitActivity extends Activity{
    @Override protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        if(android.os.Build.VERSION.SDK_INT >= 21){
            finishAndRemoveTask();
        }
        else{
            finish();
        }
    }

    public static void exitApplicationAnRemoveFromRecent(Context mContext){
        Intent intent = new Intent(mContext, ExitActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK  | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_ANIMATION);
        mContext.startActivity(intent);
    }
}

Now exit from other Activity:

@Override
public void onBackPressed() {
 ExitActivity.exitApplicationAnRemoveFromRecent(mContext);
 super.onBackPressed();
}

Disclaimer: Hiren Patel (http://stackoverflow.com/questions/13385289/remove-app-from-recent-apps-programmatically)

 
2 Comments

Posted by on September 1, 2016 in Android Source Code

 

Android Debug Bridge (adb) Wireless Debugging Over Wi-Fi

Screenshot_2016-09-02-11-01-50

  1. Open cmd
    go to path till
    C:\Users\servpro.nikhil\AppData\Local\Android\sdk\platform-tools
  2. Type
    adb usb
  3. Type
    adb devices
  4. Type
    adb tcpip 5556
  5. Type
    adb connect <Your device IP address>
    Note: To check device IP address go to settings > About Phone > Status > IP address
    You will find status ad Connected to
  6. Remove USB and Enjoy
 
Leave a comment

Posted by on August 30, 2016 in Android Source Code

 

How to do clear data through app – Android

public void clearApplicationData()
{
File cache = getCacheDir();
File appDir = new File(cache.getParent());
if (appDir.exists())
{
String[] children = appDir.list();
for (String s : children)
{
if (!s.equals("lib"))
{
deleteDir(new File(appDir, s));
Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
}
}
}
}

public static boolean deleteDir(File dir)
{
if (dir != null &amp;&amp; dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i &lt; children.length; i++)
{
boolean success = deleteDir(new File(dir, children[i]));
if (!success)
{
return false;
}
}
}
return dir.delete();
}

 
Leave a comment

Posted by on April 26, 2016 in Android Source Code

 

ViewPager update fragment on swipe – Android

1) Attach the Listener:

mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int i, final float v, final int i2) {
            }
            @Override
            public void onPageSelected(final int i) {
                YourFragmentInterface fragment = (YourFragmentInterface) mPagerAdapter.instantiateItem(mViewPager, i);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                } 
            }
            @Override
            public void onPageScrollStateChanged(final int i) {
            }
        });

2) This is your Interface:

public interface YourFragmentInterface {
    void fragmentBecameVisible();
}

3) Change your fragments so they implement this:

public class YourLovelyFragment extends Fragment implements YourFragmentInterface {

4) Implement the interface in the fragment

@Override
public void fragmentBecameVisible() {
    // You can do your animation here because we are visible! (make sure onViewCreated has been called too and the Layout has been laid. Source for another question but you get the idea.
}
 
Leave a comment

Posted by on February 5, 2016 in Android Source Code

 

Handle onKeyListener in Fragment – Android

public static class MyFragment extends Fragment {

        @Override
        public void onResume() {
            super.onResume();
            getView().setFocusableInTouchMode(true);
            getView().requestFocus();
            getView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    //YOUR CODE
                    return false;
                }
            });
        }
    }
 
Leave a comment

Posted by on February 3, 2016 in Android Source Code