-1

I have a problem with creating a custom view component and then displaying the nested xml content when the control is defined in my layouts.

I have tried iterating the children and only getting the nested elements and then using removeChild then addChild to place the controls into the nested control but this does not work.

I have the following component SonrCard. This is a relative layout but I can't get the children to display in the correct place. I would like to inject the textview into the content area of the Sonr Card

Implementation of the component in my activity.xaml

<com.syntax.sonr.components.SonrCard
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        ads:title="@string/description">

                        <TextView
                            android:id="@+id/description_text"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="16dp"
                            android:layout_marginLeft="16dp"
                            android:layout_marginRight="16dp"
                            tools:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede." />

                    </com.syntax.sonr.components.SonrCard>

The merge.xml file is as follows

    <merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">


    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/open_sans_light"
        android:textSize="18sp"
        tools:text="Title" />

    // I would like to display the content (TextView) here


    <View
        android:id="@+id/border"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:background="#c1c1c1" />

I have created a derived class from RelativeLayout and I can get the attrs

public class SonrCard extends RelativeLayout {

String _title;
TextView _titleTextView;
LinearLayout _card;
FrameLayout _content;

public SonrCard(Context context) {
    super(context);
}

public SonrCard(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);

    getAttributes(context, attrs);
    initializeView(context);
}

public SonrCard(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    getAttributes(context, attrs);
    initializeView(context);
}

public SonrCard(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);

    getAttributes(context, attrs);
    initializeView(context);
}

private void initializeView(Context context) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.sonrcard_component_view, this);
}

private void getAttributes(Context context, AttributeSet attrs) {

    TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.SonrCard);
    _title = attributes.getString(R.styleable.SonrCard_title);

    attributes.recycle();
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    _card = findViewById(R.id.sonr_card);
    _titleTextView = findViewById(R.id.title);
    _titleTextView.setText(_title);
    _content = findViewById(R.id.content);

    final int count = _card.getChildCount();

    for (int i = 0; i < count; i++) {
        final View child = _card.getChildAt(i);
        int id = child.getId();

        if (id != R.id.title && id != R.id.border){
            _card.removeView(child);
            _content.addView(child);
        }
    }
}

}

Thank you

  • It's not clear which is your current approach – having the content `TextView` as a direct child of `SonrCard`, positioned between the existing `TextView` and `View`; or moving the content `TextView` into a `FrameLayout` already positioned there. If the first, then you just need to adjust the content `TextView`'s `LayoutParams` in `onFinishInflate()`. If the second, you'll have to show the full, current layout, and explain how your code isn't working as expected. – Mike M. Oct 20 '18 at 12:26

1 Answers1

0

Instead of <Merge> why don't you use <Include>?

So replace your merge.xml as

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >


<TextView
    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="16dp"
    android:fontFamily="@font/open_sans_light"
    android:textSize="18sp"
    tools:text="Title" />

<include 
android:id="@+id/myIncludedLayout"
layout="@layout/yourSonrCardLayout"/>


<View
    android:id="@+id/border"
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:background="#c1c1c1" />

Read more about Re-using layouts with Include tag

To inflate the Included layout

View includedLayout = findViewById(R.id.myIncludedLayout);

// geting the description_text TextView declared in yourSonrCardLayout.xml by myIncludedLayout
TextView description_text= (TextView) myIncludedLayout.findViewById(R.id.description_text);
description_text.setText("this is header text 3");
Daksh Gargas
  • 3,498
  • 2
  • 23
  • 37