Skip to content

Commit 964b8c8

Browse files
committed
Add styleable attributes for setting padding by percentage and dp
1 parent 187ee8f commit 964b8c8

File tree

4 files changed

+43
-13
lines changed

4 files changed

+43
-13
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,22 @@ private fun initDeck() {
3131

3232
or set padding based on percentage of the screen width
3333

34-
```kotlin
35-
deckPager.setPercentagePadding(activity)
34+
```
35+
// in kotlin
36+
deckPager.setPercentagePadding(activity, 8)
37+
38+
// in XML
39+
app:padding_percentage="8"
3640
```
3741

3842
or set padding based on dp value
3943

40-
```kotlin
41-
deckPager.setDpPadding(activity)
44+
```
45+
// in kotlin
46+
deckPager.setDpPadding(activity, 16)
47+
48+
// in XML
49+
app:padding_dp="16dp"
4250
```
4351

4452
##### Remember you can set all other view pager properties with deck
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
34
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
@@ -9,6 +10,7 @@
910
android:id="@+id/deck_pager"
1011
android:layout_width="match_parent"
1112
android:layout_height="400dp"
12-
android:layout_centerInParent="true"/>
13+
android:layout_centerInParent="true"
14+
/>
1315

1416
</RelativeLayout>

lib/src/main/java/com/example/lib/Deck.kt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.example.lib
22

33
import android.app.Activity
44
import android.content.Context
5+
import android.content.res.TypedArray
56
import android.support.v4.view.ViewPager
67
import android.util.AttributeSet
78
import android.util.DisplayMetrics
@@ -17,7 +18,19 @@ class Deck : ViewPager {
1718
private val pageTransformer = CoverFlowTransformer()
1819

1920
constructor(context: Context) : super(context)
20-
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
21+
22+
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
23+
val typedArray: TypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.Deck)
24+
val percentagePaddingXml = typedArray.getInt(R.styleable.Deck_padding_percentage, Integer.MAX_VALUE)
25+
if (percentagePaddingXml != Integer.MAX_VALUE) {
26+
setPercentagePadding(context, percentagePaddingXml)
27+
}
28+
val dipPaddingXmlInPixel = typedArray.getDimensionPixelSize(R.styleable.Deck_padding_dp, Integer.MAX_VALUE)
29+
if (dipPaddingXmlInPixel != Integer.MAX_VALUE) {
30+
initProperties(context, dipPaddingXmlInPixel.toFloat())
31+
}
32+
typedArray.recycle()
33+
}
2134

2235
init {
2336
initView()
@@ -30,22 +43,22 @@ class Deck : ViewPager {
3043
/**
3144
* Set left and right padding with default value
3245
*/
33-
fun setDefaultPadding(context: Activity) {
46+
fun setDefaultPadding(context: Context) {
3447
setPercentagePadding(context, DEFAULT_PERCENTAGE_PADDING)
3548
}
3649

3750
/**
3851
* Set left and right padding based on percentage of the screen width
3952
* If the percentage is more than 18, the left and right items height might not be consistent
4053
*/
41-
fun setPercentagePadding(context: Activity, percentage: Int) {
54+
fun setPercentagePadding(context: Context, percentage: Int) {
4255
when {
4356
percentage == 0 -> initProperties(context, 0f)
4457
percentage < 0 -> throw IllegalArgumentException("Percentage can't be lower than 0")
4558
percentage > 100 -> throw IllegalArgumentException("Percentage can't be higher than 100")
4659
else -> {
4760
val metrics = DisplayMetrics()
48-
context.windowManager.defaultDisplay.getMetrics(metrics)
61+
(context as Activity).windowManager.defaultDisplay.getMetrics(metrics)
4962
val padding = metrics.widthPixels * percentage / 100f
5063
initProperties(context, padding)
5164
}
@@ -55,7 +68,7 @@ class Deck : ViewPager {
5568
/**
5669
* Set left and right padding based on dp value
5770
*/
58-
fun setDpPadding(context: Activity, dp: Float) {
71+
fun setDpPadding(context: Context, dp: Float) {
5972
val padding = TypedValue.applyDimension(
6073
TypedValue.COMPLEX_UNIT_DIP,
6174
dp,
@@ -64,14 +77,14 @@ class Deck : ViewPager {
6477
initProperties(context, padding)
6578
}
6679

67-
private fun initProperties(context: Activity, padding: Float) {
80+
private fun initProperties(context: Context, padding: Float) {
6881
val intPadding = padding.toInt()
6982
setPadding(intPadding, 0, intPadding, 0)
7083
clipToPadding = false
7184
pageMargin = 0
7285

7386
val metrics = DisplayMetrics()
74-
context.windowManager.defaultDisplay.getMetrics(metrics)
87+
(context as Activity).windowManager.defaultDisplay.getMetrics(metrics)
7588
pageTransformer.paddingFactor = padding / metrics.widthPixels
7689
}
7790
}

lib/src/main/res/values/attrs.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<declare-styleable name="Deck">
4+
<attr name="padding_percentage" format="integer"/>
5+
<attr name="padding_dp" format="dimension"/>
6+
</declare-styleable>
7+
</resources>

0 commit comments

Comments
 (0)