Skip to content

Commit 503f3c5

Browse files
committed
Merge pull request navasmdc#213 from akiwarheit/master
Adding text view beside check box
2 parents 414a969 + 353418c commit 503f3c5

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

MaterialDesign/src/com/gc/materialdesign/views/CheckBox.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.view.MotionEvent;
1717
import android.view.View;
1818
import android.widget.RelativeLayout;
19+
import android.widget.TextView;
1920

2021
public class CheckBox extends CustomView {
2122

@@ -70,20 +71,45 @@ public void run() {
7071
});
7172

7273
checkView = new Check(getContext());
74+
checkView.setId(View.generateViewId());
7375
RelativeLayout.LayoutParams params = new LayoutParams(Utils.dpToPx(20,
7476
getResources()), Utils.dpToPx(20, getResources()));
7577
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
7678
checkView.setLayoutParams(params);
7779
addView(checkView);
7880

81+
// Adding text view to checkbox
82+
int textResource = attrs.getAttributeResourceValue(ANDROIDXML, "text", -1);
83+
String text = null;
84+
85+
if(textResource != -1) {
86+
text = getResources().getString(textResource);
87+
} else {
88+
text = attrs.getAttributeValue(ANDROIDXML, "text");
89+
}
90+
91+
if(text != null) {
92+
params.removeRule(RelativeLayout.CENTER_IN_PARENT);
93+
params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
94+
TextView textView = new TextView(getContext());
95+
RelativeLayout.LayoutParams textViewLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
96+
LayoutParams.WRAP_CONTENT);
97+
textViewLayoutParams.addRule(RelativeLayout.RIGHT_OF, checkView.getId());
98+
textViewLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
99+
textViewLayoutParams.setMargins(10, 0, 0, 0);
100+
textView.setLayoutParams(textViewLayoutParams);
101+
textView.setText(text);
102+
103+
addView(textView);
104+
}
79105
}
80-
106+
81107
@Override
82108
public void invalidate() {
83109
checkView.invalidate();
84110
super.invalidate();
85111
}
86-
112+
87113

88114
@Override
89115
public boolean onTouchEvent(MotionEvent event) {
@@ -137,7 +163,7 @@ private void changeBackgroundColor(int color) {
137163

138164
/**
139165
* Make a dark color to press effect
140-
*
166+
*
141167
* @return
142168
*/
143169
protected int makePressColor() {

0 commit comments

Comments
 (0)