Last active
September 5, 2021 06:46
-
-
Save aslamanver/cb10d9690ed057b2859383210d2c3b4b to your computer and use it in GitHub Desktop.
Custom View in Android - Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<com.yourapp.widget.DetailsView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
app:text="Welcome" | |
app:value="Feb" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<declare-styleable name="DetailsView"> | |
<attr format="string" name="text"/> | |
<attr format="string" name="value"/> | |
</declare-styleable> | |
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<merge xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/text_label" | |
android:layout_width="0dip" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.5" | |
tools:text="Label" /> | |
<TextView | |
android:id="@+id/text_value" | |
android:layout_width="0dip" | |
android:layout_height="wrap_content" | |
android:layout_weight="0.5" | |
tools:text="Value" /> | |
</LinearLayout> | |
</merge> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context | |
import android.content.res.TypedArray | |
import android.util.AttributeSet | |
import android.view.View | |
import android.widget.LinearLayout | |
import android.widget.TextView | |
class DetailsView @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : LinearLayout(context, attrs, defStyleAttr) { | |
private val attributes: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.DetailsView) | |
private val view: View = View.inflate(context, R.layout.details_view, this) | |
init { | |
view.findViewById<TextView>(R.id.text_label).text = attributes.getString(R.styleable.DetailsView_text) | |
view.findViewById<TextView>(R.id.text_value).text = attributes.getString(R.styleable.DetailsView_value) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment