Combining Android Components in a Custom View

Iโ€™m not sure where to start, Iโ€™ve been developing the application for the last week and have made significant progress and now I need a user view. I looked at http://developer.android.com/guide/topics/ui/custom-components.html . but yes ... not sure how to put it together. I want to place the number marker on the left, and the other text and image to the right and be able to listen to clicks on the number selection panel. To be honest, I'm not sure where to start.

+3
source share
1 answer

I would use a layout to combine your Components. Define your layout in xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal">
       <NumberPicker
        android:id="@+id/picker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        />
       <ImageView 
       android:id="@+id/image"
       android:layout_toRightOf="@id/picker"
       android:src="..."
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
       />
      <TextView 
       android:id="@+id/text"
       android:text="some text"
       android:layout_toRightOf="@id/image"
       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
       />
</RelativeLayout>

: , BaseAdapter, getView :

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = null;

    //if convertView is null, inflate a view.
    //otherwise reuse the convertView   
    if(convertView == null)
    {
        view = mInflater.inflate(R.layout.filebrowserrow, null);
    }
    else
    {
        view = convertView;
    }

    //retrieve picker to set listeners, initialize values, etc  
    NumberPicker picker = (NumberPicker)view.findViewById(R.id.picker);

    //retrieve ImageView to change image, etc   
    ImageView image = (ImageView)view.findViewById(R.id.image);

    //retrieve TextView to change text, etc 
    TextView text = (TextView)view.findViewById(R.id.text);     

    return view;

}

+2

All Articles