I've created a custom component that looks and works similar to the iPhone's
UIPicker. For a single wheel, I used an excellent solution available from
this project: http://code.google.com/p/android-wheel/ (although I trimmed it
down and added a couple of features). What I wanted to do is to create a
single component that could be very easily placed in any layout, like so:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";

android:id="@+id/layout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<com.test.Meter

android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:gravity="center_vertical|center_horizontal"

/>

</LinearLayout>

The component would then be given the number of dials to display in the
code, and it would automatically adjust the size of the dials to the screen
density.

All of this was (mostly) done, and the custom component works fine on
Android 2.2 and 2.3. However, on the earlier platforms the above layout
produces an empty screen. My guess is that the earlier versions of the
platform notice that com.test.Meter extends a LinearLayout (I will paste in
the code for this component below) and that it has no children, so they just
'optimise it out', don't bother displaying it at all. I think so because one
workaround that I've found is to add a dummy view inside of the Meter, then
override the dispatchDraw on the component. Then the component is displayed,
but probably the dummy view interferes with the layout because the first of
the dials doesn't scroll properly.

What I am looking for is a way to suppress any pre-2.2 layout optimisation
and get my component displayed without funny workarounds. Any help will be
highly appreciated.

Here goes all the relevant code.

First the Meter component itself (note that Dial is my modified version of
the kankan.wheel.widget.WheelView from the project referenced above; I could
potentially post its code but it is over 400 lines):

package com.test;


import java.util.ArrayList;

import java.util.List;


import android.content.Context;

import android.graphics.Canvas;

import android.util.AttributeSet;

import android.view.ViewGroup;

import android.widget.LinearLayout;


public class Meter extends LinearLayout {

 // TODO: to be defined at runtime

private final static int NUMBER_OF_DIALS = 6;

 private Context context;

private List<Dial> dials;

private int dialWidth;

 public Meter(Context ctx) {

super(ctx);

setWillNotDraw(false);

context = ctx;

}


public Meter(Context ctx, AttributeSet attrs) {

super(ctx, attrs);

setWillNotDraw(false);

context = ctx;

}

 @Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

 int width = MeasureSpec.getSize(widthMeasureSpec);

dialWidth = width / NUMBER_OF_DIALS;

}

 protected void onLayout (boolean changed, int l, int t, int r, int b) {

super.onLayout(changed, l, t, r, b);

 if (dials == null) {

dials = new ArrayList<Dial>();

for (int i = 0; i < NUMBER_OF_DIALS; i++) {

Dial d = new Dial(context);

d.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.
WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

d.setRequiredWidth(dialWidth);

d.setWillNotDraw(false);

dials.add(d);

this.addView(d);

}

}

}

 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {

return super.checkLayoutParams(p);

}

 protected LinearLayout.LayoutParams generateLayoutParams
(ViewGroup.LayoutParams p) {

return super.generateLayoutParams(p);

}

}


This is the trick that makes the component displayed on Android 1.6. First I
change the layout to look like so:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android";

android:id="@+id/layout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

<com.test.Meter

android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:gravity="center_vertical|center_horizontal"

>

<View

android:layout_width="1px"

     android:layout_height="1px"

/>

</com.test.Meter>

</LinearLayout>

Then I add this method to the Meter:

protected void dispatchDraw (Canvas canvas) {

super.dispatchDraw(canvas);

 for (Dial d : dials) {

d.draw(canvas);

}

}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to