RecyclerView Last Item Extra Bottom Margin
Tags: android, recyclerview, java
Published: 2023-06-02 20:34:26

**RecyclerView** has been an everyday used item in Android App Development. During our use case, we often need some extra twists with the existing RecyclerView. One of them is to have some extra bottom margin for the last element of RecyclerView. Today, we'll try a good solution for that.



## Solution
A good way to make this possible is to use LayoutParams in onBindViewHolder in the adapter class. We can set params for the last item and params for the rest of the items. The last item will have an extra bottom margin to achieve the goal.

## Example Code

``` java
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        if (position == list.size() - 1) {
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
            params.bottomMargin = 150; // last item bottom margin
            holder.itemView.setLayoutParams(params);

        } else {
            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
            params.bottomMargin = 10; // other items bottom margin
            holder.itemView.setLayoutParams(params);
        }

        // all other codes

    }
```

## Explanation
by `position == last.size() - 1` we find out the last item of the list. And with the LayoutParams we set the bottom margin for the last item. And in the else section, we set the bottom margin for the rest of the items.
Now you know how to add an extra bottom margin to the last item to RecyclerView.

Find me on - [Github](https://github.com/ahmmedrejowan), [Facebook](https://www.facebook.com/ahmmedrejowan)

## More

- [Navigating Pages with Ease: Tap Left/Right — A Custom ViewPager in Android](https://blog.rejowan.com/tutorial/code/java/navigating-pages-with-ease-tap-left-right-a-custom-viewpager-in-android/)
- [Making Android Views Draggable: A Practical Guide](https://blog.rejowan.com/tutorial/code/java/making-android-views-draggable-a-practical-guide/)
- [Hide the Soft Keyboard and Remove Focus from EditText in Android](https://blog.rejowan.com/tutorial/code/java/hide-the-soft-keyboard-and-remove-focus-from-edittext-in-android/)
- [Building a Visual Password Strength Meter in Android: A Simple Guide](https://blog.rejowan.com/tutorial/code/java/building-a-visual-password-strength-meter-in-android-a-simple-guide/)
- [Android SearchView OnCloseListener Not Working? Here’s How to Fix It](https://blog.rejowan.com/tutorial/code/java/android-searchview-oncloselistener-not-working-here-is-how-to-fix-it/)
- [CardView vs. MaterialCardView](https://blog.rejowan.com/tutorial/design/cardview-vs-materialcardview/)
- [Android 13’s Restricted Setting: A Stronghold Against Malicious Apps](https://blog.rejowan.com/news/android-news/android-13s-restricted-setting-a-stronghold-against-malicious-apps/)
- [Boost Your Android Development with ADB Idea Plugin](https://blog.rejowan.com/tutorial/android-studio/boost-your-android-development-with-adb-idea-plugin/)
-  [Troubleshooting “Execution Failed for Task app:mergeDebugResources” Error in Android XML Files](https://blog.rejowan.com/qa/android-qa/troubleshooting-execution-failed-for-task-appmergedebugresources-error-in-android-xml-files/)
- [Android Activity Lifecycle: A Comprehensive Guide](https://blog.rejowan.com/tutorial/series/android-basics/android-activity-lifecycle-a-comprehensive-guide/)