diff options
author | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-01-09 12:31:45 +0100 |
---|---|---|
committer | Dominik Schürmann <dominik@dominikschuermann.de> | 2014-01-09 12:31:45 +0100 |
commit | 11ed8d7e16abfb98cd96d8a4447382c83435dac8 (patch) | |
tree | 0db8e5a572caf65f0a19da9f26240ccbd993d288 /libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java | |
parent | 14a8738933bd2bb323ee05a47421881337f6e9d7 (diff) | |
download | open-keychain-11ed8d7e16abfb98cd96d8a4447382c83435dac8.tar.gz open-keychain-11ed8d7e16abfb98cd96d8a4447382c83435dac8.tar.bz2 open-keychain-11ed8d7e16abfb98cd96d8a4447382c83435dac8.zip |
add AndroidBootstrap lib
Diffstat (limited to 'libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java')
-rw-r--r-- | libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java b/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java new file mode 100644 index 000000000..c258f8a09 --- /dev/null +++ b/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java @@ -0,0 +1,188 @@ +package com.beardedhen.androidbootstrap; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import android.content.Context; +import android.content.res.TypedArray; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.widget.EditText; + +public class BootstrapEditText extends EditText { + + private boolean roundedCorners = false; + + public BootstrapEditText(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + initialise(attrs); + } + + public BootstrapEditText(Context context, AttributeSet attrs) { + super(context, attrs); + initialise(attrs); + } + + public BootstrapEditText(Context context) { + super(context); + initialise(null); + } + + public static final String BOOTSTRAP_EDIT_TEXT_DEFAULT = "default"; + public static final String BOOTSTRAP_EDIT_TEXT_SUCCESS = "success"; + public static final String BOOTSTRAP_EDIT_TEXT_WARNING = "warning"; + public static final String BOOTSTRAP_EDIT_TEXT_DANGER = "danger"; + + + private void initialise( AttributeSet attrs ) + { + + TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BootstrapEditText); + + //get defaults + float fontSize = 14.0f; + String state = "default"; + String text = ""; + String hint = ""; + boolean enabled = true; + + //font size + if (a.getString(R.styleable.BootstrapEditText_android_textSize) != null) { + + String xmlProvidedSize = attrs.getAttributeValue( "http://schemas.android.com/apk/res/android", "textSize"); + final Pattern PATTERN_FONT_SIZE = Pattern + .compile("([0-9]+[.]?[0-9]*)sp"); + Matcher m = PATTERN_FONT_SIZE.matcher(xmlProvidedSize); + + if (m.find()) { + if (m.groupCount() == 1) { + fontSize = Float.valueOf(m.group(1)); + } + } + } + + //rounded corners + if(a.getString(R.styleable.BootstrapEditText_be_roundedCorners) != null) { + roundedCorners = a.getBoolean(R.styleable.BootstrapEditText_be_roundedCorners, false); + } + + //state + if(a.getString(R.styleable.BootstrapEditText_be_state) != null) { + state = a.getString(R.styleable.BootstrapEditText_be_state); + } + + //text + if(a.getString(R.styleable.BootstrapEditText_android_text) != null) { + text = a.getString(R.styleable.BootstrapEditText_android_text); + } + + //hint + if(a.getString(R.styleable.BootstrapEditText_android_hint) != null) { + hint = a.getString(R.styleable.BootstrapEditText_android_hint); + } + + //enabled + if(a.getString(R.styleable.BootstrapEditText_android_enabled) != null) { + enabled = a.getBoolean(R.styleable.BootstrapEditText_android_enabled, true); + } + + //set values + this.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize); + this.setText(text); + this.setHint(hint); + this.setEnabled(enabled); + + if (enabled){ + //work out the right background + setBackgroundDrawable(state); + + } + + a.recycle(); + + //addView(editTextView); + } + + + private void setBackgroundDrawable(String state) + { + if(roundedCorners){ + this.setBackgroundResource(R.drawable.edittext_background_rounded); + } else { + this.setBackgroundResource(R.drawable.edittext_background); + } + + if(roundedCorners){ + + if (state.equals(BOOTSTRAP_EDIT_TEXT_SUCCESS)){ + this.setBackgroundResource(R.drawable.edittext_background_rounded_success); + } else if (state.equals(BOOTSTRAP_EDIT_TEXT_WARNING)){ + this.setBackgroundResource(R.drawable.edittext_background_rounded_warning); + } else if (state.equals(BOOTSTRAP_EDIT_TEXT_DANGER)){ + this.setBackgroundResource(R.drawable.edittext_background_rounded_danger); + } + + } else { + + if (state.equals(BOOTSTRAP_EDIT_TEXT_SUCCESS)){ + this.setBackgroundResource(R.drawable.edittext_background_success); + } else if (state.equals(BOOTSTRAP_EDIT_TEXT_WARNING)){ + this.setBackgroundResource(R.drawable.edittext_background_warning); + } else if (state.equals(BOOTSTRAP_EDIT_TEXT_DANGER)){ + this.setBackgroundResource(R.drawable.edittext_background_danger); + } + + } + } + + + /** + * Change the BootstrapEditTextState + * @param state + */ + public void setState(String state){ + setBackgroundDrawable(state); + } + + /** + * Set the BootstrapEditText to a successful state + */ + public void setSuccess() + { + setBackgroundDrawable(BOOTSTRAP_EDIT_TEXT_SUCCESS); + } + + /** + * Set the BootstrapEditText to a warning state + */ + public void setWarning() + { + setBackgroundDrawable(BOOTSTRAP_EDIT_TEXT_WARNING); + } + + /** + * Set the BootstrapEditText to a danger state + */ + public void setDanger() + { + setBackgroundDrawable(BOOTSTRAP_EDIT_TEXT_DANGER); + } + + /** + * Set the BootstrapEditText to a default state + */ + public void setDefault() + { + setBackgroundDrawable(BOOTSTRAP_EDIT_TEXT_DEFAULT); + } + + /** + * Specifies whether the BootstrapEditText is enabled or disabled + * @param enabled - boolean state for either enabled or disabled + */ + public void setBootstrapEditTextEnabled(boolean enabled) + { + this.setEnabled(enabled); + } + +} |