aboutsummaryrefslogtreecommitdiffstats
path: root/libraries/AndroidBootstrap/src/com/beardedhen/androidbootstrap/BootstrapEditText.java
blob: c258f8a097756b18567e63a4165c2dcf90a44102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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);
	}
	
}