Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Number Picker Dialog?

My Requirement is like whenever user select any number from number picker on that time that number should be printed on some text view and second time whenever user opens dialog again than previously selected number should be shown on that dialog

    ...
    ...

    view = (TextView) findViewById(R.id.textView7);

    LinearLayout L = (LinearLayout) findViewById(R.id.linearLayout5);
    L.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showdialog();
        }
    });
}

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
         Log.i("value is",""+newVal);
}

public void showdialog()
{
    Dialog dialog = new Dialog(NameActivity.this);
    View npView = getLayoutInflater().inflate(R.layout.multidialog, null);
    final NumberPicker firPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker1);
    firPicker.setMaxValue(9);
    firPicker.setMinValue(0);
    final NumberPicker secPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker2);
    secPicker.setMaxValue(9);
    secPicker.setMinValue(0);
    final NumberPicker tirPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker3);
    tirPicker.setMaxValue(9);
    tirPicker.setMinValue(0);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("title");
    builder.setView(npView);
    builder.setPositiveButton("Set",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            view.setText(String.valueOf(firPicker.getValue() *100 
                    + secPicker.getValue() *10 + tirPicker.getValue()));
        }
    });

    builder.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    dialog = builder.create();
    dialog.show();
}
like image 482
user3363368 Avatar asked Mar 05 '14 04:03

user3363368


1 Answers

public class sample extends Activity{

NumberPicker MyNumPicker1, MyNumPicker2, MyNumPicker3;
TextView txtMyText;
AlertDialog alertdialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sample_layout);

    txtMyText = (TextView) findViewById(R.id.txtMyText);
    txtMyText.setText("5");

    txtMyText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v1 = inflater.inflate(R.layout.numpicker, null);
                MyNumPicker1 = (NumberPicker) v1.findViewById(R.id.MyNunPicker1);
                MyNumPicker2 = (NumberPicker) v1.findViewById(R.id.MyNunPicker2);
                MyNumPicker3 = (NumberPicker) v1.findViewById(R.id.MyNunPicker3);

                MyNumPicker1.setMaxValue(20);
                MyNumPicker1.setMinValue(1);
                MyNumPicker1.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker1.setWrapSelectorWheel(true);

                MyNumPicker2.setMaxValue(20);
                MyNumPicker2.setMinValue(1);
                MyNumPicker2.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker2.setWrapSelectorWheel(true);

                MyNumPicker3.setMaxValue(20);
                MyNumPicker3.setMinValue(1);
                MyNumPicker3.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker3.setWrapSelectorWheel(true);


                AlertDialog.Builder builder = new AlertDialog.Builder(sample.this);

                builder.setView( v1 );
                builder.setTitle("Select Number");
                builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int NumVal1 = MyNumPicker1.getValue();
                        int NumVal2 = MyNumPicker2.getValue();
                        int NumVal3 = MyNumPicker3.getValue();

                        txtMyText.setText(""+ ( (NumVal1 * 100) + (NumVal2 * 10) + NumVal3) );
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertdialog.dismiss();

                    }
                });

                alertdialog = builder.create();
                alertdialog.show();

            }
        });
}

}

here's the number_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<NumberPicker
    android:id="@+id/MyNunPicker2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<NumberPicker
    android:id="@+id/MyNunPicker3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/MyNunPicker2" />

<NumberPicker
    android:id="@+id/MyNunPicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/MyNunPicker2"/>

</RelativeLayout>
like image 131
kevz Avatar answered Sep 21 '22 05:09

kevz