Skip to content

Instantly share code, notes, and snippets.

@chrisjenx
Created August 8, 2014 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisjenx/84106c43845db6311e04 to your computer and use it in GitHub Desktop.
Save chrisjenx/84106c43845db6311e04 to your computer and use it in GitHub Desktop.
Unfinished FormGroup to support ImageViews outside of EditTexts. Will set states on the ImageViews based on the focus state of the EditTexts.
<com.justyoyo.ui.views.forms.FormGroupView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/space_small"
android:paddingRight="@dimen/space_small">
<TextView
style="@style/TextViewV3.AssetEditText"
android:text="S"/>
<Space
android:layout_width="@dimen/space_micro"
android:layout_height="match_parent"/>
<com.justyoyo.ui.views.forms.NameEditText
android:id="@+id/flow_about_you_first_name"
style="@style/EditTextV3.Name"
android:layout_width="0dp"
android:layout_weight="1"
android:nextFocusDown="@+id/flow_about_you_mobile"
android:nextFocusForward="@+id/flow_about_you_last_name"
android:nextFocusRight="@+id/flow_about_you_last_name">
<requestFocus/>
</com.justyoyo.ui.views.forms.NameEditText>
<Space
android:layout_width="@dimen/space_micro"
android:layout_height="match_parent"/>
<com.justyoyo.ui.views.forms.NameEditText
android:id="@+id/flow_about_you_last_name"
style="@style/EditTextV3.Name.Last"
android:layout_width="0dp"
android:layout_weight="1"
android:nextFocusDown="@+id/flow_about_you_mobile"
android:nextFocusForward="@+id/flow_about_you_mobile"
android:nextFocusLeft="@+id/flow_about_you_first_name"
android:nextFocusRight="@+id/flow_about_you_mobile"
android:nextFocusUp="@+id/flow_about_you_first_name"/>
</com.justyoyo.ui.views.forms.FormGroupView>
/**
* Created by chris on 06/08/2014.
* For Yoyo-Android.
*/
public class FormGroupView extends LinearLayout {
protected List<EditText> mEditTexts = new ArrayList<>();
protected List<View> mOtherViews = new ArrayList<>();
public FormGroupView(final Context context, final AttributeSet attrs) {
super(context, attrs);
setOrientation(HORIZONTAL);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mEditTexts.clear();
mOtherViews.clear();
for (int i = 0; i < getChildCount(); i++) {
final View child = getChildAt(i);
if (child instanceof EditText) {
mEditTexts.add((EditText) child);
} else {
mOtherViews.add(child);
}
}
getViewTreeObserver().addOnPreDrawListener(new StateChangePreDrawListener(mEditTexts));
}
/**
* Will activate the ImageViews
*/
private void onFocusText(boolean focus) {
for (View otherViews : mOtherViews) {
otherViews.setActivated(focus);
}
}
protected class StateChangePreDrawListener implements ViewTreeObserver.OnPreDrawListener {
private final View[] mViews;
private final boolean[] currentFocusStates;
public StateChangePreDrawListener(@NotNull final List<EditText> editTexts) {
mViews = editTexts.toArray(new View[editTexts.size()]);
currentFocusStates = new boolean[mViews.length];
}
@Override
public boolean onPreDraw() {
for (int i = 0; i < mViews.length; i++) {
if (currentFocusStates[i] && mViews[i].isFocused() && mViews[i].isEnabled()) {
// Still got focus return true! :)
return true;
} else if (!currentFocusStates[i] && mViews[i].isFocused() && mViews[i].isEnabled()) {
// Gaining focus, show focus state :)
currentFocusStates[i] = true;
onFocusText(true);
return true;
}
// Lost focus for this EditText
currentFocusStates[i] = mViews[i].isFocused();
}
// Nothing has focus or is disabled..
onFocusText(false);
return true; // We always want it to draw.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment