1

I want to convert this XML file Into a Custom View, but it doesn't work:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:src="@drawable/ic_launcher" />

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/imageView1">

        <TextView
            android:id="@+id/posted_content_user"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="posted_content_user"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/posted_content_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/posted_content_user"
            android:layout_alignParentRight="true"
            android:text="59m"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/relativeLayout1"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/relativeLayout1"
        android:text="TextView 183091480914809 11rh1h23 k1j2h 3oi12u 3o12h3kj12h3iu12 h3kj12h3 12uy3h12kjh31 i2uy3ijh" />

</RelativeLayout>

It Supposed to look like this:

https://i.stack.imgur.com/czXKH.png

This is My Code for generated View

public void generateView(Context context) {
    this.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

    // Contacts Pictures
    ivContactPictures = new ImageView(context);
    ivContactPictures.setLayoutParams(new RelativeLayout.LayoutParams(80,
            80));

    // RelativeLayout
    RelativeLayout relLayoutContent = new RelativeLayout(context);
    RelativeLayout.LayoutParams relLayoutContentParams = new RelativeLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    relLayoutContentParams.addRule(RIGHT_OF, ivContactPictures.getId());
    relLayoutContent.setLayoutParams(relLayoutContentParams);

    // RelativeLayout -> TextView Post User
    posted_content_user = new TextView(context);
    posted_content_user.setLayoutParams(new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    // posted_content_user.setTextAppearance(context,
    // R.attr.textAppearanceMedium);

    // RelativeLayout -> TextView Time
    posted_content_date = new TextView(context);
    RelativeLayout.LayoutParams posted_content_dateParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // posted_content_user.setTextAppearance(context,
    // R.attr.textAppearanceSmall);
    posted_content_dateParams.addRule(ALIGN_BOTTOM,
            posted_content_user.getId());
    posted_content_dateParams.addRule(ALIGN_PARENT_RIGHT);
    posted_content_date.setLayoutParams(posted_content_dateParams);

    // textView Content
    posted_content = new TextView(context);
    RelativeLayout.LayoutParams posted_contentParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    posted_contentParams.addRule(ALIGN_LEFT, relLayoutContent.getId());
    posted_contentParams.addRule(ALIGN_PARENT_RIGHT);
    posted_contentParams.addRule(BELOW, relLayoutContent.getId());
    posted_content.setLayoutParams(posted_contentParams);

    // Add RelativeLayout
    this.addView(ivContactPictures);
    relLayoutContent
            .addView(posted_content_user);
    relLayoutContent
            .addView(posted_content_date);
    this.addView(relLayoutContent);
    this.addView(posted_content);

}

Result: https://i.stack.imgur.com/63e3m.png

Is there anything wrong with my code?

mutokenji
  • 525
  • 1
  • 5
  • 14

1 Answers1

0

That's not how you generate a custom view- or at least its the very hard way. What you should do instead is create a child of View that inflates that xml into itself as the parent. That will make that class equal to the entire contents of the xml file. Then you can provide whatever getter/setter/other functionality that's appropriate.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I got that tutorial in android developer site. Would you mind if you tell me how to do it? I still new in android development. – mutokenji Jun 27 '13 at 14:03
  • Check out http://stackoverflow.com/questions/12978298/custom-view-made-of-multiple-views which is where I asked a similar question a while ago. – Gabe Sechan Jun 27 '13 at 15:13