Edit a View (TextView / ImageView) inside a layout used in <include> tag using custom attribute

225 Views Asked by At

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?

2

There are 2 best solutions below

2
On BEST ANSWER

As your description, you want to post userName1 and userImage1 to the View in my_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:

<com.amscf.view.InviteItemView
    android:id="@+id/invite_item_instagram"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/item_color"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    app:appicon="@drawable/icon_app_instagram"
    app:btntext=""
    app:coindesc="Post a invitation"
    app:cointext="Share to Instagram"
    app:showweek="false"/>

In the view InviteItemView you can get the attibute appIcon, btnText with below code:

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.invite_item_view);
int refId = typedArray.getResourceId(R.styleable.invite_item_view_appicon, R.mipmap.ic_launcher);
String btnText = typedArray.getString(R.styleable.invite_item_view_btntext);

the refId and btnText is what you will get.

Add the below code to your attrs.xml, this is the definition of invite_item_view

<declare-styleable name="invite_item_view">
    <attr name="appicon" format="reference"/>
    <attr name="cointext" format="string"/>
    <attr name="coindesc" format="string"/>
    <attr name="btntext" format="string"/>
    <attr name="showweek" format="boolean"/>
</declare-styleable>
1
On

<inlcude /> tag is to help you reuse layout that difine in different page. One example is to define a custom ToolBar and reuse in Activities, Fragment.

There is noway you can add this custom tag like you mentioned, but instead you can straightaway declare in code behind, simply without DataBinding:

For example, with this toolbar.xml:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/colorWhite" />
</android.support.v7.widget.Toolbar>

and in activity_main.xml, you have:

<include layout="@layout/toolbar"/>

In MainActivity.java you can define this:

private TextView title = (TextView) findViewById(R.id.toolbar_title);
title.setText("Whatever you want");