Search code examples
androidandroid-spinnerandroid-alertdialogcustomdialog

how to add a widget which has two spinner items and put in a single dialog in android


I am trying to design confirmation dialog which has two lists (scrollable). One part shows numbers of foot and another shows inches values. This should be in a single dialog as shown in the image below. Any suggestions to do this is appreciated.

see this image

I achieved this UI by using the following code
mycustomwidget.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:text="@string/txtHeight"
        android:id="@+id/tvHeight" />
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.50"
        android:id="@+id/editText_Height"
        android:hint="select Height"
        android:paddingBottom="8dp"
        android:background="@drawable/border_bottom" />
</LinearLayout>

in my Activity is used this code to get the dialog

editText_Height.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final Dialog dialog = new Dialog(ProfileInfo.this);
    dialog.setContentView(R.layout.height_picker);
    dialog.show();
    }
});

heightpicker.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="250dp"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtHeight"
        android:layout_marginTop="24dp"
        android:textAlignment="center"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="@drawable/border_bottom">
        <com.myapp.HelperClasses.MyNumberPicker
            android:id="@+id/footValue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:orientation="vertical"
            max="7"
            min="4" />
        <com.myapp.HelperClasses.MyNumberPicker
            android:id="@+id/inchValue"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.5"
            android:layout_gravity="center"
            android:orientation="vertical"
            max="11"
            min="0" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/textView_cancel"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="36dp"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="24dp"
            android:text="@string/txtCancel"
            android:textAlignment="center"
            android:textColor="@color/blue"/>
        <TextView
            android:id="@+id/textView_Okay"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="32dp"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:text="@string/txtOkay"
            android:textAlignment="center"
            android:textColor="@color/blue"/>
    </LinearLayout>
</LinearLayout>

Solution

  • You can use NumberPicker like this way

    <NumberPicker
            android:id="@+id/numberPicker1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"  />
    

    Java

    public class MainActivity extends Activity {
    
        NumberPicker np;
        TextView tv1, tv2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            np = (NumberPicker) findViewById(R.id.numberPicker1);
            tv1 = (TextView) findViewById(R.id.textView2);
            tv2 = (TextView) findViewById(R.id.textView3);
    
            np.setMinValue(0);
            np.setMaxValue(10);
            np.setWrapSelectorWheel(false); 
    
            np.setOnValueChangedListener(new OnValueChangeListener() {
    
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                // TODO Auto-generated method stub
    
                String Old = "Old Value : ";
                String New = "New Value : ";
    
                tv1.setText(Old.concat(String.valueOf(oldVal)));
                tv2.setText(New.concat(String.valueOf(newVal)));
            }
        });
        }