0

I have create a number picker using this code - Number Picker

But the output looks something like this -

enter image description here

But I want it to look something like -

enter image description here

I want to display the Cancel and set buttons as given in image.

But when I try this code, it shows error - The method setButton(int, String, Dialog) is undefined for the type Dialog.

Dialog noPicker = new Dialog(MainActivity.this);
noPicker.setButton(Dialog.BUTTON_POSITIVE, "Ok", noPicker);
noPicker.setButton(Dialog.BUTTON_NEGATIVE, "Cancel", noPicker);

Any ideas on how to resolve this?

Community
  • 1
  • 1
Confuse
  • 5,646
  • 7
  • 36
  • 58
  • we need more code her, can you pls post your layout.xml – Westranger Sep 11 '14 at 12:01
  • The xml layout is there in the link given. Anyways here it is - http://stackoverflow.com/questions/17805040/how-to-create-a-number-picker-dialog/17806895#17806895 – Confuse Sep 11 '14 at 12:01
  • use DialogFragment with number picker http://developer.android.com/guide/topics/ui/dialogs.html#DialogFragment – mcd Sep 11 '14 at 12:20

2 Answers2

1

The idea is to wrap the buttons into a linear layout an to place it at the bottom. then you need to weight the buttons equally. The code must be similar to this one:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:weightSum="2"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Set" />

    </LinearLayout>
</RelativeLayout>
Westranger
  • 1,308
  • 19
  • 29
0

Set the theme of dialog, it should work: Below sample code may help

Dialog dialog=new Dialog(context, theme)

and define theme in style.xml file. It will be like:

<style name="DoneCancelBar" parent="android:Theme.Light">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
    <item name="android:orientation">horizontal</item>
</style>
Pankaj
  • 833
  • 12
  • 35