From 83ee5d8c611cb249fb5f140f168680098906a298 Mon Sep 17 00:00:00 2001 From: xynngh Date: Fri, 8 May 2020 15:01:27 +0400 Subject: [PATCH] UI improvements Thanks to an anonymous contributor --- .../CustomVerticalDivider.java | 46 +++++++++++++++++++ .../yetanothercallblocker/MainActivity.java | 1 + app/src/main/res/layout/activity_main.xml | 42 ++++++++--------- app/src/main/res/layout/call_log_item.xml | 23 ++-------- 4 files changed, 71 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/dummydomain/yetanothercallblocker/CustomVerticalDivider.java diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/CustomVerticalDivider.java b/app/src/main/java/dummydomain/yetanothercallblocker/CustomVerticalDivider.java new file mode 100644 index 0000000..a9d2f90 --- /dev/null +++ b/app/src/main/java/dummydomain/yetanothercallblocker/CustomVerticalDivider.java @@ -0,0 +1,46 @@ +package dummydomain.yetanothercallblocker; + +import android.content.Context; +import android.content.res.TypedArray; +import android.graphics.Canvas; +import android.graphics.drawable.Drawable; +import android.view.View; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +/* + * https://stackoverflow.com/a/27037230 + */ +public class CustomVerticalDivider extends RecyclerView.ItemDecoration { + + private static final int[] ATTRS = new int[]{android.R.attr.listDivider}; + + private Drawable mDivider; + + public CustomVerticalDivider(Context context) { + final TypedArray a = context.obtainStyledAttributes(ATTRS); + mDivider = a.getDrawable(0); + a.recycle(); + } + + @Override + public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) { + int dpItemPadding = parent.getResources().getDimensionPixelSize(R.dimen.item_padding); + int left = dpItemPadding; // parent.getPaddingLeft(); + int right = parent.getWidth() - dpItemPadding; // parent.getPaddingRight(); + + int childCount = parent.getChildCount(); + for (int i = 0; i < childCount; i++) { + View child = parent.getChildAt(i); + + RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); + + int top = child.getBottom() + params.bottomMargin; + int bottom = top + mDivider.getIntrinsicHeight(); + + mDivider.setBounds(left, top, right, bottom); + mDivider.draw(c); + } + } +} diff --git a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java index 1ab62f5..4e0897e 100644 --- a/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java +++ b/app/src/main/java/dummydomain/yetanothercallblocker/MainActivity.java @@ -51,6 +51,7 @@ public class MainActivity extends AppCompatActivity { callLogAdapter = new CallLogItemRecyclerViewAdapter(callLogItems, this::onCallLogItemClicked); RecyclerView recyclerView = findViewById(R.id.callLogList); recyclerView.setAdapter(callLogAdapter); + recyclerView.addItemDecoration(new CustomVerticalDivider(this)); } @Override diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 17e9430..825b73c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,29 +7,29 @@ android:orientation="vertical" tools:context=".MainActivity"> - + android:orientation="vertical" + android:padding="@dimen/item_padding"> - + + + + + android:orientation="horizontal" + android:padding="@dimen/item_padding"> @@ -39,10 +30,6 @@ android:id="@+id/numberInfoIcon" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginLeft="6dp" - android:layout_marginTop="8dp" - android:layout_marginRight="1dp" - android:layout_marginBottom="8dp" android:src="@drawable/ic_thumb_down_24dp" />