Setting LayoutParams but its not working android - android-layout

Trying to create a linear layout programmatically and setting its width and height by layout params. But it seems layout params isn't working.
this is the code:
// CREATING A NEW LINEAR LAYOUT PROGRAMMATICALLY
LinearLayout linearLayout = new LinearLayout(getActivity());
ViewGroup.LayoutParams layoutParams = new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// CREATING CHILDDRENS (TEXT VIEWS)
TextView name = new TextView(getContext(), null, 0, R.style.item_layout_style);
name.setText("Pine");
TextView qty = new TextView(getContext(), null, 0, R.style.item_layout_style);
qty.setText("10");
TextView cost = new TextView(getContext(), null, 0, R.style.item_layout_style);
cost.setText("785");
TextView tCost = new TextView(getContext(), null, 0, R.style.item_layout_style);
tCost.setText("1000");
// SET TEXT VIEW TO LINEAR LAYOUT
linearLayout.addView(name);
linearLayout.addView(qty);
linearLayout.addView(cost);
linearLayout.addView(tCost);
// SET LINEAR LAYOUT TO PINE LAYOUT
LinearLayout daddy= (LinearLayout) view.findViewById(R.id.layout);
daddy.addView(linearLayout, 2);
// Return the view
return view;
I have root layout (daddy) which has many linear layouts (vertically oriented), but I need to create a linear layout programmatically and add that to "daddy". But the text views are sticked together, they aren't getting the entire space horizontally.
Do help me!

Everything is fine with the code. It is the style that I tried to give to text views, they weren't getting layout weight, width and height. How did I find this out? Well I set the linear layout's background-color to black to see if it's really not getting width and height set by LayoutParams. And I wasn't wrong! Width was set to match parent. So what I did was create a new layout param for text views and set it to them.

Related

Layout composition varying between preview and running app in CN1

I configured a layout with the following result:
The Layout of the Form's contentpane is TableLayout which consists of one cell. The contentpane has only one container as child for which I set the layoutConstraint with the following modified values: width = 100%, height = 100%, Align = CENTER, Vertical Align = CENTER.
In other words, I'm centering the components of the form.
In the preview it looks as expected but not when the app runs in the simulator!
Note:
I'm not modifiying any style or layout properties in my code! Thus I have no idea why it's not as intended in the simulator.
The approach from How to show two or more label at centre of the container in codenameone didn't work for me!
this is my code concerning that form:
#Override
protected void postIntroLangSelect(Form f) {
f.getTitleArea().setHidden(true);
f.revalidate();
// button configuration
Button btnDE = findButtonDE(f);
Button btnTR = findButtonTR(f);
btnDE.addActionListener((e) -> {
settings.setLocale(Lang.GERMAN);
showNextForm();
});
btnTR.addActionListener((e) -> {
settings.setLocale(Lang.TURKISH);
showNextForm();
});
}
Set your form's layout to BorderLayout and check the Absolute Center, then add a container on the form with its layout constraint set to Center. Give this container a BoxLayout Y layout and add all your other components in it.
If this approach didn't work for you, then you will have to hand-code your form and set it's Layout to new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER).

Align textView next to another one programatically

I am programmatically creating a text view and trying to place another textView right next to the first one. But I am unable to do it.
Here is the code I have written,
//1st textview
TextView itemText = new TextView(context);
itemText.setText(mItemText);
Typeface itemFont = Typeface.createFromAsset(context.getAssets(), "fonts/" + "Roboto" + ".ttf");
itemText.setTypeface(itemFont,Typeface.BOLD);
itemText.setPadding(0, padding, 0, 0);
itemText.setId(10);
RelativeLayout.LayoutParams itemTextParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
itemTextParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
itemText.setTextSize(font_size);
itemText.setLayoutParams(itemTextParams);
//2nd text view
TextView seperator = new TextView(context);
seperator.setText(mSeperator);
seperator.setPadding(0,padding,0,0);
seperator.setTypeface(null,Typeface.BOLD);
RelativeLayout.LayoutParams seperatorParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
seperatorParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId());
seperatorParams.addRule(RelativeLayout.CENTER_VERTICAL);
seperator.setLayoutParams(seperatorParams);
seperatorParams.addRule(RelativeLayout.CENTER_HORIZONTAL) works, but when I use seperatorParams.addRule(RelativeLayout.RIGHT_OF,itemText.getId()) , the text is not shown.
Can anyone point out where I am going wrong? Or is there any other way to do this?
The width of itemTextParams is set to MATCH_PARENT, so there's no space to put anything to the right of it. Change it to WRAP_CONTENT or define a width value.

JavaFX scene builder questions

I'm working with JavaFX scene builder and have two questions.
Fisrt one:"How to add border to Pane in JavaFX scene builder?"
Second one: "How to split cells in HBox?"
I dont know why you would want to join HBox cells as you can set the resize behaviour for every child of the hbox.
There is a example in HBox's Javadoc:
//For example, if an hbox needs the TextField to be allocated all extra space:
HBox hbox = new HBox();
TextField field = new TextField();
HBox.setHgrow(field, Priority.ALWAYS);
hbox.getChildren().addAll(new Label("Search:"), field, new Button("Go"));
Joining cells would be possible in a GridPane with the row- and/or columnSpan.
GridPane gridpane = new GridPane();
gridpane.add(new Button(), 0, 0, 2, 2); // column=0 row=0, spans over 2 columns and 2 rows
gridpane.add(new Label(), 3, 1); // column=3 row=1 (spans over 1 column and 1 row (default))
row-/columnSpan and the vertical horizontal Grow can be specified in the Properties bar of the Scene Builder, residing on the right side, by default.
You can set border by using setStyle() and use some styles like -fx-border
examples
P.S. styles are the same, as like css, but with -fx- prefix

Add text view to dialog

I'm trying to align right text in my dialog.
How can I do that ?
I've tried to:
TextView loadMsg = new TextView(context);
loadMsg.setText("טוען...");
loadMsg.setGravity(Gravity.RIGHT);
dialog.setView(loadMsg);
dialog.show();
But the text does not show.
Sub-way: Create a sub layout and set content view by this
dialog.setContentView(R.layout.dialog_layout);
And design the sub layout the way you like :D
This is because you set the gravity inside the TextView. But if your TextView is laid out with WRAP_CONTENT the gravity really doesn't matter for such a short String. You should insert the TextView inside some Layout, specify FILL_PARENT and setting the layout as the Dialog content view
// width is FILL_PARENT -1, height is WRAP_CONTENT -2
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(-1, -2);
LinerLayout layout = new LinearLayout(context, params);
layout.addView(loadMsg, params);
dialog.setView(layout);
Alternatively, you can set the gravity on the layout itself and set the TextView's width to WRAP_CONTENT
Maybe you are missing LayoutParams in which you have to define heigh and width. If you use wrap_content for width then it would not be aligned as there will no be any extra space.

placing buttons at customized locations(say a circle) in blackberry

I am developing a game app for blackberry where i want to place the buttons in a semi circle fashion on the home screen.....so far I have seen all the buttons being aligned either horizontally or vertically....is there any way out where we can place buttons at custom locations such as using a layout in android and hardcoding to place them in terms of pixels? Any help is greatly appreciated
thanks
You may do this by overriding sublayout in your field manager.
In the following example setPositionChild defines the X,Y position of the first field (field 0) added to this manager. To add additional fields increment the number in this.getField() and add the fields to hfm in the order you position them.
public HorizontalFieldManager testingXYPlacement() {
HorizontalFieldManager hfm = new HorizontalFieldManager() {
// Define the x,y, positions of the fields
protected void sublayout( int width, int height ) {
super.sublayout( width, height );
Field field = null;
field = this.getField(0);
if (field != null && equals(field.getManager())) {
setPositionChild(field, XPOS, YPOS);
}
setExtent( width, height);
}
};
hfm.add(new ButtonField("hello!"));
return hfm;
}
if you are used custom button than sometimes set margin is not working properly so used button.setpadding for place your desire location .please keep USEALLWIDTH for your Horizontal or Vertical layout
If your layout is a RelativeLayout you can position elements relative to other elements within the layout (or to the entire RelativeLayout). This directly lets you place views next to other views, and you can apply margins to offset the views from others.

Resources