Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom View Example for drawing raw text and images
public class CustomRowView extends View {
private CharSequence mText;
private Layout mTextLayout;
private TextPaint mTextPaint;
private Drawable mImageDrawable;
public CustomRowView(Context context) {
this(context, null);
}
public CustomRowView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomRowView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(Color.BLACK);
//Text Size: 14dp
float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics());
mTextPaint.setTextSize(textSize);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int imageSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
int width = getDefaultSize(0, widthMeasureSpec);
if (mImageDrawable != null) {
//Size and locate the image icon
mImageDrawable.setBounds(getPaddingLeft(), getPaddingTop(),
getPaddingLeft() + imageSize,
getPaddingTop() + imageSize);
}
if (mText == null) { mText = ""; }
//Size the text based on the remaining width
int textWidth = width - getPaddingLeft() - getPaddingRight() - imageSize;
mTextLayout = new StaticLayout(mText, mTextPaint, textWidth, Layout.Alignment.ALIGN_NORMAL, 1f, 0f, true);
//Size the view height based on required text space
int desiredHeight = Math.max(getPaddingTop() + getPaddingBottom() + mTextLayout.getHeight(),
getPaddingTop() + getPaddingBottom() + imageSize);
int height = getDefaultSize(desiredHeight, heightMeasureSpec);
setMeasuredDimension(width, height);
}
public void setImageResource(int resId) {
//Update the icon and resize the view
mImageDrawable = getResources().getDrawable(resId);
requestLayout();
}
public void setText(CharSequence text) {
//Update the text and resize the view
mText = text;
requestLayout();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mImageDrawable != null) {
//Draw the image content
mImageDrawable.draw(canvas);
//Draw the text content
canvas.save();
canvas.translate(mImageDrawable.getBounds().right, mImageDrawable.getBounds().top);
mTextLayout.draw(canvas);
canvas.restore();
}
}
}
public class MainActivity extends ActionBarActivity {
private static final String[] ITEMS = {
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel tellus at diam blandit dictum.",
"Integer vitae nisi augue. Etiam placerat sed ipsum id pharetra. In interdum lectus massa, et semper quam mattis ac.",
"Vestibulum sed eros interdum, commodo turpis eget, ullamcorper ante.",
"Vestibulum hendrerit ligula libero, id accumsan velit tempus sed.",
"Aliquam interdum purus a tellus lacinia, quis ornare sem sagittis",
"Nam ultricies vestibulum libero, ac gravida mauris pharetra ac. Nunc tincidunt felis egestas urna pulvinar adipiscing ut consequat leo.",
"Suspendisse nisl est, tincidunt quis aliquam eu, pellentesque non neque. Vestibulum vitae varius dolor.",
"Aliquam eleifend orci massa, id convallis nulla pharetra vitae.",
"Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.",
"Sed nec egestas ipsum. Nullam pellentesque, quam gravida viverra pellentesque, urna nibh consectetur erat, consequat venenatis ante lorem id velit.",
"Mauris imperdiet felis at elit dignissim imperdiet.",
"Pellentesque rhoncus est quis libero blandit ornare. Pellentesque ac nibh accumsan dui porta ultrices.",
"Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.",
"Ut sit amet tempus neque. Etiam ut urna pharetra odio fermentum consectetur vitae in ligula.",
"Ut ut tellus non metus vestibulum pulvinar. Mauris quis mollis eros, nec accumsan ligula. Fusce vehicula neque et nisi sagittis, sed posuere neque eleifend.",
"Ut a diam sit amet arcu vestibulum convallis. Quisque hendrerit consectetur nisl vel dignissim. Aliquam erat volutpat.",
"Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras pellentesque ac orci et tempor.",
"Etiam a tellus turpis. Vestibulum eget mattis ligula."
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list = new ListView(this);
list.setAdapter(new CustomListAdapter(this, ITEMS));
setContentView(list);
}
private static class CustomListAdapter extends ArrayAdapter<String> {
public CustomListAdapter(Context context, String[] items) {
super(context, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = new CustomRowView(getContext());
}
CustomRowView rowView = (CustomRowView) convertView;
rowView.setImageResource(R.drawable.ic_launcher);
rowView.setText(getItem(position));
return rowView;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.