androidandroid-alertdialogandroid-number-picker

start number picker with previously selected value


I am using alert dailog as number picker,i am able to select value and set it to my textview,but what i am trying is,if i select suppose 5 from number picker,then it will set to textview and again if i click on textview then 5 must be focused or selected to numberpicker

 public void qtyshow()
    {

        final AlertDialog.Builder alert = new AlertDialog.Builder(ProductView.this);

        alert.setTitle("Select the Qty: ");

        final NumberPicker np = new NumberPicker(ProductView.this);


        np.setMaxValue(20); // max value 20
        np.setMinValue(1);   // min value 0
        np.setWrapSelectorWheel(false);

        alert.setView(np);


        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                proqty.setText(String.valueOf(np.getValue())); //set the value to textview
                // Do something with value!



                Float mulprice=Float.parseFloat(packlist.get(0).getProductPack_SP());
                int mulqty= np.getValue();

                Float total=mulprice*mulqty;
                packsp.setText("$" + String.format("%.2f%n", total));
                System.out.println("price test" + packlist.get(0).getProductPack_SP() + "," + mulprice + "," + mulqty + "," + String.format("%.2f%n", total));
                Float mulelseprice=Float.parseFloat(packlist.get(0).getProductPack_ElseWhere());
                Float totalelse=mulelseprice*mulqty;

                packelsewhere.setText("$" + String.format("%.2f%n", totalelse));
            }
        });

        alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                // Cancel.

            }
        });

        alert.show();


    }

Solution

  • you can do like this:

    mNumberPicker.setValue(number);