================================================
FILE: .travis.yml
================================================
language: android
jdk: oraclejdk8
android:
components:
- tools
- platform-tools
- build-tools-27.0.3
- android-27
- extra-google-m2repository
- extra-android-m2repository
script:
- ./gradlew build
licenses:
- 'android-sdk-preview-license-.+'
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'
before_install:
- mkdir "$ANDROID_HOME/licenses" || true
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"
- echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"
install:
# Temporary fix to Android SDK 27
# Ref: https://github.com/travis-ci/travis-ci/issues/8874
- yes | sdkmanager "platforms;android-27"
================================================
FILE: LICENSE
================================================
MIT License
Copyright (c) 2018 Camerash
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
================================================
FILE: README.md
================================================
# ToggleEditTextView :pencil:
Easily switch between EditText and TextView seamlessly.


## Grab via Gradle :coffee:
```groovy
dependencies {
implementation 'com.camerash:toggleedittextview:0.1.1'
}
```
## Usage :computer:
### ToggleEditTextView
In your layout:
```xml
```
Available properties:
* `android:textSize`
* `android:hint`
* `android:inputType`
* `android:minLines`
* `android:maxLines`
* `app:tetv_editing` - Initial state of ToggleEditTextView
* `app:tetv_textViewColor` - Text color of TextView
* `app:tetv_editTextViewColor` - Text color of EditText
* `app:tetv_editTextBottomLineColor` - Bottom line's color of EditText
Available methods:
* `set/getEditing()` - Control editing state of ToggleEditTextView
* `set/getText()` - Set/Get text of ToggleEditTextView
* `set/getHint()` - Set/Get hint of ToggleEditTextView
* `set/getTextSize()` - Set/Get text size of ToggleEditTextView
* `set/getTextViewColor()` - Set/Get color of text of the TextView in ToggleEditTextView
* `set/getEditTextColor()` - Set/Get color of text of the EditText in ToggleEditTextView
* `setEditTextBottomLineColor(Int)` - Set color of bottom line of the EditText in ToggleEditTextView
* `set/getEditTextEnabled()` - Set/Get state of the EditText in ToggleEditTextView
* `set/getInputType()` - Set/Get inputType of the EditText in ToggleEditTextView
* `setMinLines()` - Set minLines of ToggleEditTextView
* `setMaxLines()` - Set maxLines of ToggleEditTextView
---
### ToggleEditButton - Button controller of ToggleEditTextViews
In your layout:
```xml
```
Available properties:
* `app:teb_edit` - Initial state of ToggleEditButton (Overrides the state of the controlling ToggleEditTextViews)
* `app:teb_tint` - Tint color of the icon
* `app:teb_animationOffset` - Offset duration of fading animation between each ToggleEditTextView controlled by the ToggleEditButton
Available methods:
* `bind(vararg ToggleEditTextView)` - Bind multiple ToggleEditTextViews to the button
* `unbind(ToggleEditTextView)` - Unbind ToggleEditTextView from the button
* `unbindAll()` - Unbind all ToggleEditTextViews from the button
* `set/getAnimationOffset()` - Set/Get the Offset duration of fading animation between each ToggleEditTextView controlled by the ToggleEditButton
* `setOnClickListener(OnClickListener)` - Works the same as a good o' button
## Common usage :bell:
Kotlin:
```kotlin
val tetv1 = findViewById(R.id.tetv1)
val tetv2 = findViewById(R.id.tetv2)
val teb = findViewById(R.id.teb)
teb.bind(tetv1, tetv2)
```
Java:
```java
ToggleEditTextView tetv1 = findViewById(R.id.tetv1);
ToggleEditTextView tetv2 = findViewById(R.id.tetv2);
ToggleEditButton teb = findViewById(R.id.teb);
teb.bind(tetv1, tetv2);
```
## Sample :closed_book:
Sample app is under `/sample` directory :tada:
## License :page_with_curl:
```
MIT License
Copyright (c) 2018 Camerash
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
```
================================================
FILE: build.gradle
================================================
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
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.6'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
================================================
FILE: gradle/wrapper/gradle-wrapper.properties
================================================
#Thu Apr 26 11:22:30 HKT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
================================================
FILE: gradle.properties
================================================
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
================================================
FILE: gradlew
================================================
#!/usr/bin/env bash
##############################################################################
##
## Gradle start up script for UN*X
##
##############################################################################
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`
# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
warn ( ) {
echo "$*"
}
die ( ) {
echo
echo "$*"
echo
exit 1
}
# OS specific support (must be 'true' or 'false').
cygwin=false
msys=false
darwin=false
case "`uname`" in
CYGWIN* )
cygwin=true
;;
Darwin* )
darwin=true
;;
MINGW* )
msys=true
;;
esac
# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
# Determine the Java command to use to start the JVM.
if [ -n "$JAVA_HOME" ] ; then
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
# IBM's JDK on AIX uses strange locations for the executables
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
if [ ! -x "$JAVACMD" ] ; then
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
MAX_FD="$MAX_FD_LIMIT"
fi
ulimit -n $MAX_FD
if [ $? -ne 0 ] ; then
warn "Could not set maximum file descriptor limit: $MAX_FD"
fi
else
warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
fi
fi
# For Darwin, add options to specify how the application appears in the dock
if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi
# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
SEP=""
for dir in $ROOTDIRSRAW ; do
ROOTDIRS="$ROOTDIRS$SEP$dir"
SEP="|"
done
OURCYGPATTERN="(^($ROOTDIRS))"
# Add a user-defined pattern to the cygpath arguments
if [ "$GRADLE_CYGPATTERN" != "" ] ; then
OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
fi
# Now convert the arguments - kludge to limit ourselves to /bin/sh
i=0
for arg in "$@" ; do
CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
function splitJvmOpts() {
JVM_OPTS=("$@")
}
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
================================================
FILE: gradlew.bat
================================================
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem Gradle startup script for Windows
@rem
@rem ##########################################################################
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome
set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
if exist "%JAVA_EXE%" goto init
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
goto fail
:init
@rem Get command-line arguments, handling Windowz variants
if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2
:win9xME_args_slurp
if "x%~1" == "x" goto execute
set CMD_LINE_ARGS=%*
goto execute
:4NT_args
@rem Get arguments from the 4NT Shell from JP Software
set CMD_LINE_ARGS=%$
:execute
@rem Setup the command line
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd
:fail
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
exit /b 1
:mainEnd
if "%OS%"=="Windows_NT" endlocal
:omega
================================================
FILE: sample/.gitignore
================================================
/build
================================================
FILE: sample/build.gradle
================================================
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.camerash.toggleedittextview.sample"
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
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.2'
implementation project(':toggleedittextview')
}
================================================
FILE: sample/proguard-rules.pro
================================================
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
================================================
FILE: sample/src/main/AndroidManifest.xml
================================================
================================================
FILE: sample/src/main/java/com/camerash/toggleedittextview/sample/MainActivity.kt
================================================
package com.camerash.toggleedittextview.sample
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initBinding()
}
private fun initBinding() {
toggleEditButton.bind(tetv1, tetv2, tetv3, tetv4)
}
}
================================================
FILE: sample/src/main/res/drawable/ic_launcher_background.xml
================================================
================================================
FILE: sample/src/main/res/drawable-v24/ic_launcher_foreground.xml
================================================
================================================
FILE: sample/src/main/res/layout/activity_main.xml
================================================
================================================
FILE: sample/src/main/res/mipmap-anydpi-v26/ic_launcher.xml
================================================
================================================
FILE: sample/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml
================================================
================================================
FILE: sample/src/main/res/values/colors.xml
================================================
#0277bd#004c8c#439889
================================================
FILE: sample/src/main/res/values/strings.xml
================================================
ToggleEditTextViewNameAgeHobbiesAddress
================================================
FILE: sample/src/main/res/values/styles.xml
================================================
================================================
FILE: settings.gradle
================================================
include ':sample', ':toggleedittextview'
================================================
FILE: toggleedittextview/.gitignore
================================================
/build
================================================
FILE: toggleedittextview/build.gradle
================================================
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'com.github.dcendents.android-maven'
def VERSION = '0.1.1'
group = 'com.camerash'
version = VERSION
def bintrayUser = System.getenv('BINTRAY_USER')
def bintrayApiKey = System.getenv('BINTRAY_API_KEY')
if (project.rootProject.file('local.properties').exists()) {
Properties localProperties = new Properties()
localProperties.load(project.rootProject.file('local.properties').newDataInputStream())
bintrayUser = localProperties.getProperty('bintray.user', bintrayUser)
bintrayApiKey = localProperties.getProperty('bintray.apikey', bintrayApiKey)
}
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName VERSION
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation "com.android.support:design:27.1.1"
}
repositories {
mavenCentral()
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
task javadoc(type: Javadoc) {
excludes = ['**/*.kt'] // exclude kotlin files
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives javadocJar
archives sourcesJar
}
bintray {
user = bintrayUser
key = bintrayApiKey
configurations = ['archives']
pkg {
repo = 'maven'
name = 'ToggleEditTextView'
desc = 'Easily switch between EditText and TextView seamlessly.'
websiteUrl = 'https://github.com/Camerash/ToggleEditTextView'
vcsUrl = 'https://github.com/Camerash/ToggleEditTextView.git'
licenses = ['MIT']
publish = true
publicDownloadNumbers = true
version {
name = VERSION
desc = "ToggleEditTextView ${VERSION}"
released = new Date()
vcsTag = VERSION
}
}
}
install {
repositories.mavenInstaller {
pom {
project {
packaging 'aar'
groupId this.group
artifactId 'toggleedittextview'
name 'toggleedittextview'
description 'Easily switch between EditText and TextView seamlessly.'
url 'https://github.com/Camerash/ToggleEditTextView'
version = this.version
licenses {
license {
name 'The MIT License'
url 'http://opensource.org/licenses/MIT'
}
}
developers {
developer {
id 'camerash'
name 'Esmond Wong'
email 'esmond9981127@gmail.com'
}
}
scm {
connection 'https://github.com/Camerash/ToggleEditTextView.git'
developerConnection 'https://github.com/Camerash/ToggleEditTextView.git'
url 'https://github.com/Camerash/ToggleEditTextView'
}
}
}
}
}
================================================
FILE: toggleedittextview/proguard-rules.pro
================================================
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
================================================
FILE: toggleedittextview/src/main/AndroidManifest.xml
================================================
================================================
FILE: toggleedittextview/src/main/java/com/camerash/toggleedittextview/ToggleEditButton.kt
================================================
package com.camerash.toggleedittextview
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.os.Bundle
import android.os.Handler
import android.os.Parcelable
import android.support.graphics.drawable.AnimatedVectorDrawableCompat
import android.support.v7.widget.AppCompatImageButton
import android.util.AttributeSet
/**
* Created by camerash on 4/26/18.
* Controller button for controlling single / grouped ToggleEditTextView(s)
*/
class ToggleEditButton(context: Context, attrs: AttributeSet?, defStyleAttr: Int): AppCompatImageButton(context, attrs, defStyleAttr) {
private val tetvArrayList = arrayListOf()
private val editToConfirmAnim = AnimatedVectorDrawableCompat.create(context, R.drawable.edit_to_confirm_anim)
private val confirmToEditAnim = AnimatedVectorDrawableCompat.create(context, R.drawable.confirm_to_edit_anim)
private var onClickListener: OnClickListener? = null
private var editing = false
private var animationOffset = 100L
constructor(context: Context): this(context, null, 0)
constructor(context: Context, attrs: AttributeSet): this(context, attrs, 0)
init {
if(attrs != null) {
val styled = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleEditButton)
initAttributes(styled)
styled.recycle()
}
super.setOnClickListener {
setEditing(!editing, true)
onClickListener?.onClick(it)
}
}
private fun initAttributes(styled: TypedArray) {
val tint = styled.getColor(R.styleable.ToggleEditButton_teb_tint, Color.BLACK)
setTint(tint)
val editing = styled.getBoolean(R.styleable.ToggleEditButton_teb_edit, false)
setEditing(editing, false)
val offset = styled.getInteger(R.styleable.ToggleEditButton_teb_animationOffset, 100).toLong()
setAnimationOffset(offset)
}
private fun resetButton(animate: Boolean) {
editToConfirmAnim?.stop()
confirmToEditAnim?.stop()
if(animate) {
setImageDrawable(if (this.editing) editToConfirmAnim else confirmToEditAnim)
if (this.editing) editToConfirmAnim?.start() else confirmToEditAnim?.start()
} else {
setImageDrawable(if (this.editing) confirmToEditAnim else editToConfirmAnim)
}
}
fun getEditing(): Boolean = this.editing
fun setEditing(editing: Boolean, animate: Boolean) {
this.editing = editing
if(animate) {
tetvArrayList.forEachIndexed { i, tetv ->
tetv.setEditTextEnabled(this.editing)
Handler().postDelayed({ tetv.setEditing(this.editing, animate) }, animationOffset*i)
}
resetButton(animate)
} else {
tetvArrayList.forEach { it.setEditing(this.editing, animate) }
resetButton(animate)
}
}
fun setTint(color: Int) {
editToConfirmAnim?.setTint(color)
confirmToEditAnim?.setTint(color)
}
fun bind(vararg toggleEditTextView: ToggleEditTextView) {
tetvArrayList.addAll(toggleEditTextView)
tetvArrayList.forEach { it.setEditing(this.editing, false) }
}
fun unbind(toggleEditTextView: ToggleEditTextView) {
tetvArrayList.remove(toggleEditTextView)
}
fun unbindAll() {
tetvArrayList.clear()
}
fun getAnimationOffset(): Long = animationOffset
fun setAnimationOffset(offset: Long) {
this.animationOffset = offset
}
override fun setOnClickListener(l: OnClickListener?) {
this.onClickListener = l
}
override fun onSaveInstanceState(): Parcelable {
val bundle = Bundle()
bundle.putParcelable(SUPER_STATE_KEY, super.onSaveInstanceState())
bundle.putBoolean(EDIT_KEY, getEditing())
return bundle
}
override fun onRestoreInstanceState(state: Parcelable) {
var superState = state
if (state is Bundle) {
superState = state.getParcelable(SUPER_STATE_KEY)
setEditing(state.getBoolean(EDIT_KEY),false)
}
super.onRestoreInstanceState(superState)
}
companion object {
const val SUPER_STATE_KEY = "super_state"
const val EDIT_KEY = "edit"
}
}
================================================
FILE: toggleedittextview/src/main/java/com/camerash/toggleedittextview/ToggleEditTextView.kt
================================================
package com.camerash.toggleedittextview
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Color
import android.graphics.PorterDuff
import android.os.Bundle
import android.os.Parcelable
import android.support.transition.Fade
import android.support.transition.TransitionManager
import android.support.transition.TransitionSet
import android.text.InputType
import android.util.AttributeSet
import android.util.SparseArray
import android.util.TypedValue
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
/**
* Created by camerash on 4/26/18.
* Compound View to switch between Edit Text and Text View
*/
class ToggleEditTextView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : FrameLayout(context, attrs, defStyleAttr) {
constructor(context: Context) : this(context, null, 0)
constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)
var textView: TextView
private set
var editText: EditText
private set
private var editing = false
init {
inflate(getContext(), R.layout.view_toggle_edit_text, this)
textView = findViewById(R.id.text_view)
editText = findViewById(R.id.edit_text)
if (attrs != null) {
val styled = getContext().obtainStyledAttributes(attrs, R.styleable.ToggleEditTextView)
initAttributes(styled)
styled.recycle()
}
}
private fun initAttributes(styled: TypedArray) {
editing = styled.getBoolean(R.styleable.ToggleEditTextView_tetv_editing, false)
textView.visibility = if (editing) View.GONE else View.VISIBLE
editText.visibility = if (editing) View.VISIBLE else View.GONE
val textViewColor = styled.getColor(R.styleable.ToggleEditTextView_tetv_textViewColor, Color.BLACK)
setTextViewColor(textViewColor)
val editTextColor = styled.getColor(R.styleable.ToggleEditTextView_tetv_editTextViewColor, Color.BLACK)
setEditTextColor(editTextColor)
val editTextBottomLineColor = styled.getColor(R.styleable.ToggleEditTextView_tetv_editTextBottomLineColor, editTextColor)
setEditTextBottomLineColor(editTextBottomLineColor)
val size = styled.getDimension(R.styleable.ToggleEditTextView_android_textSize, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18f, context.resources.displayMetrics))
setTextSize(size)
val hint = styled.getString(R.styleable.ToggleEditTextView_android_hint)
if(hint != null) setHint(hint)
val inputType = styled.getInteger(R.styleable.ToggleEditTextView_android_inputType, InputType.TYPE_CLASS_TEXT)
setInputType(inputType)
val minLines = styled.getInteger(R.styleable.ToggleEditTextView_android_minLines, 0)
setMinLines(minLines)
val maxLines = styled.getInteger(R.styleable.ToggleEditTextView_android_maxLines, Integer.MAX_VALUE)
setMaxLines(maxLines)
}
private fun generateTransitionSet(editing: Boolean): TransitionSet {
val transitionSet = TransitionSet()
val textViewFade = Fade()
textViewFade.addTarget(textView)
val editTextFade = Fade()
editTextFade.addTarget(editText)
if(editing) textViewFade.startDelay = FADE_DELAY else editTextFade.startDelay = FADE_DELAY
return transitionSet.addTransition(textViewFade).addTransition(editTextFade)
}
fun setEditing(editing: Boolean, animate: Boolean) {
if (editing != this.editing) {
this.editing = editing
if (!this.editing) textView.text = editText.text // Set editText text to textView
if(animate) TransitionManager.beginDelayedTransition(this, generateTransitionSet(this.editing))
textView.visibility = if (this.editing) View.GONE else View.VISIBLE
editText.visibility = if (this.editing) View.VISIBLE else View.GONE
editText.isEnabled = this.editing
}
}
fun getEditing(): Boolean = this.editing
fun getText(): String = editText.text.toString()
fun setText(text: String) {
textView.text = text
editText.setText(text)
}
fun getHint(): String = editText.hint.toString()
fun setHint(hint: String) {
textView.hint = hint
editText.hint = hint
}
fun getTextSize(): Float = editText.textSize
fun setTextSize(size: Float) {
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size)
}
fun getTextViewColor(): Int = textView.textColors.defaultColor
fun setTextViewColor(color: Int) {
textView.setTextColor(color)
}
fun getEditTextColor(): Int = editText.textColors.defaultColor
fun setEditTextColor(color: Int) {
editText.setTextColor(color)
}
fun getEditTextEnabled(): Boolean = editText.isEnabled
fun setEditTextEnabled(enable: Boolean) {
editText.isEnabled = enable
}
fun getInputType(): Int = editText.inputType
fun setInputType(type: Int) {
editText.inputType = type
}
fun setMinLines(minLines: Int) {
textView.minLines = minLines
editText.minLines = minLines
}
fun setMaxLines(maxLines: Int) {
textView.maxLines = maxLines
editText.maxLines = maxLines
}
fun setEditTextBottomLineColor(color: Int) {
editText.background.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
}
override fun onSaveInstanceState(): Parcelable {
val bundle = Bundle()
bundle.putParcelable(SUPER_STATE_KEY, super.onSaveInstanceState())
bundle.putString(TEXT_KEY, getText())
bundle.putBoolean(EDIT_KEY, getEditing())
return bundle
}
override fun onRestoreInstanceState(state: Parcelable) {
var superState = state
if (state is Bundle) {
superState = state.getParcelable(SUPER_STATE_KEY)
setEditing(state.getBoolean(EDIT_KEY),false)
setText(state.getString(TEXT_KEY))
}
super.onRestoreInstanceState(superState)
}
override fun dispatchSaveInstanceState(container: SparseArray) {
super.dispatchFreezeSelfOnly(container)
}
override fun dispatchRestoreInstanceState(container: SparseArray) {
super.dispatchThawSelfOnly(container)
}
companion object {
const val FADE_DELAY = 50L
const val SUPER_STATE_KEY = "super_state"
const val TEXT_KEY = "text"
const val EDIT_KEY = "edit"
}
}
================================================
FILE: toggleedittextview/src/main/res/drawable/confirm_to_edit_anim.xml
================================================
================================================
FILE: toggleedittextview/src/main/res/drawable/edit_to_confirm_anim.xml
================================================
================================================
FILE: toggleedittextview/src/main/res/layout/view_toggle_edit_text.xml
================================================
================================================
FILE: toggleedittextview/src/main/res/values/attrs.xml
================================================
================================================
FILE: toggleedittextview/src/main/res/values/strings.xml
================================================
ToggleEditTextView