23.07.2015 Views

Android Tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER<br />

17<br />

Custom Components<br />

<strong>Android</strong> offers a great list of pre-built widgets like Button, TextView, EditText, ListView, CheckBox,<br />

RadioButton, Gallery, Spinner, AutoCompleteTextView etc. which you can use directly in your <strong>Android</strong> application<br />

development, but there may be a situation when you are not satisfied with existing functionality of any of the<br />

available widgets. <strong>Android</strong> provides you with means of creating your own custom components which you can<br />

customized to suit your needs.<br />

If you only need to make small adjustments to an existing widget or layout, you can simply subclass the widget or<br />

layout and override its methods which will give you precise control over the appearance and function of a screen<br />

element.<br />

This tutorial explains you how to create custom Views and use them in your application using simple and easy<br />

steps.<br />

Creating a Simple Custom Component<br />

The simplest way to create your custom component is to extend an existing widget class or subclass with your own<br />

class if you want to extend the functionality of existing widget like Button, TextView, EditText, ListView, CheckBox<br />

etc. otherwise you can do everything yourself by starting with theandroid.view.View class.<br />

At its simplest form you will have to write your constructors corresponding to all the constructors of the base class.<br />

For example if you are going to extend TextView to create a DateView then following three constructors will be<br />

created for DateView class:<br />

public class DateView extends TextView {<br />

public DateView(Context context) {<br />

}<br />

super(context);<br />

//--- Additional custom code --<br />

public DateView(Context context, AttributeSet attrs) {<br />

super(context, attrs);<br />

//--- Additional custom code --<br />

TUTORIALS POINT<br />

Simply Easy Learning

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!