DatePickerDialog change the dialog size - java

I need to show the DatePickerDialog in my apps. I would like to change the dialog size to bigger.
I tried below code.
WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.gravity = Gravity.TOP;
params.width = 1000; // dialogWidth;
params.height = 1000; // dialogHeight;
dialog.getWindow().setAttributes(params);
But this is not work. So anybody know how can change the dialog size.
Thanks a lots.

Related

Android programmatically setting up layout for different screen sizes/densities

I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.
Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.
Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:
public static int convertToPixels(int value) {
Resources resources = mContext.getResources();
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
return x;
}
Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.
If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setId(Integer.parseInt(txn.getId().toString()));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
params.setMargins(0, convertToPixels(1), 0, 0);
row.setLayoutParams(params);
row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
LinearLayout imageLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
imageParams.gravity = Gravity.CENTER;
imageLayout.setLayoutParams(imageParams);
ImageView image = new ImageView(mContext);
if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
}
imageLayout.addView(image);
row.addView(imageLayout);
LinearLayout txnMiddleLayout = new LinearLayout(mContext);
txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
txnMiddleLayout.setLayoutParams(txnTopParams);
TextView txnTopContents = new TextView(mContext);
txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnTopContents.setText(txn.getTopLineContents());
txnTopContents.setTextColor(Color.BLACK);
// txnTopContents.setTextSize(convertToPixels(16));
TextView txnBottomContents = new TextView(mContext);
txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnBottomContents.setText(txn.getBottomLineContents());
// txnBottomContents.setTextSize(convertToPixels(12));
txnMiddleLayout.addView(txnTopContents);
txnMiddleLayout.addView(txnBottomContents);
row.addView(txnMiddleLayout);
LinearLayout txnBottomLayout = new LinearLayout(mContext);
txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
txnBottomLayout.setLayoutParams(txnBottomParams);
TextView amount = new TextView(mContext);
amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
amount.setText(txn.getAmountStr());
amount.setTextColor(Color.BLACK);
// amount.setTextSize(convertToPixels(16));
TextView date = new TextView(mContext);
date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
date.setText(txn.getDateStr());
// date.setTextSize(convertToPixels(12));
txnBottomLayout.addView(amount);
txnBottomLayout.addView(date);
row.addView(txnBottomLayout);
txnList.addView(row);
I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.
https://stackoverflow.com/a/3996104/4056947

Adjusting height of linear layout programmatically

Whenever I try setting the height of a LinearLayout, I always get this exception:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
Here is my code:
LinearLayout.LayoutParams hide = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
LinearLayout.LayoutParams show = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
driverMainOptions.setLayoutParams(hide);
mapDirections.setLayoutParams(show);
Is there a specific import statement I need for this to execute properly?
This should work
LinearLayout lLayout = new LineaLayout(context);
LayoutParams params = lLayout.getLayoutParams();
params.height = 200;
params.width = 200;
lLayout.setLayoutParams(params);
see the accepted answer at Android: How to Programmatically set the size of a Layout

Linear layout dividing programmatically

for (int i = 0; i < c.getCount(); i++)
{
ChildLayout=new LinearLayout(getActivity());
ChildLayout.setWeightSum(100);
ChildLayout.setOrientation(LinearLayout.HORIZONTAL);
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
ChildLayout.setId(i);
Prd_txt = new TextView(getActivity());
Prd_txt.setId(c.getInt(3));
ProdStr_All.add(c.getString(4));
ProdTxt_All.add(Prd_txt);
Prd_txt.setPadding(8, 0, 0, 0);
edit_phy = new EditText(getActivity());
edit_phyAll.add(edit_phy);
edit_phy.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
edit_phy.setId(Dist_cat[k]);
edit_phy.setGravity(Gravity.RIGHT);
edit_trn = new EditText(getActivity());
edit_trnAll.add(edit_trn);
edit_trn.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
edit_trn.setId(Dist_cat[k]);
edit_trn.setGravity(Gravity.RIGHT);
Total=new TextView((getActivity()));
tot_all.add(Total);
Total.setId(k);
Total.setGravity(Gravity.RIGHT);
Total.setTextSize((global_variables.ConvertPixels(getActivity(), 19)));
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,50);
LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT,16);
ChildLayout.addView(Prd_txt, 0,layoutParams2);
ChildLayout.addView(edit_phy, 1,layoutParams3);
ChildLayout.addView(edit_trn, 2,layoutParams3);
ChildLayout.addView(Total, 3,layoutParams3);
if(i%2==0)
{
ChildLayout.setBackgroundResource(R.color.colortablelight);
}else
{
ChildLayout.setBackgroundResource(R.color.colortablemedium);
}
MainLayout.addView(ChildLayout,layoutParams);
c.moveToNext();
}
for(int m=0;m<ProdTxt_All.size();m++)
{
ProdTxt_All.get(m).setText(ProdStr_All.get(m));
}
output:
Problem:
I want to divide my linear layout in fixed size of Product weightsum 50 and rest
16 but could not achieve that, Tried to add height and width, but in different resolution it shows differently. So tried for weightsum. How to solve?
#Rashmi S
What is the problem doing that on XML? since its easier to handle especially with UI Designer software or what you called that and its more cleaner from the perspective of maintainability and readability. Thats only my opinion, in case you got some reason to do that, possibly there's an alternative aproach.
By using WRAP_CONTENT and weight at the same time, LinearLayout will calculate and prepare enough space for content before taking care of the weight. That's why sometimes you see this warning when setup views inside design layout with xml.
To ensure the parent view separates childs by weight properly, setup height = 0 (VERTICAL) as well as width = 0 (HORIZONTAL) before weight.
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,50);
LinearLayout.LayoutParams layoutParams3 = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,16);

How to get margins of a TextView?

I'm trying to get top and left margins of a textview which I gave hardcoded margins.
Why I'm doing this? Because I wanna loop through all the textviews I placed BELOW this textview so I can programmaticaly add radiobutton groups at these exact margins.
textView[0] = new TextView(context);
relativeLayoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
textView[0].setId(1); // changed id from 0 to 1
textView[0].setText(naam[0].toUpperCase());
relativeLayoutParams.setMargins(24, 39, 0, 0);
int tyo = relativeLayoutParams.getTop(); //Trying to get top margins.
Well you can simply to it the other way around. Just get the layout parameters.
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) textView.getLayoutParams();
Then you can access the margins via global variables.
lp.topMargin
lp.leftMargin
lp.bottomMargin
lp.rightMargin
Try this
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.CENTER_VERTICAL);
RelativeLayout.LayoutParams textParams;
textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textParams.addRule(RelativeLayout.RIGHT_OF, id_of_left_content);
textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, id_of_top_content);
textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, id_of_bottom_content);
textParams.addRule(RelativeLayout.CENTER_VERTICAL);
i hope this concept will help you

Android: What happens when you change the dimensions of a frameLayout that is set to wrap_content programmatically?

I have a FrameLayout in my application and in the xml file its width and height is set to wrap_content . then I want to change its dimensions programmatically like this :
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
ViewGroup.LayoutParams previewParam = preview.getLayoutParams() ;
previewParam.height =(int) (height * 0.86);
previewParam.width = (int)(previewParam.height/ratio) ;
preview.setLayoutParams(previewParam) ;
When I do this the program exits when I start this activity, is the problem with wrap_content or not ?
I believe its because previewParam is null at that point. Try doing it like this:
FrameLayout preview = (FrameLayout) findViewById(R.id.abs__action_bar);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((height/ratio),
(int) (height * 0.86));
preview.setLayoutParams(params);
preview.requestLayout();

Categories

Resources