Skip to content

Commit

Permalink
Setup fontFamily, Default value enum, text color in attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
unaisulhadi committed Jun 26, 2021
1 parent 21660af commit 07410e0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package com.hadi.emojiratingbar

import android.animation.*
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.annotation.FontRes
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat

@SuppressLint("ResourceType")
class EmojiRatingBar(context: Context, attributeSet: AttributeSet) :
LinearLayout(context, attributeSet) {

Expand Down Expand Up @@ -40,7 +45,8 @@ class EmojiRatingBar(context: Context, attributeSet: AttributeSet) :
private lateinit var tvGreat: TextView

private var showText: Boolean = true

private var color: Int ?= null
private var fontFamilyId = 0

init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
Expand All @@ -53,19 +59,30 @@ class EmojiRatingBar(context: Context, attributeSet: AttributeSet) :

try {
showText = getBoolean(R.styleable.EmojiRatingBar_showText, true)
color = getColor(R.styleable.EmojiRatingBar_titleColor, 0)

rating = RateStatus.values()[getInt(R.styleable.EmojiRatingBar_defaultValue,2)]
fontFamilyId = getResourceId(R.styleable.EmojiRatingBar_android_fontFamily,0)
} finally {
recycle()
}
}
initType()
}



private fun initType() {

binding()
handleRatingClick()

color?.let {
setInitialColor(it)
}
if(fontFamilyId > 0){
setTypeFace(fontFamilyId)
}
setCurrentRateStatus(rating)
if (!showText) {
hideAllTitles()
}
Expand Down Expand Up @@ -240,7 +257,7 @@ class EmojiRatingBar(context: Context, attributeSet: AttributeSet) :

ivAwful.setImageResource(R.drawable.ic_awful_inactive)
ivBad.setImageResource(R.drawable.ic_bad_inactive)
ivOkay.setImageResource(R.drawable.ic_good_inactive)
ivOkay.setImageResource(R.drawable.ic_okay_inactive)
ivGreat.setImageResource(R.drawable.ic_great_inactive)


Expand Down Expand Up @@ -280,11 +297,27 @@ class EmojiRatingBar(context: Context, attributeSet: AttributeSet) :
}

fun setTypeFaceFromAssets(fontPath: String) {
tvAwful.typeface = Typeface.createFromAsset(context.assets,fontPath)
tvBad.typeface = Typeface.createFromAsset(context.assets,fontPath)
tvOkay.typeface = Typeface.createFromAsset(context.assets,fontPath)
tvGood.typeface = Typeface.createFromAsset(context.assets,fontPath)
tvGreat.typeface = Typeface.createFromAsset(context.assets,fontPath)
tvAwful.typeface = Typeface.createFromAsset(context.assets, fontPath)
tvBad.typeface = Typeface.createFromAsset(context.assets, fontPath)
tvOkay.typeface = Typeface.createFromAsset(context.assets, fontPath)
tvGood.typeface = Typeface.createFromAsset(context.assets, fontPath)
tvGreat.typeface = Typeface.createFromAsset(context.assets, fontPath)
}

fun setTitleColor(color: Int) {
tvAwful.setTextColor(ContextCompat.getColor(context,color))
tvBad.setTextColor(ContextCompat.getColor(context,color))
tvOkay.setTextColor(ContextCompat.getColor(context,color))
tvGood.setTextColor(ContextCompat.getColor(context,color))
tvGreat.setTextColor(ContextCompat.getColor(context,color))
}

private fun setInitialColor(color: Int) {
tvAwful.setTextColor(color)
tvBad.setTextColor(color)
tvOkay.setTextColor(color)
tvGood.setTextColor(color)
tvGreat.setTextColor(color)
}

private fun hideAllTitles() {
Expand Down
11 changes: 10 additions & 1 deletion emojiratingbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="EmojiRatingBar">
<attr name="showText" format="boolean"/>
<attr name="showText" format="boolean" />
<attr name="titleColor" format="color" />
<attr name="android:fontFamily"/>
<attr name="defaultValue" format="enum">
<enum name="AWFUL" value="0" />
<enum name="BAD" value="1" />
<enum name="OKAY" value="2" />
<enum name="GOOD" value="3" />
<enum name="GREAT" value="4" />
</attr>
</declare-styleable>
</resources>
2 changes: 2 additions & 0 deletions emojiratingbar/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@

<color name="black">#000000</color>
<color name="white">#FFFFFF</color>

<color name="some_random_color">#FF00FF</color>
</resources>
12 changes: 8 additions & 4 deletions sample/src/main/java/com/hadi/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.hadi.emojiratingbar.EmojiRatingBar
import com.hadi.emojiratingbar.RateStatus

class MainActivity : AppCompatActivity() {

private lateinit var emojiRatingBar: EmojiRatingBar
private lateinit var tvSelectedRate: TextView
private lateinit var btnGetCurrentRate: Button
private lateinit var btnEnableTitle: Button
private lateinit var btnSetFontFromResource: Button
private lateinit var btnSetFontFromAssets: Button
private lateinit var btnChangeTitleColor: Button


override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -25,14 +26,12 @@ class MainActivity : AppCompatActivity() {


emojiRatingBar = findViewById(R.id.emoji_rating_bar)
tvSelectedRate = findViewById(R.id.tv_rate_status)
btnGetCurrentRate = findViewById(R.id.btn_get_rating)
btnEnableTitle = findViewById(R.id.btn_enable_title)
btnSetFontFromResource = findViewById(R.id.btn_set_font_from_res)
btnSetFontFromAssets = findViewById(R.id.btn_set_font_from_asset)
btnChangeTitleColor = findViewById(R.id.btn_change_title_color)


tvSelectedRate.text = emojiRatingBar.getCurrentRateStatus().toString()
btnGetCurrentRate.setOnClickListener {
Toast.makeText(this, "${emojiRatingBar.getCurrentRateStatus()}", Toast.LENGTH_SHORT)
.show()
Expand Down Expand Up @@ -82,6 +81,11 @@ class MainActivity : AppCompatActivity() {
btnSetFontFromAssets.setOnClickListener {
emojiRatingBar.setTypeFaceFromAssets("fonts/kaushanscript_regular.ttf")
}

//Change Color
btnChangeTitleColor.setOnClickListener {
emojiRatingBar.setTitleColor(R.color.black)
}
}

}
24 changes: 14 additions & 10 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">
Expand Down Expand Up @@ -37,6 +37,9 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
app:showText="true"
app:titleColor="#844848"
app:defaultValue="AWFUL"
android:fontFamily="@font/playfair_display_semibold"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="24dp"/>
Expand All @@ -51,19 +54,13 @@
android:layout_marginBottom="12dp"
android:layout_gravity="end"/>

</LinearLayout>


</LinearLayout>

</androidx.cardview.widget.CardView>


<TextView
android:id="@+id/tv_rate_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
/>
</androidx.cardview.widget.CardView>



Expand All @@ -89,4 +86,11 @@
android:layout_marginTop="24dp"
android:text="Set custom font from Resources"/>

<Button
android:id="@+id/btn_change_title_color"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="CHANGE COLOR"/>

</LinearLayout>

0 comments on commit 07410e0

Please sign in to comment.