Detect screen rotation on Android

Manifest:

<activity android:name=".MyActivity" android:configChanges="screenSize|orientation|screenLayout|navigation"/>

Activity:

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    Log.d("tag", "config changed");
    super.onConfigurationChanged(newConfig);

    int orientation = newConfig.orientation;
    if (orientation == Configuration.ORIENTATION_PORTRAIT)
        Log.d("tag", "Portrait");
    else if (orientation == Configuration.ORIENTATION_LANDSCAPE)
        Log.d("tag", "Landscape");
    else
        Log.w("tag", "other: " + orientation);

    ....
}

References
https://stackoverflow.com/questions/6896243/how-can-i-detect-screen-rotation

Show Progress bar while loading image using Glide

Glide.with(this)
            .load("https://raw.githubusercontent.com/bumptech/glide/master/static/glide_logo.png")
            .listener(new RequestListener<Drawable>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }

                @Override
                public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                    progressBar.setVisibility(View.GONE);
                    return false;
                }
            })
            .into(imageView);

 

References
https://stackoverflow.com/questions/35305875/progress-bar-while-loading-image-using-glide

Detect if App is in release or debug mode in android

Make sure you are referencing your project’s BuildConfig class, not from any of your dependency libraries.

if (BuildConfig.DEBUG) {
  // do something for a debug build
}
release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            debuggable false
        }
        debug {
            debuggable true
        }

References
https://stackoverflow.com/questions/23844667/detect-if-i-am-in-release-or-debug-mode-in-android
http://tekeye.uk/android/examples/android-debug-vs-release-build

Detect when the last item is shown on RecyclerView

public abstract class OnVerticalScrollListener
        extends RecyclerView.OnScrollListener {

    @Override
    public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (!recyclerView.canScrollVertically(-1)) {
            onScrolledToTop();
        } else if (!recyclerView.canScrollVertically(1)) {
            onScrolledToBottom();
        } else if (dy < 0) {
            onScrolledUp();
        } else if (dy > 0) {
            onScrolledDown();
        }
    }

    public void onScrolledUp() {}

    public void onScrolledDown() {}

    public void onScrolledToTop() {}

    public void onScrolledToBottom() {}
}

Useful info

visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();

References
https://stackoverflow.com/questions/26543131/how-to-implement-endless-list-with-recyclerview

Badge for TabLayout using setCustomView

layout/tab_header_badge.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutTabHeader"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageViewTabHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_bell" />

    <TextView
        android:id="@+id/textViewActiveAlarmBadge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/imageViewTabHeader"
        android:text="1"
        android:textColor="@color/accent" />

</RelativeLayout>
            viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

            viewPagerAdapter.addFragment(moreFragment); // 3
            viewPagerAdapter.addFragment(alarmHistoryFragment); // 2
            viewPagerAdapter.addFragment(activeAlarmsFragment); // 1
            viewPagerAdapter.addFragment(mainFragment); // 0


            viewPagerMain.setAdapter(viewPagerAdapter);
            viewPagerMain.setCurrentItem(3); // set default view on open
            tabLayoutMain.setupWithViewPager(viewPagerMain);

            tabLayoutMain.getTabAt(3).setIcon(R.drawable.ic_home);
            //tabLayoutMain.getTabAt(2).setIcon(R.drawable.ic_bell);
            tabLayoutMain.getTabAt(1).setIcon(R.drawable.ic_envelope_open);
            tabLayoutMain.getTabAt(0).setIcon(R.drawable.ic_bars);


            View tabHeader = LayoutInflater.from(this).inflate(R.layout.tab_header_badge, null);
            TextView textViewBadge = tabHeader.findViewById(R.id.textViewActiveAlarmBadge);
            textViewBadge.setText("10");
            tabLayoutMain.getTabAt(2).setCustomView(tabHeader);

            // endregion

References
https://mobikul.com/make-custom-tabs-icons-android/
https://stackoverflow.com/questions/41530141/how-to-show-notification-counter-in-a-tablayout
https://stackoverflow.com/questions/43266079/how-to-add-badge-to-android-tablayout

Tablayout with Icons on Android

mTabLayout.setupWithViewPager(mViewPager);
for (int i = 0; i < mTabLayout.getTabCount(); i++) {
  mTabLayout.getTabAt(i).setIcon(R.drawable.your_icon);
}

or

<android.support.design.widget.TabLayout
         app:tabTextColor="@color/gray"
         app:tabMode="fixed"
         app:tabBackground="@color/red"
         app:tabIndicatorHeight="4dp"
         app:tabIndicatorColor="@color/purple"
         app:tabPadding="2dp"
         app:tabSelectedTextColor="@color/white"
         app:tabMinWidth="64dp"
         android:layout_height="wrap_content"
         android:layout_width="match_parent">

     <!--add height and width to TabItem -->
     <android.support.design.widget.TabItem 
             android:text="@string/tab_text"/>

     <android.support.design.widget.TabItem
             android:icon="@drawable/ic_android"/>

 </android.support.design.widget.TabLayout>

References
https://stackoverflow.com/questions/30892545/tablayout-with-icons-only