Use Kotlin to implement custom Android views (KAD 06) and kotlinandroid

Source: Internet
Author: User

Use Kotlin to implement custom Android views (KAD 06) and kotlinandroid

By Antonio Leiva

Time: Dec 27,201 6

Link: https://antonioleiva.com/custom-views-android-kotlin/

 

 

When reading articles about class, you may remember to use only one constructor. This is a problem for creating custom views.

 

The Android framework wants multiple constructors to correspond to different view creation locations and view creation methods (through code, using XML, setting themes, and so on), so that we cannot ignore this situation.

 

To this end, the Kotlin team provides the ability to have multiple constructors in the same class, as mentioned here.

 

Java compatibility is the basic requirement of Kotlin. Therefore, no matter what the situation you encounter, you should think of methods to implement your needs.

 

Create a custom view in Kotlin

 

Even if you already have experience creating custom views and some Kotlin knowledge, you may create custom views in Kotlin for the first time, and you will find it a bit complicated.

 

 

Implementing several constructors in Kotlin is one of the most complex tasks. To be precise, this is a rare usage.

 

 

However, you don't have to worry. Once you get in touch, the rest are very similar.

 

 

Note: although this article can help you understand how to use multiple constructors in the Kotlin class, the method mentioned in the comments by KirillRakhman is better. Read at the end.

 

Create a class that inherits the View

 

 

In this way, create a class as we saw earlier. For example, it inherits the View, but does not indicate any constructor:

1 class KotlinView : View {2 }

 

This section of code invites to call the constructor of the parent class, so this section cannot be compiled.

 

 

For example, if you just expand your view in the Kotlin code, you may use the unique constructor form we have seen:

1 class KotlinView(context: Context?) : View(context) {2 }

 

But be careful, because if you decide to add XML to this view, it will fail.

 

 

Note: Do you see the question mark on the Right of Context? In Kotlin, if we want a variable or parameter to be null, we must specify it with a question mark. Then the compiler will require us to check before using this variable that it cannot be null. Read this in the next article.

 

Implement multiple Constructors

 

 

The constructor can be another constructor of the same type (using this) or a parent class (using super ).

 

 

Here is the constructor that you define the Android View:

 1 class KotlinView : View { 2   3     constructor(context: Context?) : this(context, null) 4     constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0) 5   6     constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { 7         ... 8     } 9  10 }

 

Simpler implementation

 

In the comment, Kirill mentioned (Thank you very much !) Another method is simpler and easier to read. It allocates default values for the independent variables of the constructor, but requires a slight adjustment.

 

 

The problem is that when you use the default value of the independent variable to create a constructor (or any function), the bytecode generated in Kotlinzhong only allows those default values. If you use constructors in Java, You can forcibly describe the values of all independent variables.

 

 

This is because Java does not have such language features. In Java, you need to solve it by generating function overloading based on your needs.

 

 

In Kotlin, you can use @ JvmOverloads to annotate the automatically generated code.

 

 

The Code is as follows:

1 class KotlinView @JvmOverloads constructor(2         context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 03 ) : View(context, attrs, defStyleAttr)

 

Conclusion

 

Once you understand it, it is not complicated to generate a custom view that contains multiple constructors. It is easier to annotate it with @ JvmOverloads.

 

 

This is useful for any class that requires multiple constructors. But in general, if you can assign values to the default values of parameters (to avoid overloading), there is usually no need for multiple constructors.

 

 

If you want to learn more about this and write an actual APP, I suggest you read my book "Kotlin for Android Developers".

 

Create a custom view in Kotlin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.