Search code examples
androidandroid-custom-view

Uniquely identify elements which are inside android Compound View


The Problem: How to uniquely identify elements which are inside android Compound View?

Background: In my application I used Compound View. Because It has two TextFields, one image and one seek bar in each row. The layout is working fine.

So in the layout there are two separate seek bars. I need to get the value of them in my activity.

I can identify each row separately because they have id. But the items inside the row repeat in each row.

My problem is how can I identify each seek bar uniquely?

row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >

       <RelativeLayout
            android:id="@+id/colunm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="40sp" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="6dip"
                android:layout_marginRight="6dip"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:fadingEdge="horizontal"
                android:singleLine="true"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:maxLines="1"
                android:textAppearance="?android:attr/textAppearanceSmall" />

            <SeekBar
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </RelativeLayout>

    </merge>

screen_layout.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <com.example.test.view.Compound
            android:id="@+id/vol_Row1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            custom:column_summary="TEST SUMMARY I"
            custom:column_title="Test TITLE 1"
            custom:image_Icon="@drawable/ic_action1" />

        <com.example.test.view.Compound
            android:id="@+id/vol_Row2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            custom:column_summary="TEST SUMMARY 2"
            custom:column_title="Test TITLE 2"
            custom:image_Icon="@drawable/ic_action2" />
    </LinearLayout>

row_layout class

    public Compound(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.Options, 0, 0);
        String titleText=a.getString(R.styleable.Options_title);
        String summaryText=a.getString(R.styleable.Options_summary);
        int icon=a.getInt(R.styleable.Options_image_Icon,0);

        a.recycle();

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

        imageView=getChildAt(0);
        imageView.setImageResource(icon);

        title=getChildAt(1)
        title.setText(titleText);

        summary=getChildAt(2)
        summary.setText(summaryText);

    }
}

Solution

  • I did it in this way. In compound view I gave unique ids to each element. Then in test code I could access them uniquely like this,

    private View inflaterView;
        LayoutInflater i=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflaterView=i.inflate(R.layout.screen_layout, null);
            seekBar=(SeekBar) inflaterView.findViewById(R.id.vol_Row1).findViewById(R.id.seekbar);