3

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?

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
Hanif
  • 95
  • 1
  • 6
  • Careful it might be XY problem. Why do you need to ad this `custom:userName` – Jacky Jan 16 '18 at 06:43
  • @Jacky The purpose is to change some Views inside the `my_layout` layout, maybe TextView or ImageView. The `custom:` namespace is just determine that what I need is an custom attribute I define myself. – Hanif Jan 16 '18 at 07:10
  • @Jacky I have updated the code in my question. – Hanif Jan 16 '18 at 07:12

2 Answers2

2

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>
Sneh Pandya
  • 7,230
  • 6
  • 32
  • 49
  • Thanks for the solution. I will try it first. And I have update my question. – Hanif Jan 16 '18 at 07:57
  • 1
    Ok looks like I get a further solution [here](https://developer.android.com/training/custom-views/create-view.html). Thanks for your explanation. I seem to use your answer because it has custom attribute solution and still allowing reusable layout or custom view. – Hanif Jan 16 '18 at 11:39
1

<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");
Jacky
  • 2,348
  • 3
  • 20
  • 31
  • Thanks for your answer. Your solution is good either. But now I need a reusable component that has custom attribute as well. Will upvote it too :) – Hanif Jan 16 '18 at 11:44