0

I want to add aEditText below aNumberPicker in the below code, currently aEditText is appearing in the top left corner of the alert, how would you manage to get it below aNumberPicker

     public void onClickNotes(View view)
 { 
     Context mContext = this;
     RelativeLayout linearLayout = new RelativeLayout(mContext);
     final NumberPicker aNumberPicker = new NumberPicker(mContext);
     final EditText aEditText = new EditText(mContext);
     aNumberPicker.setMaxValue(50);
     aNumberPicker.setMinValue(1);


     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
     RelativeLayout.LayoutParams numPickerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     RelativeLayout.LayoutParams EditParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
     numPickerParams.addRule(RelativeLayout.CENTER_HORIZONTAL);


     linearLayout.setLayoutParams(params);
     linearLayout.addView(aNumberPicker,numPickerParams);
     EditParams.addRule(RelativeLayout.ABOVE, 0);
     linearLayout.addView(aEditText,EditParams);

     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
     alertDialogBuilder.setTitle("Entry Index");
     alertDialogBuilder.setView(linearLayout);
     alertDialogBuilder
             .setCancelable(false)
             .setPositiveButton("Select",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                             int id) {
                            //aNumberPicker.getValue();
                         }
                     })
             .setNegativeButton("Cancel",
                     new DialogInterface.OnClickListener() {
                         public void onClick(DialogInterface dialog,
                                             int id) {
                             dialog.cancel();
                         }
                     });
     AlertDialog alertDialog = alertDialogBuilder.create();
     alertDialog.show();
 }

1 Answers1

0

Try to change:

EditParams.addRule(RelativeLayout.ABOVE, 0);

to

EditParams.addRule(RelativeLayout.BELOW, aNumberPicker.getId());

Reference: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)

UPDATE
You need also to setId(..) to aNumberPicker directly, before getting it. So, paste this code after aNumberPicker instantiation:

aNumberPicker.setId(generateViewId());

Source code of method generateViewId() got from here: Android: View.setID(int id) programmatically - how to avoid ID conflicts?

private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
    @SuppressLint("NewApi")
    public static int generateViewId() {
        if (Build.VERSION.SDK_INT < 17) {
            for (;;) {
                final int result = sNextGeneratedId.get();
                // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
                int newValue = result + 1;
                if (newValue > 0x00FFFFFF)
                    newValue = 1; // Roll over to 1, not 0.
                if (sNextGeneratedId.compareAndSet(result, newValue)) {
                    return result;
                }
            }
        } else {
            return View.generateViewId();
        }
    }

Of course, you can set Id to a specific value, like 1, 2, 38432, but you should avoid id collisions, and this method (added in android API 17) is modified for use on all API versions.

Hope it helps

Community
  • 1
  • 1
krossovochkin
  • 12,030
  • 7
  • 31
  • 54
  • Also, in this case EditText will be below NumberPicker, but aligned to the left. You can set "RelativeLayout.LayoutParams EditParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);" to stretch EditText by width – krossovochkin May 18 '14 at 08:49