Skip to content

Instantly share code, notes, and snippets.

@plastiv
Created May 7, 2015 12:33
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 plastiv/5143896623f7ecc606bc to your computer and use it in GitHub Desktop.
Save plastiv/5143896623f7ecc606bc to your computer and use it in GitHub Desktop.
SegmentedEditText
// SegmentedEditText is horizontal linear layout contains 6 edit texts
public class SegmentedEditText extends LinearLayout {
public SegmentedEditText(Context context) {
super(context);
LayoutInflater inflater = LayoutInflater.from(context);
for (int i = 0; i < 6; i++) {
// have to do next:
EditText editText = (EditText) inflater.inflate(R.layout.partial_segmented_edittext, this, false);
addView(editText);
// instead of this:
// code below crashes at the runtime, because inflate method returns LinearLayout not EditText
// I don't need reference for the LinearLayout, since I already have it. It is provided to the method.
// What I need is reference for the newly inflated EditText to avoid finding it by Id later
EditText editText = (EditText) inflater.inflate(R.layout.partial_segmented_edittext, this, true);
}
}
}
@filipeuva
Copy link

Dude, you can't do this. Just do a simple layout for this :

segmented_edit_text_layout.xml

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<EditText android:id="@+id/edit2" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<EditText android:id="@+id/edit3" android:layout_width="match_parent" android:layout_height="wrap_content"/>
</LinearLayout>

Then on your Activity you set it as your content view :

OnCreate(Bundle args) {
...
setContentView(R.layout.main);
..
}

OR on Fragment

OnCreateView(...) {
return inflater.inflate(R.layout.main,viewParent,false);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment