I have a slightly modified NumberPicker that I use and when the activity starts, the default value is already selected. This is annoying because it brings up the keyboard or brings up the dialog to cut/copy/paste. I did instruct the keyboard to stay down but it is still selected and will bring up the cut/copy/paste dialog. Once I scroll through the values the number is no longer selected and it acts normally. Does anyone know how to set the NumberPicker to not have anything selected at startup?
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class NumPicker extends NumberPicker
{
public NumPicker(Context context)
{
super(context);
}
public NumPicker(Context context, AttributeSet attrs)
{
super(context, attrs);
processAttributeSet(attrs);
}
public NumPicker(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
processAttributeSet(attrs);
}
/**
* Reads the xml, sets the properties
*/
private void processAttributeSet(AttributeSet attrs)
{
setMinValue(attrs.getAttributeIntValue(null, "min", 0));
setMaxValue(attrs.getAttributeIntValue(null, "max", 0));
setValue(4);
}
}
This is how it is used in the xml:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp"
android:layout_marginTop="40dp"
style="@style/CustomText"
max="100"
min="2"
The reason this happens is that the NumberPicker is the first (or only?) focusable element in your activity. You can trap the focus to your main layout view by adding this attribute to the root view:
android:focusableInTouchMode="true"