From 55fdddb73b66183355f81913b03a97ab0202403a Mon Sep 17 00:00:00 2001 From: Hakob Astvatsatryan Date: Tue, 29 May 2018 22:10:21 +0400 Subject: [PATCH] Now text and color attributes can be set directly without using only reference types. --- .idea/misc.xml | 2 +- app/build.gradle | 10 +-- app/src/main/res/layout/activity_main.xml | 14 +++- app/src/main/res/values/styles.xml | 11 ++++ build.gradle | 4 +- dropdown-textview/build.gradle | 6 +- .../hakobastvatsatryan/DropdownTextView.kt | 66 ++++++++++++------- .../src/main/res/values/attrs.xml | 8 +-- gradle/wrapper/gradle-wrapper.properties | 4 +- 9 files changed, 80 insertions(+), 45 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 75dac50..3963879 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -24,7 +24,7 @@ - + diff --git a/app/build.gradle b/app/build.gradle index 594e709..639545c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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') } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index ce984a1..fe26571 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -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"/> + + - + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930..c57266c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,11 +1,22 @@ + + + + diff --git a/build.gradle b/build.gradle index 6ea009e..f69a655 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/dropdown-textview/build.gradle b/dropdown-textview/build.gradle index ef0eeb9..bd01f09 100644 --- a/dropdown-textview/build.gradle +++ b/dropdown-textview/build.gradle @@ -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" } diff --git a/dropdown-textview/src/main/java/hakobastvatsatryan/DropdownTextView.kt b/dropdown-textview/src/main/java/hakobastvatsatryan/DropdownTextView.kt index 4db6299..33672b2 100644 --- a/dropdown-textview/src/main/java/hakobastvatsatryan/DropdownTextView.kt +++ b/dropdown-textview/src/main/java/hakobastvatsatryan/DropdownTextView.kt @@ -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 @@ -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 @@ -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 @@ -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) @@ -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, @@ -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, diff --git a/dropdown-textview/src/main/res/values/attrs.xml b/dropdown-textview/src/main/res/values/attrs.xml index 7b67bb2..e90371f 100644 --- a/dropdown-textview/src/main/res/values/attrs.xml +++ b/dropdown-textview/src/main/res/values/attrs.xml @@ -2,16 +2,16 @@ - - + + - + - + diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index c7aad50..8aa8c5d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -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