I have an issue with ScrollView which seems to add a top margin for its child. I have a LinearLayout above it, which sets Top margin in order for the layout to be below other elements (its parent is FrameLayout), then ScrollView is inserted into the LinearLayout, and a finally a child is attached to ScrollView.
The following image shows empty space that appears between "border" and order details (TextView):
Image
Java Code:
// Inner layout to accommodate ScrollView
LinearLayout inner_layout = new LinearLayout(app.getApplicationContext());
LinearLayout.LayoutParams inner_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 440);
inner_params.setMargins(0, 135, 0, 0);
inner_layout.setLayoutParams(inner_params);
// ScrollView for details of the order (in case everything doesn't fit in the body)
ScrollView scrollView = new ScrollView(app.getApplicationContext());
params_in = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(params_in);
scrollView.setFillViewport(false);
TextView body_text = new TextView(app.getApplicationContext());
params_in = new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params_in.setMargins(10, 0, 10, 10);
body_text.setLayoutParams(params_in);
body_text.setTextColor(getResources().getColor(R.color.colorTextSecondary, null));
It's almost as if top margin of 135 is also used for ScrollView's child.
If someone could help understand what I'm doing wrong I would appreciate it a lot.
Related
I have linear layout but bottom part not display if scrolling data is more then screen size.
Streaching Bottom Part when Using Scrollview inside the linearlayout, when scrolling data more then height of the device height.
If i have less data then vertical screen size then all button of bottom appears correctly.
public class TestGUI extends LinearLayout {
sv = new ScrollView(context);
hsv = new HorizontalScrollView(context);
this.addView(topLinearHorizonal);
hsv.addView(tableLayout);
sv.addView(hsv);
this.addView(sv);
this.addView(fullbottomLinearHorizonal);
}
Edited:
LinearLayout fullbottomLinearHorizonal= new LinearLayout(context);
fullbottomLinearHorizonal.setOrientation(VERTICAL);
fullbottomLinearHorizonal.addView(clearLinearHorizonal);
fullbottomLinearHorizonal.addView(bottomLinearHorizonal);
If you want to show fullbottomLinearHorizonal always at the bottom(visible) you can try below while adding views to TestGUI:
LinearLayout.LayoutParams svParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 1);
this.addView(sv, svParams);
LinearLayout.LayoutParams fullbottomParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, WRAP_CONTENT);
this.addView(fullbottomLinearHorizonal, fullbottomParams);
Edit: Also default orientation for LinearLayout is horizontal. See link.
You should better set it vertical at TestGUI:
this.setOrientation(VERTICAL);
If you want fullbottomLinearHorizonal at the end of the scroll content then you must add it inside of the scrollView.
I am getting stucked on the following problem:
For a questions app I want to implement a nice output for a survey showing the possible answers and the percentage of people answered the specific question. Therefore, I want to add a "bar" to my app that displays the percentage. I try to solve this by using views and weighting them in a linear layout.
I want to add the different answers programmatically, this is the code I've got so far.
My problems are, that I am not getting closer to weighting the views and resizing them.
/* Add all questions */
RelativeLayout my_root = (RelativeLayout) findViewById(R.id.ownerRL);
/* Add a new Linearlayout as a container for the question */
LinearLayout A = new LinearLayout(this);
A.setOrientation(LinearLayout.HORIZONTAL);
my_root.addView(A);
/* Create a new View in this container, for the status bar */
View new_view = new View(getBaseContext());
new_view.setBackgroundColor(Color.YELLOW);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(50, 20, 3);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) A.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.question1);
A.addView(new_view);
View new_view2 = new View(getBaseContext());
new_view.setBackgroundColor(Color.GREEN);
ViewGroup.LayoutParams lp2 = new ViewGroup.LayoutParams(50, 20);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) A.getLayoutParams();
params2.addRule(RelativeLayout.BELOW, R.id.question1);
params2.addRule(RelativeLayout.LEFT_OF, new_view.getId());
A.addView(new_view2);
The Green view should actually be right of the yellow one (which is not visible).
How do get it managed such that it creates a red/white bar that has a height of 4px and the red and weight parts are weighted?
Thanks for your help!
check this link, Set Linear Layout weight it like this
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);
The last parameter is weight.Hope this help.
Currently I have a view within another view (its an ad) but because I am using MATCH_PARENT the width is the entire screen length when I only want it to be the ad size. Now WRAP_CONTENT fixes the problem but how do I centre the view on the bottom of the screen now? I was using the following code...
LinearLayout ll = new LinearLayout(this);
ll.setHorizontalGravity(Gravity.CENTER); //Place centre
ll.setVerticalGravity(Gravity.BOTTOM); //place bottom
mobfoxView = new MobFoxView(this, "xxxxxxxxxxxxxxxx", true, true);
mobfoxView.setBannerListener(this);
mobfoxView.setVisibility(View.GONE);
ll.addView(mobfoxView);
this.addContentView(ll, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
problem is that it wont centre anymore because of the WRAP_CONTENT. How do I get this to work so the layout is centred at the bottom of the screen?
Try these edits:
ll.setGravity(Gravity.CENTER | Gravity.BOTTOM);
mobfoxView = new MobFoxView(this, "xxxxxxxxxxxxxxxx", true, true);
mobfoxView.setBannerListener(this);
mobfoxView.setVisibility(View.GONE);
ll.addView(mobfoxView, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
this.addContentView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
In this way if ll is appended at the end of the tree it will fill everything and position on the bottom.
I created a custom view. In it, theres a line, a textview, another line. beneath the bottom line, i wanted to put a new horizontally oriented linearlayout. when i run it, this nested linearlayout doesnt seem to show up at all. Instead, i can see the test button right underneath the bottom line. what am i doing wrong?
public class MyView extends LinearLayout {
public MyView(Context context, Question question) {
super(context);
// this.setLayoutParams(params);
this.setOrientation(VERTICAL);
this.setBackgroundColor(Color.WHITE);
LinearLayout.LayoutParams lineParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 2);
View topLine = new View(context);
lineParams.setMargins(0, 15, 0, 0);
topLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
topLine.setLayoutParams(lineParams);
this.addView(topLine);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
//Challenge Question
TextView questionText = new TextView(context);
questionText.setTextColor(Color.BLACK);
questionText.setTextSize(14);
questionText.setLayoutParams(params);
questionText.setGravity(Gravity.CENTER_HORIZONTAL);
questionText.setText(question.getQuestion());
this.addView(questionText);
View bottomLine = new View(context);
bottomLine.setBackgroundColor(Color.argb(255, 0, 159, 218));
bottomLine.setLayoutParams(lineParams);
this.addView(bottomLine);
LinearLayout innerLayout = new LinearLayout(context);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT);
innerLayout.setLayoutParams(innerLayoutParams);
innerLayout.setBackgroundColor(Color.RED);
innerLayout.setOrientation(HORIZONTAL);
//TableLayout for the multiple choices
TableLayout tableLayout = new TableLayout(context);
LayoutParams tableLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// tableLayoutParams.weight = .8f;
tableLayout.setBackgroundColor(Color.RED);
tableLayout.setLayoutParams(tableLayoutParams);
innerLayout.addView(tableLayout);
this.addView(innerLayout);
Button button = new Button(context);
button.setLayoutParams(params);
button.setText("testing 123");
this.addView(button);
}
Note that I pasted the code without all the stuff that I added to the tablelayout. I probably should have pasted that too. But it didn't work when I did that either. but either way, if i set the nested linearlayout to 300 width and set a background color of red to it, i should at least see it, no?
Think about what the height of the inner layout should be. Right now it is wrap_content and contains a TableLayout (with no rows) with its height also set to wrap_content. There doesn't seem to be anything in that inner layout giving it a height dimension, so that may be why it is not being displayed.
Trying the following will make your layout visible:
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(300, 300);
More usefully, you can try adding something with a real width/height to the TableLayout.
Also consider writing your layout in XML to better separate your application logic and the presentation.
Hi i´m trying to set the margins on a ScrollView with this code but nothing happens.
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new ScrollView.LayoutParams(
ScrollView.LayoutParams.FILL_PARENT,
ScrollView.LayoutParams.WRAP_CONTENT);
// layoutParams.bottomMargin = 100;
layoutParams.setMargins(0, 0, 0, 100);
sv.setLayoutParams(layoutParams);
sv.addView(ll);
// Add a TableView to ScrollView...
// Display my view
this.setContentView(sv);
What I'm trying to do is have some buttons bellow the ScrollView so you always see them.
I'm quite new to Android development so any suggestions are appreciated.
/Olle
I think layoutParams.setMargins(0, 0, 0, 100); does works.but you dont add buttons at the bottom in this way. check out the following links
http://www.anddev.org/code-snippets-for-android-f33/relativelayout-scrollview-buttons-at-bottom-t8904.html
Two equal sized buttons at bottom of scrollview