From 240bfc9e65a2de0fd280483b9b6c45bc7154a36a Mon Sep 17 00:00:00 2001 From: Mostafa Date: Wed, 9 Oct 2024 20:21:24 +0300 Subject: [PATCH 1/5] Refactor RecyclerView item decoration in AboutFragment and SettingsFragment - Extracted the custom RecyclerView item decoration logic into a separate class called CustomDividerItemDecoration. - Added the CustomDividerItemDecoration to the RecyclerViews in AboutFragment and SettingsFragment. - Applied a custom drawable as the divider with a width of 1dp and rounded corners. - Set the left and right margins of the dividers to 50dp. Fixes #139 --- .../java/be/scri/fragments/AboutFragment.kt | 25 +++++++++++-- .../be/scri/fragments/SettingsFragment.kt | 12 +++++++ .../scri/views/RecyclerViewItemDecoration.kt | 36 +++++++++++++++++++ app/src/main/res/drawable/rv_divider.xml | 7 ++++ .../main/res/layout/card_view_with_image.xml | 14 ++------ app/src/main/res/layout/fragment_about.xml | 11 ++++-- app/src/main/res/layout/fragment_settings.xml | 7 ++-- app/src/main/res/values-night-v31/colors.xml | 2 ++ app/src/main/res/values-v31/colors.xml | 1 + 9 files changed, 98 insertions(+), 17 deletions(-) create mode 100644 app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt create mode 100644 app/src/main/res/drawable/rv_divider.xml diff --git a/app/src/main/java/be/scri/fragments/AboutFragment.kt b/app/src/main/java/be/scri/fragments/AboutFragment.kt index 98a343c9..c32467dd 100644 --- a/app/src/main/java/be/scri/fragments/AboutFragment.kt +++ b/app/src/main/java/be/scri/fragments/AboutFragment.kt @@ -1,13 +1,16 @@ package be.scri.fragments +import CustomDividerItemDecoration import android.content.Intent import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.activity.addCallback +import androidx.appcompat.content.res.AppCompatResources.getDrawable import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import be.scri.BuildConfig import be.scri.R import be.scri.activities.MainActivity @@ -48,16 +51,34 @@ class AboutFragment : Fragment() { recyclerView1.layoutManager = LinearLayoutManager(context) recyclerView1.adapter = CustomAdapter(getFirstRecyclerViewData(), requireContext()) recyclerView1.suppressLayout(true) - + recyclerView1.apply { + addCustomItemDecoration() + } val recyclerView2 = binding.recycleView recyclerView2.layoutManager = LinearLayoutManager(context) recyclerView2.adapter = CustomAdapter(getSecondRecyclerViewData(), requireContext()) recyclerView2.suppressLayout(true) - + recyclerView2.apply { + addCustomItemDecoration() + } val recyclerView3 = binding.recycleView3 recyclerView3.layoutManager = LinearLayoutManager(context) recyclerView3.adapter = CustomAdapter(getThirdRecyclerViewData(), requireContext()) recyclerView3.suppressLayout(true) + recyclerView3.apply { + addCustomItemDecoration() + } + } + + private fun RecyclerView.addCustomItemDecoration() { + val itemDecoration = CustomDividerItemDecoration( + context = requireContext(), + drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, + width = 1, + marginLeft = 50, + marginRight = 50 + ) + addItemDecoration(itemDecoration) } private fun getFirstRecyclerViewData(): List = diff --git a/app/src/main/java/be/scri/fragments/SettingsFragment.kt b/app/src/main/java/be/scri/fragments/SettingsFragment.kt index 5e6daece..f7afc082 100644 --- a/app/src/main/java/be/scri/fragments/SettingsFragment.kt +++ b/app/src/main/java/be/scri/fragments/SettingsFragment.kt @@ -1,5 +1,6 @@ package be.scri.fragments +import CustomDividerItemDecoration import android.content.Context import android.content.Intent import android.content.res.Configuration @@ -16,6 +17,7 @@ import android.view.ViewGroup import android.view.inputmethod.InputMethodManager import androidx.activity.addCallback import androidx.appcompat.app.AppCompatDelegate +import androidx.appcompat.content.res.AppCompatResources.getDrawable import androidx.fragment.app.Fragment import androidx.recyclerview.widget.LinearLayoutManager import be.scri.R @@ -140,6 +142,16 @@ class SettingsFragment : Fragment() { recyclerView.layoutManager = LinearLayoutManager(requireContext()) recyclerView.adapter = adapter recyclerView.suppressLayout(true) + recyclerView.apply { + val itemDecoration = CustomDividerItemDecoration( + context = requireContext(), + drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, + width = 1, + marginLeft = 50, + marginRight = 50 + ) + addItemDecoration(itemDecoration) + } } private fun getRecyclerViewElements(): MutableList { diff --git a/app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt b/app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt new file mode 100644 index 00000000..bef5b177 --- /dev/null +++ b/app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt @@ -0,0 +1,36 @@ +import android.content.Context +import android.graphics.Canvas +import android.graphics.Rect +import android.graphics.drawable.Drawable +import android.view.View +import androidx.annotation.NonNull +import androidx.recyclerview.widget.RecyclerView + +class CustomDividerItemDecoration( + context: Context, + private val drawable: Drawable, + private val width: Int, + private val marginLeft: Int, + private val marginRight: Int +) : RecyclerView.ItemDecoration() { + + override fun onDraw(@NonNull canvas: Canvas, @NonNull parent: RecyclerView, @NonNull state: RecyclerView.State) { + val left = parent.paddingLeft + marginLeft + val right = parent.width - parent.paddingRight - marginRight + + val childCount = parent.childCount + for (i in 0 until childCount - 1) { // Exclude the last item + val child = parent.getChildAt(i) + val params = child.layoutParams as RecyclerView.LayoutParams + val top = child.bottom + params.bottomMargin + val bottom = top + width + + drawable.setBounds(left, top, right, bottom) + drawable.draw(canvas) + } + } + + override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { + outRect.set(0, 0, 0, width) + } +} diff --git a/app/src/main/res/drawable/rv_divider.xml b/app/src/main/res/drawable/rv_divider.xml new file mode 100644 index 00000000..68649987 --- /dev/null +++ b/app/src/main/res/drawable/rv_divider.xml @@ -0,0 +1,7 @@ + + + + + + diff --git a/app/src/main/res/layout/card_view_with_image.xml b/app/src/main/res/layout/card_view_with_image.xml index b28b07a3..b7447138 100644 --- a/app/src/main/res/layout/card_view_with_image.xml +++ b/app/src/main/res/layout/card_view_with_image.xml @@ -1,16 +1,9 @@ - + - diff --git a/app/src/main/res/layout/fragment_about.xml b/app/src/main/res/layout/fragment_about.xml index 2a7d2f0f..3d3b2bc8 100644 --- a/app/src/main/res/layout/fragment_about.xml +++ b/app/src/main/res/layout/fragment_about.xml @@ -27,6 +27,9 @@ android:id="@+id/recycleView2" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" + android:background="@drawable/rounded_all_corners" android:layout_marginTop="10dp" /> - diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 0b561211..d78eb106 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -31,6 +31,7 @@ android:id="@+id/recyclerViewSettings" android:layout_width="match_parent" android:layout_height="wrap_content" + /> - - diff --git a/app/src/main/res/values-night-v31/colors.xml b/app/src/main/res/values-night-v31/colors.xml index 41457397..2a877896 100644 --- a/app/src/main/res/values-night-v31/colors.xml +++ b/app/src/main/res/values-night-v31/colors.xml @@ -15,6 +15,8 @@ @color/dark_tutorial_button_color @color/dark_button_outline_color #D17B0F + #b2aeae + @android:color/system_accent2_800 @android:color/system_accent1_700 diff --git a/app/src/main/res/values-v31/colors.xml b/app/src/main/res/values-v31/colors.xml index 79dd3101..eaef2621 100644 --- a/app/src/main/res/values-v31/colors.xml +++ b/app/src/main/res/values-v31/colors.xml @@ -15,6 +15,7 @@ @color/light_tutorial_button_color @color/light_tutorial_button_color #000000 + #d7d7d9 @android:color/system_accent2_50 From 837f67fbd7a7f27bd18c634e80bf7ebb1e449242 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Thu, 10 Oct 2024 21:06:17 +0300 Subject: [PATCH 2/5] formatting: fix detekt issue --- app/src/main/java/be/scri/fragments/AboutFragment.kt | 1 - app/src/main/java/be/scri/fragments/SettingsFragment.kt | 1 - ...yclerViewItemDecoration.kt => CustomDividerItemDecoration.kt} | 1 - 3 files changed, 3 deletions(-) rename app/src/main/java/be/scri/views/{RecyclerViewItemDecoration.kt => CustomDividerItemDecoration.kt} (98%) diff --git a/app/src/main/java/be/scri/fragments/AboutFragment.kt b/app/src/main/java/be/scri/fragments/AboutFragment.kt index c32467dd..f31ca4fd 100644 --- a/app/src/main/java/be/scri/fragments/AboutFragment.kt +++ b/app/src/main/java/be/scri/fragments/AboutFragment.kt @@ -72,7 +72,6 @@ class AboutFragment : Fragment() { private fun RecyclerView.addCustomItemDecoration() { val itemDecoration = CustomDividerItemDecoration( - context = requireContext(), drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, width = 1, marginLeft = 50, diff --git a/app/src/main/java/be/scri/fragments/SettingsFragment.kt b/app/src/main/java/be/scri/fragments/SettingsFragment.kt index 564353ac..f8a3ee3c 100644 --- a/app/src/main/java/be/scri/fragments/SettingsFragment.kt +++ b/app/src/main/java/be/scri/fragments/SettingsFragment.kt @@ -149,7 +149,6 @@ class SettingsFragment : Fragment() { recyclerView.suppressLayout(true) recyclerView.apply { val itemDecoration = CustomDividerItemDecoration( - context = requireContext(), drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, width = 1, marginLeft = 50, diff --git a/app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt b/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt similarity index 98% rename from app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt rename to app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt index bef5b177..f2ed57e0 100644 --- a/app/src/main/java/be/scri/views/RecyclerViewItemDecoration.kt +++ b/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt @@ -7,7 +7,6 @@ import androidx.annotation.NonNull import androidx.recyclerview.widget.RecyclerView class CustomDividerItemDecoration( - context: Context, private val drawable: Drawable, private val width: Int, private val marginLeft: Int, From 79384bbcc20616c4da13da51aa0e19bdd3813732 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Thu, 10 Oct 2024 21:07:20 +0300 Subject: [PATCH 3/5] formatting: fix klint issues --- .../java/be/scri/fragments/AboutFragment.kt | 13 +++++++------ .../java/be/scri/fragments/SettingsFragment.kt | 13 +++++++------ .../scri/views/CustomDividerItemDecoration.kt | 17 ++++++++++++----- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/be/scri/fragments/AboutFragment.kt b/app/src/main/java/be/scri/fragments/AboutFragment.kt index f31ca4fd..0c40be12 100644 --- a/app/src/main/java/be/scri/fragments/AboutFragment.kt +++ b/app/src/main/java/be/scri/fragments/AboutFragment.kt @@ -71,12 +71,13 @@ class AboutFragment : Fragment() { } private fun RecyclerView.addCustomItemDecoration() { - val itemDecoration = CustomDividerItemDecoration( - drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, - width = 1, - marginLeft = 50, - marginRight = 50 - ) + val itemDecoration = + CustomDividerItemDecoration( + drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, + width = 1, + marginLeft = 50, + marginRight = 50, + ) addItemDecoration(itemDecoration) } diff --git a/app/src/main/java/be/scri/fragments/SettingsFragment.kt b/app/src/main/java/be/scri/fragments/SettingsFragment.kt index f8a3ee3c..8f8a1227 100644 --- a/app/src/main/java/be/scri/fragments/SettingsFragment.kt +++ b/app/src/main/java/be/scri/fragments/SettingsFragment.kt @@ -148,12 +148,13 @@ class SettingsFragment : Fragment() { recyclerView.adapter = adapter recyclerView.suppressLayout(true) recyclerView.apply { - val itemDecoration = CustomDividerItemDecoration( - drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, - width = 1, - marginLeft = 50, - marginRight = 50 - ) + val itemDecoration = + CustomDividerItemDecoration( + drawable = getDrawable(requireContext(), R.drawable.rv_divider)!!, + width = 1, + marginLeft = 50, + marginRight = 50, + ) addItemDecoration(itemDecoration) } } diff --git a/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt b/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt index f2ed57e0..35c80c9a 100644 --- a/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt +++ b/app/src/main/java/be/scri/views/CustomDividerItemDecoration.kt @@ -1,4 +1,3 @@ -import android.content.Context import android.graphics.Canvas import android.graphics.Rect import android.graphics.drawable.Drawable @@ -10,10 +9,13 @@ class CustomDividerItemDecoration( private val drawable: Drawable, private val width: Int, private val marginLeft: Int, - private val marginRight: Int + private val marginRight: Int, ) : RecyclerView.ItemDecoration() { - - override fun onDraw(@NonNull canvas: Canvas, @NonNull parent: RecyclerView, @NonNull state: RecyclerView.State) { + override fun onDraw( + @NonNull canvas: Canvas, + @NonNull parent: RecyclerView, + @NonNull state: RecyclerView.State, + ) { val left = parent.paddingLeft + marginLeft val right = parent.width - parent.paddingRight - marginRight @@ -29,7 +31,12 @@ class CustomDividerItemDecoration( } } - override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) { + override fun getItemOffsets( + outRect: Rect, + view: View, + parent: RecyclerView, + state: RecyclerView.State, + ) { outRect.set(0, 0, 0, width) } } From 6024cdeea5b5615bb528c3af19655217501f14bf Mon Sep 17 00:00:00 2001 From: Mostafa Date: Sun, 13 Oct 2024 18:08:37 +0300 Subject: [PATCH 4/5] fix design mistake in settings page "installed keyboard" --- app/src/main/res/layout/card_view_text.xml | 15 ++++----------- app/src/main/res/layout/card_view_with_switch.xml | 14 +++----------- app/src/main/res/layout/fragment_settings.xml | 3 +++ 3 files changed, 10 insertions(+), 22 deletions(-) diff --git a/app/src/main/res/layout/card_view_text.xml b/app/src/main/res/layout/card_view_text.xml index 68493a60..e517b7c6 100644 --- a/app/src/main/res/layout/card_view_text.xml +++ b/app/src/main/res/layout/card_view_text.xml @@ -1,16 +1,10 @@ - + - diff --git a/app/src/main/res/layout/card_view_with_switch.xml b/app/src/main/res/layout/card_view_with_switch.xml index 7d9980b0..4aad215a 100644 --- a/app/src/main/res/layout/card_view_with_switch.xml +++ b/app/src/main/res/layout/card_view_with_switch.xml @@ -1,16 +1,9 @@ - - diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 759692ac..69917089 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -31,6 +31,9 @@ android:id="@+id/recyclerViewSettings" android:layout_width="match_parent" android:layout_height="wrap_content" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" + android:background="@drawable/rounded_all_corners" /> From 8d2f7005e5f8385105892fdf6bfc0ba5e23590a1 Mon Sep 17 00:00:00 2001 From: Mostafa Date: Sun, 13 Oct 2024 18:50:07 +0300 Subject: [PATCH 5/5] Adjust layout margins in fragment_language_settings.xml --- app/src/main/res/layout/fragment_language_settings.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/res/layout/fragment_language_settings.xml b/app/src/main/res/layout/fragment_language_settings.xml index e8bd3a35..6fd30a2b 100644 --- a/app/src/main/res/layout/fragment_language_settings.xml +++ b/app/src/main/res/layout/fragment_language_settings.xml @@ -29,6 +29,8 @@ android:layout_height="wrap_content" android:layout_marginTop="50dp" android:padding="12dp" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" @@ -61,6 +63,8 @@ android:layout_marginTop="20dp" android:padding="12dp" app:layout_constraintEnd_toEndOf="parent" + android:layout_marginStart="8dp" + android:layout_marginEnd="8dp" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/functionalityRecyclerView" />