Search code examples
javaandroiduipickerviewpickernumberpicker

Android - Weight Picker


I come from iOS and Swift and I don't really know how I could replace simply the PickerView.

In my app, the user should be able to define its weight (for example 75.6kg).

The best way to do that seems to be like that: enter image description here

Given that I'm new in Android I don't really know how to reproduce that. I made some research and it seems to be a NumberPicker? But I don't know how I could have these two distincts columns.


Solution

  • enter image description here

    You can achieve something similar with a little customization through the usage of two NumberPickers positioned side by side:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal">
    
        <NumberPicker
            android:id="@+id/first_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/CustomNumberPickerTheme" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="."
            android:textSize="24sp" />
    
        <NumberPicker
            android:id="@+id/second_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/CustomNumberPickerTheme" />
    </LinearLayout>
    

    To better match the screenshot a TextView was included in between the two NumberPickers as the decimal point along with a custom theme CustomNumberPickerTheme which you'll need to set inside res/values/styles.xml

    <resources>
    
        ...
    
        <style name="CustomNumberPickerTheme" parent="AppTheme">
            <item name="colorControlNormal">@android:color/transparent</item>
        </style>
    </resources>