Reputation: 125
I am trying to reproduce the IOS UIPickerView functionality but in android.
In IOS you can set this UIPickerView as an input type of the keyboard. I need this same type of interaction but in android.
UPDATE: When I say input type I mean the UIPickerView simply replaces the keyboard view. So if a textfield were clicked the UIPickerView would open instead of the Keyboard. Hope that clears stuff up!
Upvotes: 7
Views: 7041
Reputation: 1
You can use a number picker, if you're looking for a similar feel. Just make sure to add a personalized style as theme (or else it will take your app theme as default) and add a formatter to the number picker. You'll run into some trouble on the first render (it won't show the first value until you scroll), just use the solution provided here: Android NumberPicker with Formatter doesn't format on first rendering
You can also put it into a cardview like in the image, it looks tidy enough. formatted number picker screenshot
Ps.: Actually you can modify several fields, but you have to acces the class fields/methods. To do so, do as this exmaple code.
public void setNumberPickerDivider (NumberPicker picker) {
Field[] fields = NumberPicker.class.getDeclaredFields();
for(Field f:fields){
if(f.getName().equals("mSelectionDividerHeight")){
f.setAccessible(true);
try {
f.set(picker,1);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
}
Upvotes: 0
Reputation: 1769
I would suggest making a layout with multiple pickers, that way you can have as many as you need and initialise them with any values you require.
The below layout will give you two spinners side by side similar to the iOS UIPickerView.
I've posted a link to my github repo at the bottom with a simple example showing how to use 3 spinners, one for minutes, hours and days that should help you (Or anyone else coming from iOS with the same question) can use as a base to get started.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/multispinner">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<Spinner
android:id="@+id/spinner1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"/>
<Spinner
android:id="@+id/spinner2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
My simple multiple picker example
Upvotes: 0
Reputation: 73936
not sure exactly what you mean by
as an inputtype of the keyboard
but the android equivalent to that image would be the TimePickerDialog
Update
If you want that picker type of UI you have 3 options for pickers, the TimePicker, DatePicker or the NumberPicker obviously the first 2 are out so you are left with the NumberPicker
with the number picker you can populate it with the numbers you want (inches) and then another for feet
I suggest you take a quicl look at the Pickers article https://developer.android.com/guide/topics/ui/controls/pickers.html
Upvotes: 2