Mahmoud Ahmed
Mahmoud Ahmed

Reputation: 17

How to make Number Picker increase by Specific value in android with good design

i have to make NumberPicker to make user choose between 0 - 4 but not increase every time by one ...
i need to increase it by 0.05 like (0.05 - 0.1 - 0.15 - .... - 3.95 - 4)
and which attributes make numberpicker look good in design
Thanks in advance

main_xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">
<NumberPicker
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/pick">
        </NumberPicker>
</LinearLayout>

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grades_scale);
        NumberPicker numberPicker=(NumberPicker)findViewById(R.id.pick);
        numberPicker.setMinValue(0);
        numberPicker.setMaxValue(4);
        numberPicker.setWrapSelectorWheel(false);

    }
}

Upvotes: 0

Views: 2039

Answers (1)

abdul khan
abdul khan

Reputation: 863

you need to format NumberPicker for a "0.05" count difference between values.

 NumberPicker.Formatter formatter = new NumberPicker.Formatter() {
                @Override
                public String format(int value) {
                    int diff = value * 0.05;
                    return "" + diff;
                }
            };
            numberPicker.setFormatter(formatter);

and then set max and min value.

numberPicker.setMinValue(0);
numberPicker.setMaxValue(4);

Answer from https://stackoverflow.com/a/30469826/4565874

Upvotes: 2

Related Questions