Skip to content

Commit

Permalink
Now text and color attributes can be set directly without using only …
Browse files Browse the repository at this point in the history
…reference types.
  • Loading branch information
hakobast committed May 29, 2018
1 parent e06277e commit 55fdddb
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':dropdown-textview')
}
14 changes: 11 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
app:bg_drawable_expanded="@drawable/bg_first_dropdown_text_view_expanded"
app:bg_drawable_regular="@drawable/bg_first_dropdown_text_view_regular"
app:content_padding="@dimen/drop_down_text_view_content_padding"
app:content_text="@string/dropdown_content_text"
app:content_text_color="@color/first_drop_down_text_view_content"
app:panel_padding="@dimen/drop_down_text_view_panel_padding"
app:title_text_color="@color/first_drop_down_text_view_title"
app:title_text="@string/dropdown_title_text"
app:content_text="@string/dropdown_content_text"/>
app:title_text_color="@color/first_drop_down_text_view_title"/>

<hakobastvatsatryan.DropdownTextView
android:id="@+id/second_dropdown_text_view"
Expand All @@ -33,5 +33,13 @@
app:bg_drawable_regular="@drawable/bg_second_dropdown_text_view_regular"
app:content_text_color="@color/second_drop_down_text_view_content"
app:title_text_color="@color/second_drop_down_text_view_title"/>

<hakobastvatsatryan.DropdownTextView
android:id="@+id/themed_dropdown_text_view"
style="@style/SupportDropdownTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title_text="Themed text"
app:content_text="@string/dropdown_content_text"/>
</LinearLayout>
</ScrollView>
</ScrollView>
11 changes: 11 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
<resources>

<attr name="color_1" format="color"/>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="color_1">#f97a12</item>
</style>

<style name="SupportDropdownTextView">
<item name="arrow_drawable">@drawable/ic_arrow</item>
<item name="bg_drawable_expanded">@drawable/bg_second_dropdown_text_view_expanded</item>
<item name="bg_drawable_regular">@drawable/bg_first_dropdown_text_view_regular</item>
<item name="content_text_color">?attr/color_1</item>
<item name="title_text_color">#ff00ff</item>
</style>

</resources>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
buildscript {
ext.kotlin_version = '1.2.21'
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.android.tools.build:gradle:3.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
Expand Down
6 changes: 3 additions & 3 deletions dropdown-textview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ android {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import android.support.v4.content.res.ResourcesCompat
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.LinearLayout
import android.widget.TextView
import android.os.Bundle
Expand All @@ -27,13 +26,13 @@ class DropdownTextView : LinearLayout {
private lateinit var arrowView: View

private var isExpanded: Boolean = false
private var titleTextRes: Int = -1
private var contentTextRes: Int = -1
private var titleText: String? = null
private var contentText: String? = null
private var expandDuration: Int = -1
private var titleTextColorRes: Int = -1
private var titleTextColor: Int? = null
private var titleTextSizeRes: Int = -1
private var titleFontRes: Int = -1
private var contentTextColorRes: Int = -1
private var contentTextColor: Int? = null
private var contentTextSizeRes: Int = -1
private var contentFontRes: Int = -1
private var arrowDrawableRes: Int = -1
Expand Down Expand Up @@ -115,13 +114,13 @@ class DropdownTextView : LinearLayout {
}

private fun readBuilder(builder: Builder) {
titleTextRes = builder.titleTextRes
titleTextColorRes = builder.titleTextColorRes
titleText = resources.getString(builder.titleTextRes)
titleTextColor = ContextCompat.getColor(context, builder.titleTextColorRes)
titleTextSizeRes = builder.titleTextSizeRes
titleFontRes = builder.titleFontRes

contentTextRes = builder.contentTextRes
contentTextColorRes = builder.contentTextColorRes
contentText = resources.getString(builder.contentTextRes)
contentTextColor = ContextCompat.getColor(context, builder.contentTextColorRes)
contentTextSizeRes = builder.contentTextSizeRes
contentFontRes = builder.contentFontRes

Expand All @@ -143,13 +142,37 @@ class DropdownTextView : LinearLayout {
0, 0
)

titleTextRes = a.getResourceId(R.styleable.DropdownTextView_title_text, -1)
titleTextColorRes = a.getResourceId(R.styleable.DropdownTextView_title_text_color, -1)
val typeValue = TypedValue()

a.getValue(R.styleable.DropdownTextView_title_text, typeValue)
titleText = when(typeValue.type){
TypedValue.TYPE_STRING -> typeValue.string as String?
TypedValue.TYPE_REFERENCE -> resources.getString(typeValue.resourceId)
else -> null
}

a.getValue(R.styleable.DropdownTextView_title_text_color, typeValue)
titleTextColor = when(typeValue.type){
TypedValue.TYPE_REFERENCE -> ContextCompat.getColor(context, typeValue.resourceId)
else -> typeValue.data
}

titleTextSizeRes = a.getResourceId(R.styleable.DropdownTextView_title_text_size, -1)
titleFontRes = a.getResourceId(R.styleable.DropdownTextView_title_font, -1)

contentTextRes = a.getResourceId(R.styleable.DropdownTextView_content_text, -1)
contentTextColorRes = a.getResourceId(R.styleable.DropdownTextView_content_text_color, -1)
a.getValue(R.styleable.DropdownTextView_content_text, typeValue)
contentText = when(typeValue.type){
TypedValue.TYPE_STRING -> typeValue.string as String?
TypedValue.TYPE_REFERENCE -> resources.getString(typeValue.resourceId)
else -> null
}

a.getValue(R.styleable.DropdownTextView_content_text_color, typeValue)
contentTextColor = when(typeValue.type){
TypedValue.TYPE_REFERENCE -> ContextCompat.getColor(context, typeValue.resourceId)
else -> typeValue.data
}

contentTextSizeRes = a.getResourceId(R.styleable.DropdownTextView_content_text_size, -1)
contentFontRes = a.getResourceId(R.styleable.DropdownTextView_content_font, -1)

Expand Down Expand Up @@ -209,12 +232,10 @@ class DropdownTextView : LinearLayout {

private fun setResources() {
arrowView.setBackgroundResource(arrowDrawableRes)
if (titleTextRes != -1) {
titleTextView.setText(titleTextRes)
}
if (titleTextColorRes != -1) {
titleTextView.setTextColor(ContextCompat.getColor(context, titleTextColorRes))
}
titleTextView.text = titleText
contentTextView.text = contentText

titleTextColor?.let { titleTextView.setTextColor(it) }
if (titleTextSizeRes != -1) {
titleTextView.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
Expand All @@ -225,12 +246,7 @@ class DropdownTextView : LinearLayout {
titleTextView.typeface = ResourcesCompat.getFont(context, titleFontRes)
}

if (contentTextRes != -1) {
contentTextView.setText(contentTextRes)
}
if (contentTextColorRes != -1) {
contentTextView.setTextColor(ContextCompat.getColor(context, contentTextColorRes))
}
contentTextColor?.let { contentTextView.setTextColor(it) }
if (contentTextSizeRes != -1) {
contentTextView.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
Expand Down
8 changes: 4 additions & 4 deletions dropdown-textview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<resources>

<declare-styleable name="DropdownTextView">
<attr name="title_text" format="reference"/>
<attr name="content_text" format="reference"/>
<attr name="title_text"/>
<attr name="content_text"/>
<attr name="bg_drawable_regular" format="reference"/>
<attr name="bg_drawable_expanded" format="reference"/>
<attr name="arrow_drawable" format="reference"/>
<attr name="title_text_color" format="color"/>
<attr name="title_text_color"/>
<attr name="title_text_size" format="dimension"/>
<attr name="title_font" format="reference"/>
<attr name="content_text_size" format="dimension"/>
<attr name="content_text_color" format="color"/>
<attr name="content_text_color" />
<attr name="content_font" format="reference"/>
<attr name="expand_duration" format="integer"/>
<attr name="panel_padding" format="dimension"/>
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Feb 11 13:40:18 AMT 2018
#Tue May 29 21:38:50 AMT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

0 comments on commit 55fdddb

Please sign in to comment.