Buttons in LinearLayout not filling entire space programmatically - android-layout

So I have a LinearLayout in horizontal mode which I add buttons to at random times in code and I like them all to fill up the space equally.
This is my current code so far:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL;
and I add it to my view here:
linearLayout.addView(button, layoutParams);
All it does is add the buttons as if it was wrap_content and not expanding their width to fill up the available space as in the buttons look left justified.
I also have tried linearLayout.setGravity(Gravity.FILL_HORIZONTAL); and
linearLayout.setWeightSum(++weightSum);
linearLayout.addView(button, layoutParams);
where layoutParams in this case is:
layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 1);
I also tried setting the layout width to zero per answers to other questions.
Am I missing another technique?
Edit: I figured it out, I forgot to set the width of the LinearLayout to match_parent instead of wrap_content.

Button's background has "margins" so they will always have gaps even if there's no space between buttons. You have to change background to your own image which doesn't have these margins.
EDIT: You should use weights for buttons.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
0, LayoutParams.WRAP_CONTENT);
params.weight = 1;
for( int i = 0; i < 5; ++i) {
Button button = new Button(this);
button.setText("Button" + (i + 1));
linearLayout.addView(button, params);
}

Related

Android: Custom bottom sheet dialog

How can I implement following design functionality with android standard component bottom sheet:
Image when Bottom sheet dialog fragment will appear:
Image when user scrolled to up to view bottom of content:
I will use ViewPager to scrolling header images and RecyclerView to showing descriptions and other informations. And parallax effect to ImageView(which are placed in ViewPager) when scrolling content vertically. Have a minimum height of the ImageView(and ViewPager), user can't collapse fully it (Look to second screenshot, which is user scrolled until the end of content).
I want stop scrolling ImageView when it will reach to minimum height(look to second one Screenshot), but the content of below ImageView should be continue scrolling
This can be done with an if statement in an on scroll view such as shown below:
ScrollView scrollView = findViewById(R.id.scrollView); //Adjust for your code
ImageView imageView = findViewById(R.id.imageView); //Adjust for your code
boolean imageIsHidden = false;
int threshold = 250;
scrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY();
if(scrollY >= threshold){
imageIsHidden = true;
//Move image outside of scroll view so it doesn't scroll
}
else if(scrollY < threshold && imageIsHidden){
imageIsHidden = false;
//Move image inside of scroll view so it does scroll
}
}
});
What this does is has a boolean called imageIsHidden and an integer called threshold. Threshold is where you want it to make it disappear. You will need to play around with this value to find a sweet spot.
You will also need to implement moving the image inside and outside of the scroll view as well in the if and if else statement.

Adding nested stackviews programmatically using xamarin.ios c#

I am creating a app for both android and ios using xamarin and mvvmcross.
In the ios app I want to add outer vertical stackview having nested horizontal stackviews. Basically I just want to create a basic person details screen where will be Label on left and textfield on right which will go in one horizontal stackview and like this there will many horizontal stackviews nested in outer vertical stackview.
I am looking for such example on internet but seems most of the examples are in swift but I was hardly able to find some in c#.
Can someone please help.
Thanks,
Santosh
UIStackView leverages the power of Auto Layout and Size Classes to manage a stack of subviews, either horizontally or vertically, which dynamically responds to the orientation and screen size of the iOS device. You can learn about it through this documentation.
In your case, we can construct a vertical stack to place several horizontal stack:
UIStackView verticalStack = new UIStackView();
View.AddSubview(verticalStack);
verticalStack.Axis = UILayoutConstraintAxis.Vertical;
verticalStack.TranslatesAutoresizingMaskIntoConstraints = false;
// Use auto layout to embed this super vertical stack in the View. Also there's no need to set the height constraint, vertical stack will automatically adjust that depending on its content
verticalStack.LeadingAnchor.ConstraintEqualTo(View.LeadingAnchor).Active = true;
verticalStack.TopAnchor.ConstraintEqualTo(TopLayoutGuide.GetBottomAnchor()).Active = true;
verticalStack.TrailingAnchor.ConstraintEqualTo(View.TrailingAnchor).Active = true;
for (int i=0; i<10; i++)
{
// Here try to put some horizontal stack with Label on left and textfield on right in the father stack.
UIStackView horizontalStack = new UIStackView();
horizontalStack.Distribution = UIStackViewDistribution.EqualSpacing;
horizontalStack.Axis = UILayoutConstraintAxis.Horizontal;
// UIStackView should use AddArrangedSubview() to add subviews.
verticalStack.AddArrangedSubview(horizontalStack);
UILabel textLabel = new UILabel();
textLabel.Text = "text";
UITextField textField = new UITextField();
textField.Placeholder = "enter text";
horizontalStack.AddArrangedSubview(textLabel);
horizontalStack.AddArrangedSubview(textField);
}
But if every horizontal stack's subViews are almost the same style and layouts. Why not try to use UITableView? You just need to set the single cell's contents and layouts, then use it in the tableView. Moreover this control is reused and scrollable.

Setting LayoutParams but its not working android

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.

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.

List view with custom view items with partially overlaps (Android)

I want to implement a list in Android that contains some customized views.
My problem is that I want the the views will be put one after the other with a little overlap between them. like in the following schema:
I also want to control this overlap in such a way that when the user clicks on one of the items, they will move apart from each other.
I tried to extend ListView but it seems to be very obscured, any suggestions?
Edit:
This can be more clear:
I did it by setting the divider height to -50dp.
this is exactly what I want to achieve, but somehow it doesn't reflect on my app.
I managed to achieve this by using scroll view with a single relative layout as a child.
I then dynamically place the views by defining a rule and margin:
for (int i = 0; i < 30; i++) {
TextView tv = new TextView(context);
tv.setText("Text \n Text" + i);
tv.setBackgroundColor(i % 2 == 0 ? Color.RED : Color.GREEN);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp.leftMargin = 0;
lp.topMargin = (i * 45);
rl.addView(tv, lp);
}
Later, you can control the positioning of the sub-views by changing their y value (for example: if you want to add animation).
This is the final result:
This can probably be achieved by using the Camera.setTranslate function. See Android: Vertical ListView with overlaped rows and Android: vertical 3d listview for similar questions (with solutions)

Resources