I have so many util codes for UI and want to create a android library for each of them. With all my research I have done below things.
File -> new module. Create an android library
in build.gradle file implement the library implementation project(':app:FirstLibrary')
Kotlin and xml file in library is below
FirstLib.kt
class FirstLib(mContext: Context) : Activity() {
private var mContext: Context? = null
private var view: View? = null
fun s(message: String?) {
layoutInflater.inflate(R.layout.first_lib_view, null)
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show()
}
}
first_lib_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Om Namah Shivaya"
android:textSize="16sp" />
</LinearLayout>
<com.example.firstlibrary.FirstLib
android:layout_width="match_parent"
android:layout_height="match_parent"
tools="ajkdfbj"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Now I'm not getting anything. Even toast message was not coming.
How to create UI library in Android and use it. I have my own custom video players, audio players, image gradient views etc. I need to make all those libraries now. Thanks for reading
To implement a custom view element you need to put your class into the separate module as you did before and inherit your custom view from a view.
xml
files then you should create smth like:MyCustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): View {
// you should override onMeasure, onLayout, onDraw here as it marked as abstract afaik
// and implement your logic
}
and implement the whole onMeasure
, onLayout
, onDraw
logic.
MyCustomView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
): TextView() {
... your logic goes here...
}
You did implement a custom activity but uses it like a view, so, that's why it doesnt work for you
PS:here you can find out more about custom view components
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.