Skip to content

Commit

Permalink
Added popupText attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
turkergoksu committed Aug 16, 2020
1 parent 89951de commit 02e2580
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">

<com.faskn.lib.ClickablePieChart
android:id="@+id/chart"
android:layout_width="250dp"
app:popupText="ziyaret"
android:layout_centerInParent="true"
android:layout_height="250dp" />
</RelativeLayout>
19 changes: 17 additions & 2 deletions lib/src/main/java/com/faskn/lib/ClickablePieChart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
Expand Down Expand Up @@ -44,13 +43,29 @@ class ClickablePieChart @JvmOverloads constructor(
private var clickListener: ((String, Float) -> Unit)? = null
private var pointsArray = arrayListOf<Pair<Float, Float>>()

// Attributes
private lateinit var popupText: String

init {
slicePaint.isAntiAlias = true
slicePaint.isDither = true
slicePaint.style = Paint.Style.FILL

centerPaint.color = Color.WHITE
centerPaint.style = Paint.Style.FILL

initAttributes(attrs)
}

private fun initAttributes(attrs: AttributeSet?) {
val typedArray =
context.theme.obtainStyledAttributes(attrs, R.styleable.ClickablePieChart, 0, 0)

try {
popupText = typedArray.getString(R.styleable.ClickablePieChart_popupText)!!
} finally {
typedArray.recycle()
}
}

private fun scale(): FloatArray {
Expand Down Expand Up @@ -138,7 +153,7 @@ class ClickablePieChart @JvmOverloads constructor(
var center = pointsArray[index].toList().average()
val halfRadius = rectF!!.centerX()

popupView.findViewById<TextView>(R.id.textViewPopupText).text = "${center.toInt()} ziyaret"
popupView.findViewById<TextView>(R.id.textViewPopupText).text = "${center.toInt()} $popupText"
ImageViewCompat.setImageTintList(
popupView.findViewById<ImageView>(R.id.imageViewPopupCircleIndicator),
ColorStateList.valueOf(ContextCompat.getColor(context, sliceColors[index]))
Expand Down
6 changes: 6 additions & 0 deletions lib/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ClickablePieChart">
<attr name="popupText" format="string"/>
</declare-styleable>
</resources>

0 comments on commit 02e2580

Please sign in to comment.