-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathBootstrapEditText.java
More file actions
172 lines (138 loc) · 6.1 KB
/
BootstrapEditText.java
File metadata and controls
172 lines (138 loc) · 6.1 KB
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
package com.beardedhen.androidbootstrap;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.EditText;
import com.beardedhen.androidbootstrap.api.attributes.BootstrapBrand;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.androidbootstrap.api.defaults.DefaultBootstrapSize;
import com.beardedhen.androidbootstrap.api.view.BootstrapBrandView;
import com.beardedhen.androidbootstrap.api.view.BootstrapSizeView;
import com.beardedhen.androidbootstrap.api.view.RoundableView;
import com.beardedhen.androidbootstrap.utils.DimenUtils;
import com.beardedhen.androidbootstrap.utils.ViewUtils;
import java.io.Serializable;
/**
* BootstrapEditText allows users to enter values like a regular Android EditText, and allows coloring
* via BootstrapBrand, and rounding of its background.
*/
public class BootstrapEditText extends EditText implements BootstrapBrandView, RoundableView,
BootstrapSizeView {
private static final String TAG = "com.beardedhen.androidbootstrap.BootstrapEditText";
private float baselineFontSize;
private float baselineVertPadding;
private float baselineHoriPadding;
private float baselineStrokeWidth;
private float baselineCornerRadius;
private BootstrapBrand bootstrapBrand;
private float bootstrapSize;
private boolean rounded;
public BootstrapEditText(Context context) {
super(context);
initialise(null);
}
public BootstrapEditText(Context context, AttributeSet attrs) {
super(context, attrs);
initialise(attrs);
}
public BootstrapEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialise(attrs);
}
private void initialise(AttributeSet attrs) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.BootstrapEditText);
try {
this.rounded = a.getBoolean(R.styleable.BootstrapEditText_roundedCorners, false);
int typeOrdinal = a.getInt(R.styleable.BootstrapEditText_bootstrapBrand, -1);
int sizeOrdinal = a.getInt(R.styleable.BootstrapEditText_bootstrapSize, -1);
this.bootstrapBrand = DefaultBootstrapBrand.fromAttributeValue(typeOrdinal);
this.bootstrapSize = DefaultBootstrapSize.fromAttributeValue(sizeOrdinal).scaleFactor();
}
finally {
a.recycle();
}
baselineFontSize = DimenUtils.pixelsFromSpResource(getContext(), R.dimen.bootstrap_edit_text_default_font_size);
baselineVertPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_edit_text_vert_padding);
baselineHoriPadding = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_edit_text_hori_padding);
baselineStrokeWidth = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_edit_text_edge_width);
baselineCornerRadius = DimenUtils.pixelsFromDpResource(getContext(), R.dimen.bootstrap_edit_text_corner_radius);
a = getContext().obtainStyledAttributes(attrs, new int[] {android.R.attr.gravity});
try {
setGravity(a.getInt(0, Gravity.CENTER_VERTICAL)); // center text vertically by default
} finally {
a.recycle();
}
updateBootstrapState();
invalidate();
}
@Override public Parcelable onSaveInstanceState() {
Bundle bundle = new Bundle();
bundle.putParcelable(TAG, super.onSaveInstanceState());
bundle.putBoolean(RoundableView.KEY, rounded);
bundle.putFloat(BootstrapSizeView.KEY, bootstrapSize);
bundle.putSerializable(BootstrapBrand.KEY, bootstrapBrand);
return bundle;
}
@Override public void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
Bundle bundle = (Bundle) state;
this.rounded = bundle.getBoolean(RoundableView.KEY);
this.bootstrapSize = bundle.getFloat(BootstrapSizeView.KEY);
Serializable brand = bundle.getSerializable(BootstrapBrand.KEY);
if (brand instanceof BootstrapBrand) {
bootstrapBrand = (BootstrapBrand) brand;
}
state = bundle.getParcelable(TAG);
}
super.onRestoreInstanceState(state);
updateBootstrapState();
}
private void updateBootstrapState() {
int vPadding = (int) (baselineVertPadding * bootstrapSize);
int hPadding = (int) (baselineHoriPadding * bootstrapSize);
setPadding(vPadding, hPadding, vPadding, hPadding);
int strokeWidth = (int) (baselineStrokeWidth * bootstrapSize);
float cornerRadius = baselineCornerRadius * bootstrapSize;
final float fontSize = baselineFontSize * bootstrapSize;
setTextSize(fontSize);
Drawable bg = BootstrapDrawableFactory.bootstrapEditText(
getContext(),
bootstrapBrand,
strokeWidth,
cornerRadius,
rounded);
ViewUtils.setBackgroundDrawable(this, bg);
}
/*
* Getters/Setters
*/
@Override public void setBootstrapBrand(@NonNull BootstrapBrand bootstrapBrand) {
this.bootstrapBrand = bootstrapBrand;
updateBootstrapState();
}
@NonNull @Override public BootstrapBrand getBootstrapBrand() {
return bootstrapBrand;
}
@Override public void setRounded(boolean rounded) {
this.rounded = rounded;
updateBootstrapState();
}
@Override public boolean isRounded() {
return rounded;
}
@Override public float getBootstrapSize() {
return bootstrapSize;
}
@Override public void setBootstrapSize(float bootstrapSize) {
this.bootstrapSize = bootstrapSize;
updateBootstrapState();
}
@Override public void setBootstrapSize(DefaultBootstrapSize bootstrapSize) {
setBootstrapSize(bootstrapSize.scaleFactor());
}
}