Android Weekdays selection custom widget

Android offers a very good componentized model for building custom UI, based on the fundamental layout classes: View and ViewGroup.

Some of the default widgets includes Button, TextView,EditText, ListView, CheckBox, RadioButton, GallerySpinner, etc. Few default available layouts are LinearLayout, FrameLayout, RelativeLayout. If none of these prebuilt widgets or layouts satisfy our business requirement, we can create our own custom widget by extending the base View, ViewGroup classes or any of the prebuilt widgets or layouts. In this tutorial we are going to learn how to create Android weekdays selection custom widget.

See the following:

  1. Extend an existing View class or subclass with our own class.
  2. Override some of the methods from the superclass, for example, onDraw(), onMeasure(), and onKeyDown().
  3. Add custom attributes to define custom input in layout file. In this tutorial I’ve added two custom attributes: highlight_color and editable.
  4. TextDrawable library is used to bring up material theme in displaying the weekday entries.

Creating a custom View class

Creating custom view class is as simple as creating a typical java class which extends base View, ViewGroup classes or any of the prebuilt widgets such as   Button, TextView or layouts such as LinearLayout, FrameLayout.

Right click  your desired package in Android Studio project explorer -> New  -> UI Component  -> Custom View

Creating custom view screenshot


This will bring up a modal dialog asking for confirmation in creating three files:

  1. – inside the chosen package. This is the file where we will write our core logic.
  2. sample_my_view.xml – in res/layout folder. We can desing our custom view with combination of prebuilt widgets and layouts.
  3. attrs_my_view.xml – in res/values folder. This is where we have to define the custom attributes.

Note: In this tutorial we are not going to use xml layout design for your custom widget. Everything is going to be done in java class.  is the default file name that Android Studio picks up for us. I’ve renamed it to . and attrs_my_view.xml  is renamed to attrs.xml .

Before developing this widget, I’ve used a combination of Checkbox controls to accomplish this functionality. The output looks something like displayed below.

Checkbox style screenshot


To make it more Materialized, I’ve used TextDrawable library. Here I’ll tell you how to include this library in your Android Studio Project.

Add the below lines in your root build.gradle  file (not your module build.gradle file)

Then add this to your module’s build.gradle file:

Do Gradle Sync your project. We are all set to write our core logic to create a Custom Weekdays Widget.


1. project_rootdir/build.gradle


2. app/build.gradle


3. Manifest File


4. Custom Attribute Definition


5. WeekdaysWidget custom view


6. Layout for demo MainActivity




8. Demonstration

Custom weekdays widget
Custom weekdays widget

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">