Reputation: 232
I am trying to show two number picker on dialog using java only, The code is working but i am not able to arrange it on equal width.
here is my code
RelativeLayout relative = new RelativeLayout(mContext);
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
final NumberPicker aNumberPickerA = new NumberPicker(mContext);
aNumberPickerA.setMaxValue(11);
aNumberPickerA.setMinValue(1);
aNumberPickerA.setDisplayedValues(new String[] { "Tea Cup", "Glass","Plate","Small Plate","Cutlets","Medium","Piece","Katori","Balls","Serving","egg"});
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50);
RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams qPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
numPicerParams.addRule(RelativeLayout.CENTER_IN_PARENT);
qPicerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relative.setLayoutParams(params);
relative.addView(aNumberPicker,numPicerParams);
relative.addView(aNumberPickerA,qPicerParams);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Select the number");
alertDialogBuilder.setView(relative);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
Log.e("","New Quantity Value : "+ aNumberPicker.getValue());
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
I want to align them on equal width.
snapshot
Upvotes: 2
Views: 5182
Reputation: 1060
You are setting RelativeLayout.LayoutParams on views which are added to LinearLayout. You should use RelativeLayout in place of LinearLayout as parent of NumberPicker. Setting the RelativeLayout.LayoutParams won't work on LinearLayout.
Edit:
I would have used a LinearLayout as parent with setting orientation to Horizontal and children weights to '1'. Which would be something like this...
LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
//
final NumberPicker aNumberPicker = new NumberPicker(mContext);
aNumberPicker.setMaxValue(50);
aNumberPicker.setMinValue(1);
//
final NumberPicker aNumberPickerA = new NumberPicker(mContext);
aNumberPickerA.setMaxValue(11);
aNumberPickerA.setMinValue(1);
aNumberPickerA.setDisplayedValues(new String[] { "Tea Cup", "Glass","Plate","Small Plate","Cutlets","Medium","Piece","Katori","Balls","Serving","egg"});
//
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(50, 50);
params.gravity = Gravity.CENTER;
//
LinearLayout.LayoutParams numPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
numPicerParams.weight = 1;
//
LinearLayout.LayoutParams qPicerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
qPicerParams.weight = 1;
//
LL.setLayoutParams(params);
LL.addView(aNumberPicker,numPicerParams);
LL.addView(aNumberPickerA,qPicerParams);
I hope this helps.
Good Luck. :)
Upvotes: 9