Hi !! This tutorial will teach you how to create these custom UI components
When we building Android Applications, Android offers a sophisticated and powerful componentized model for building your UI. The Android framework is very robust and it provides means of creating our own custom components which we can customize to suit our needs.
For this tutorial, We will see how to create a simple text view with the different text size of integer and decimal parts.
First, we’re going to need a layout XML file setup for our search view. Let’s create a new XML file called smart_numeric_text_view.xml
But we must don't forget, our Linearlayout will be created with class when we extend this class as a Linearlayout and cause of this we will use merge tag for grouping all view and we avoided using a nested layout.
Why we used this merge tag?
If we using LinearLayout tag. we used two linear layouts for grouping our two text view, the first LinearLayout comes from our extended class. And we can improve our component performance quality, changing the second LinearLayout with a merge tag.
To add a built-in View
to your user interface, you specify it in an XML element and control its appearance and behaviour with element attributes. Well-written custom views can also be added and styled via XML. To enable this behaviour in your custom view, you must:
- Define custom attributes for your view in a
<declare-styleable>
resource element - Specify values for the attributes in your XML layout
- Retrieve attribute values at runtime
- Apply the retrieved attribute values to your view
This section discusses how to define custom attributes and specify their values. The next section deals with retrieving and applying the values at runtime.
To define custom attributes, add <declare-styleable>
resources to your project.
It's customary to put these resources into a
res/values/attrs.xml
file.
Create a attrs.xml
file in the values folder and add the following code.
it’s our attrs sample…
For more information, you can check this StackOverflow question.
Defining custom attrs
I need to implement my own attributes like in com.android.R.attr Found nothing in official documentation so I need…
stackoverflow.com
and you can check all example attr definition on this repository.
First of all, the All defined Android components were extend from View
class. We will be extending our class from LinearLayout,
and make it very simple for understanding.
In this tutorial, I will be extending a LinearLayout, and make it very simple to understand.
Note: This tutorial is exactly using for functionality implementation. I tried to give the basic knowledge from here.
Click here to download the complete code on GitHub.
Kindly give your feedback on this tutorial by commenting or reach me via Twitter on @ozanturcan