setLayoutParams on multiple layouts - java

I need to change the value topMargin of my layouts programmatically. I tried the following:
android.widget.LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(
spaceBtnWidth, buttonHeight);
lp_ll.height = LayoutParams.WRAP_CONTENT;
lp_ll.width = LayoutParams.WRAP_CONTENT;
lp_ll.gravity = Gravity.CENTER_HORIZONTAL;
lp_ll.topMargin = rowDistance;
ll3.setLayoutParams(lp_ll);
ll4.setLayoutParams(lp_ll);
ll5.setLayoutParams(lp_ll);
I want all my 3 layouts ll3, ll4 and ll5 to use the same properties from lp_ll. The problem is now that only my first layout ll3 takes it. The other two don't change. I even tried a different order and put the line where I set ll4 above the ll3 and ll5 lines. Anyways ll3 is being set and the other two don't change their properties.
Thanks

for every layout create a copy of lp_ll
ll4.setLayoutParams(new LinearLayout.LayoutParams(lp_ll));
ll5.setLayoutParams(new LinearLayout.LayoutParams(lp_ll));

You can simply edit your code like this:
android.widget.LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(
spaceBtnWidth, buttonHeight);
lp_ll.height = LayoutParams.WRAP_CONTENT;
lp_ll.width = LayoutParams.WRAP_CONTENT;
lp_ll.gravity = Gravity.CENTER_HORIZONTAL;
lp_ll.topMargin = rowDistance;
ll3.setLayoutParams(lp_ll);
ll4.setLayoutParams(ll3.getLayoutParams());
ll5.setLayoutParams(ll3.getLayoutParams());
It will still do the same, i hope this helps.

Related

How to set the margin on the button I am creating in java activity?

Button button = new Button(All_HomeWork_List.this);
button.setBackground(getDrawable(R.drawable.orange_button) );
button.setTextSize(25);
buttonLay.addView(button);
You should use LayoutParams to set your button margins:
LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);
#mokhtar halim Sorry this code is not working at the moment. please check photo,,,,setMargins problem showing here

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

putting two label beside each other in gwt

Code:
RootPanel footer = RootPanel.get("footer");
footer.addStyleName(StyleNameGen.createName("mainWidgetFooterStyle"));
footer.add(versionLabel);
footer.add(expirationMessage);
and it puts the versionLabel and thenexpirationMessage `below it.
What to do if I want to put the versionLabelbesides the expirationMessage?
You have to add them both to another container first like a HorizontalLayoutContainer or an HBoxLayoutContainer.
HorizontalLayoutContainer labelContainer = new HorizontalLayoutContainer()
labelContainer.add(versionLabel, new HorizontalLayoutData(-1, -1, new Margins(5)));
labelContainer.add(expirationMessage, new HorizontalLayoutData(-1, -1, new Margins(5)));
footer.add(labelContainer);
If I properly understand you need to place 2 label one over another.
For this you can try:
FlowLayoutContainer somePanel = new FlowLayoutContainer();
mixCont.add(somePanel);
Label versionLabel = new Label("12345678");
Label expirationMessage = new Label("9999999");
versionLabel.getElement().getStyle().setFloat(Style.Float.LEFT);
expirationMessage.getElement().getStyle().setPosition(Position.ABSOLUTE);
expirationMessage.getElement().getStyle().setLeft(20, Unit.PX);
somePanel.add(versionLabel);
somePanel.add(expirationMessage);
Actually next code should solve your problem. Second row helps you in positioning your overlapping message:
expirationMessage.getElement().getStyle().setPosition(Position.ABSOLUTE);
expirationMessage.getElement().getStyle().setLeft(20, Unit.PX);

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

Categories

Resources