user1384991
user1384991

Reputation: 2629

Custom components or regular composition

I'm up to creating a piece of reusable interface, namely a year-picker(just year). Essentially, this component should have a couple of buttons and a textview, sliding feature would be great as well.

As I've said I strive to make it reusable, but I'm not sure whether Android provides any means for achieving it. I've started reading the "Custom Components" article - is it what I really need ? Or should I make something like a regular composing class that has references to view objects, etc ?

Thanks.

Update: it was simple - here's an example if someone gets in the same situation

YearPicker.class

public class YearPicker extends LinearLayout {
    protected int _minValue = 1980;
    protected int _currentValue;
    protected int _maxValue;

    protected int _decrementValueView = R.id.decrement_value;
    protected int _previousValueView = R.id.previous_value;
    protected int _currentValueView = R.id.current_value;
    protected int _nextValueView = R.id.next_value;

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

    public YearPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        Calendar calendar = Calendar.getInstance();
        _maxValue = calendar.get(Calendar.YEAR);
        _currentValue = _maxValue - ((_maxValue - _minValue) / 2);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        TextView curView = (TextView) findViewById(_currentValueView);
        curView.setText(Integer.toString(_currentValue));
        findViewById(_decrementValueView).setOnClickListener(
                _onDecrementValueClick);
    }

    protected OnClickListener _onDecrementValueClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            _currentValue--;
            TextView curView = (TextView) findViewById(_currentValueView);
            curView.setText(Integer.toString(_currentValue));
        }
    };
}

main.xml

<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <include 
        layout="@layout/year_picker"/>

</LinearLayout>

year_picker.xml

<view xmlns:android="http://schemas.android.com/apk/res/android"
    class="localhost.yearpicker.YearPicker"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/decrement_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/previous_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

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

    <TextView
        android:id="@+id/next_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <Button
        android:id="@+id/increment_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</view>

Upvotes: 2

Views: 553

Answers (2)

James Coote
James Coote

Reputation: 1973

You might not need a custom view at all. I prefer to keep separate XML layouts that I can inflate as and when needed, then control them with custom OnClickListeners (or OnTouchListeners)

I'd only resort to a custom view if you need to override a base view's method (such as the onLayout or draw() methods)

Upvotes: 1

avimak
avimak

Reputation: 1628

In your case, I would just extend a preferable ViewGroup, inflate an XML - for styling and positioning Views - into it, grab an access to those View, add all relevant set/get methods, and that's it. If relevant, you can also define attributes for your component to adjust its custom functionalities while using it in XMLs.

Upvotes: 2

Related Questions