Skip to content

Instantly share code, notes, and snippets.

@liminal
Last active December 21, 2015 06:58
Show Gist options
  • Save liminal/6267484 to your computer and use it in GitHub Desktop.
Save liminal/6267484 to your computer and use it in GitHub Desktop.
Custom android ViewGroup
<?xml version="1.0" encoding="utf-8"?>
<com.example.MyItemView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/item_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</com.example.MyItemView>
package com.example;
public class MyItemView extends LinearLayout {
public static final MyItem EMPTY = new MyItem(null);
private TextView mTitleView;
private TextView mDescriptionView;
private MyItem item = EMPTY;
/** ... */
@Override
protected void onFinishInflate() {
super.onFinishInflate();
mTitleView = (TextView) findViewById(R.id.item_title);
mDescriptionView = (TextView) findViewById(R.id.item_description);
}
public void showItem(MyItem item) {
this.item = (item != null ? item : EMPTY);
String title = item.getTitle();
String description = item.getDescription();
// Either we implement a default text in EMPTY object or we return null to use default in layout
if (title != null) {
titleView.setText(title);
}
// as above but we hide description if empty
if (!TextUtils.isEmpty(description)) {
descriptionView.setText(description);
descriptionView.setVisibility(View.VISIBLE);
} else {
descriptionView.setVisibility(View.GONE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment