Skip to content

Commit

Permalink
Update FormItem drawable setting
Browse files Browse the repository at this point in the history
  • Loading branch information
AmrElmasry committed Aug 22, 2017
1 parent 883f291 commit cc2e79f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ protected void onCreate(Bundle savedInstanceState) {
validator = FormValidator.newInstance(this, new FormValidator.DefaultFormValidator() {
@Override
public void onValidationSucceeded() {
Toast.makeText(MainActivity.this, "Validation Succeeded", Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this, "Validation Succeeded, " + nameFormItem.getContentAsString(),
Toast.LENGTH_SHORT).show();
}
});

Expand Down
14 changes: 6 additions & 8 deletions forms/src/main/java/com/robustastudio/forms/widget/FormItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ open class FormItem @JvmOverloads constructor(context: Context,
context.obtainStyledAttributesSafely(attrs, R.styleable.FormItem) {
val lines = it.getInt(R.styleable.FormItem_android_lines, 1)
val maxLength = it.getInt(R.styleable.FormItem_android_maxLength, -1)
val textSize = it.getFloat(R.styleable.FormItem_android_textSize, 12f)
val textFontPath: String? = it.getString(R.styleable.FormItem_fontPath)
val drawableRes = it.getResourceId(R.styleable.FormItem_drawable, -1)
val inputType = it.getInteger(R.styleable.FormItem_inputType, /* text input type */ 0x1)
val textAsDrawable = it.getString(R.styleable.FormItem_textAsDrawable)

editText.setLines(lines)
editText.textSize = textSize
editText.inputType = inputType
if (isPasswordInputType(inputType)) {
isPasswordVisibilityToggleEnabled = true
Expand All @@ -57,10 +55,10 @@ open class FormItem @JvmOverloads constructor(context: Context,
}
textAsDrawable?.let {
val txtDrawable = TextDrawable(textAsDrawable)
setCompoundDrawablesRelativeWithIntrinsicBounds(null, txtDrawable)
setCompoundDrawablesRelativeWithIntrinsicBounds(txtDrawable)
}
if (drawableRes != -1) {
setCompoundDrawablesRelativeWithIntrinsicBounds(null, drawableRes)
setCompoundDrawablesRelativeWithIntrinsicBounds(drawableRes)
}
if (maxLength != -1) {
editText.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(maxLength))
Expand All @@ -76,12 +74,12 @@ open class FormItem @JvmOverloads constructor(context: Context,

override fun getEditText(): EditText = super.getEditText()!!

fun setCompoundDrawablesRelativeWithIntrinsicBounds(start: Drawable? = null, end: Drawable? = null) {
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(start, null, end, null)
fun setCompoundDrawablesRelativeWithIntrinsicBounds(end: Drawable? = null) {
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(null, null, end, null)
}

fun setCompoundDrawablesRelativeWithIntrinsicBounds(start: Int? = null, end: Int? = null) {
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(start ?: 0, 0, end ?: 0, 0)
fun setCompoundDrawablesRelativeWithIntrinsicBounds(end: Int? = null) {
editText.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, end ?: 0, 0)
}

fun getContentAsString(): String = editText.text.toString().trim()
Expand Down
1 change: 0 additions & 1 deletion forms/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<attr name="android:lines" />
<attr name="android:gravity" />
<attr name="android:maxLength" />
<attr name="android:textSize" />
<attr name="fontPath" format="string" />
<attr name="drawable" format="reference" />
<attr name="textAsDrawable" format="string" />
Expand Down

0 comments on commit cc2e79f

Please sign in to comment.