I'm new to Android Development.
Is it possible to change a View inside an included layout using custom attribute?
I mean like this:
This is my_layout
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listItem"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/userImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="..." />
<!-- this src attribute can be overrided using my own attribute in the iclude tage -->
<TextView
android:id="@+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="..." />
<!-- this text attribute can be overrided using my own attribute in the iclude tage -->
</LinearLayout>
and this is how I want to include my_layout
to the activity layout:
<include layout="@layout/my_layout" userName="@string/userName1" userImage="@drawable/userImage1"/>
userName
and userImage
override the value of android:text
and android:src
attribute.
I have read about Data Binding but what I mean here is not using any Java class. I just need to define a variable into data tag in my_layout
with type
value referenced to @string/
or @drawable/
to get a text or picture.
Is it possible?
As your description, you want to post
userName1
anduserImage1
to the View inmy_layout
. You can not make it. And the way you use the custom view is wrong. The tag 'include' is also not custom tag.You can use custom attribute follow this sample:
In the view
InviteItemView
you can get the attibuteappIcon
,btnText
with below code:the
refId
andbtnText
is what you will get.Add the below code to your
attrs.xml
, this is the definition ofinvite_item_view