Making Custom View Components in Android
February 26, 2014Making your own custom view in an Android app isnât too hard to do but it requires knowledge of some sparsely documented features of Android development.
Weâll walk through how to create an image button with a caption.
Creating the Layout
A captioned button is made up of two components, an ImageButton and a TextView below it. Start by making a layout file in â/res/layout/â called âcaption_button.xmlâ with a vertical LinearLayout that contains these two components:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/linearLayout"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageButton_icon"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Caption" android:id="@+id/textView_caption" android:gravity="center_horizontal"/> </LinearLayout
Alternatively, if you wanted to make a captioned image, use an ImageView instead of an ImageButton.
Custom Component Class
Now weâll make a class called âCaptionButtonâ that will handle the functionality and customization of the custom component. Since the top-level view of our custom component is a LinearLayout, CaptionButton will be a child class of LinearLayout.
public class CaptionButton extends LinearLayout { ImageButton imageButton_icon; TextView textView_caption; public CaptionButton(Context context) { super(context); } public CaptionButton(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.caption_button, this); imageButton_icon = (ImageButton) findViewById(R.id.imageButton_icon); textView_caption = (TextView) findViewById(R.id.textView_caption); } }
What have we done here? Weâve inflated the caption_button.xml layout and declared and assigned our ImageButton and TextView from our caption_button.xml layout.
Custom Attributes
When creating an XML layout with our custom component, weâre going to want to declare two custom attributes: the Drawable icon for the button and the String for the caption. When using our custom component in some other XML layout, it will look something like this:
<com.yourpackagename.CaptionButton android:layout_width="wrap_content" android:layout_height="wrap_content" my_app:button_icon="@drawable/message_icon" my_app:caption="@string/message_caption"/>
Note that custom attributes will need to use a custom namespace, so in a parent element, remember to declare
xmlns:my_app="http://schemas.android.com/apk/res-auto"
If your project doesnât already have one, create a file called âattrs.xmlâ in your â/res/values/â folder, and add this to the file:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CaptionButton"> <attr name="button_icon" format="reference" /> <attr name="caption" format="reference|string" localization="suggested" /> </declare-styleable> </resources>
Here weâve created a custom styleable named âCaptionButtonâ with two attributes, âbutton_iconâ and âcaptionâ. Letâs break down whatâs happening on each of these lines.
<declare-styleable name="CaptionButton">
The name of our custom styleable doesnât need to be âCaptionButtonâ. It can be anything, but âCaptionButtonâ easily lets us know what custom component these attributes will be applied to. If we were to reuse these attributes on other custom components, we would select a more general name.
<attr name="button_icon" format="reference" />
For the âbutton_iconâ attribute, when we want an attribute to refer to a resource in our project, such as a Drawable resource in this case, we use âformat=âreferenceââ.
<attr name="caption" format="reference|string" localization="suggested" />
For the âcaptionâ attribute, we can use either a reference to a String resource in our project, such as â@string/message_captionâ or a literal String, such as âMessageâ, hence the use of âformat=âreference | stringââ. Use of âlocalization=âsuggestedââ indicates that if a resource is retrieved, it should use the properly localized resource. |
Retrieving Custom Attributes and Applying them to the Custom Component
Now that weâve declared our custom attributes, hereâs how we retrieve them and apply them to our custom component.
public CaptionButton(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater inflater = LayoutInflater.from(context); inflater.inflate(R.layout.caption_button, this); imageButton_icon = (ImageButton) findViewById(R.id.imageButton_icon); textView_caption = (TextView) findViewById(R.id.textView_caption); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CaptionButton); Drawable icon = a.getDrawable(R.styleable.CaptionButton_button_icon); String caption = a.getString(R.styleable.CaptionButton_caption); a.recycle(); setIcon(icon); setCaption(caption); } public void setIcon(Drawable icon) { imageButton_icon.setImageDrawable(icon); } public void setCaption(String caption) { textView_caption.setText(caption); }
We use a TypedArray in the CaptionButton constructor that uses an AttributeSet to retrieve what values were given to the two custom attributes in a CaptionButton XML declaration. The Drawable reference from âmy_app:button_icon=â@drawable/message_iconââ is retrieved and assigned to the icon field, and the String from âmy_app:caption=â@string/message_captionââ is retrieved and assigned the caption field.
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CaptionButton); Drawable icon = a.getDrawable(R.styleable.CaptionButton_button_icon); String caption = a.getString(R.styleable.CaptionButton_caption);
After retrieving the attributes from the TypedArray, donât forget to call
a.recycle();
Once the attributes have been retrieved, we apply each of them to individual Views in our CaptionButton, the ImageButton and the TextView.
setIcon(icon); setCaption(caption); public void setIcon(Drawable icon) { imageButton_icon.setImageDrawable(icon); } public void setCaption(String caption) { textView_caption.setText(caption); }
We will also want to override the click listener for this custom component and apply it to the ImageButton.
@Override public void setOnClickListener(OnClickListener onClickListener) { imageButton_icon.setOnClickListener(onClickListener); }
And there we have it, our very own custom component.
The code from this post can be found in this Github Gist.
Looking for more like this?
Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.
Kotlin Multiplatform
July 14, 2022A brief look at Kotlin Multiplatform Mobile, a newer cross-platform mobile application framework.
Read moreTech is for everyone
June 20, 2024Have you ever felt like the tech world was an Ivy league school, where only the most elite students gain acceptance? Discover paths into the industry you may not have considered.
Read moreMake Your Website Shine Through Shareable Meta tags
June 20, 2023Improve the web site preview used by chat apps, websites, and social media with these easy tips.
Read more