I created a layout in my application that includes the followings:
main_layout (FrameLayout, fills whole screen)
-> includes: main_interface (LinearLayout, height/width fills parent)
Now I want to add another LinearLayout in the main_layout BEFORE the main_interface.
I tried this:
LinearLayout bar = new LinearLayout(this);
bar.setGravity(Gravity.TOP);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
bar.setLayoutParams(params);
main_layout.addView(bar);
but this just overlaps my main_interface.
Any Help?
If you want to add a View to specific position in a ViewGroup, you can use addView(view, position). In this case, assuming main_interface is the first View, you can call main_layout.addView(bar, 0).
However, you need to fix your layout first. If you use FrameLayout as your main_layout and set main_interface height and width to match_parent, then they will surely overlap. Try making your main_layout a LinearLayout with android:orientation="vertical" if you want vertically stacked Views.
If you could post your actual XML layout, I could help you modify and test it.
Related
Currently i'm working one a small android project. I ran on a problem with layouts because I must create a user interface in Java. I have 2 layouts first is the LinearLayout with horizontal orientation and the other is a TableLayout. I set the width MATCH PARENT and height WRAP CONTENT for both of them but the first one pushes the other one off the screen and I want the first layout to push the other one under him.
The code looks like this:
LinearLayout mainLayout = new LinearLayout(this);
LinearLayout hLayout = new LinearLayout(this);
hLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
hLayout.setLayoutParams(params);
mainLayout.addView(hLayout);
TableLayout table = new TableLayout(this);
params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
table.setLayoutParams(params);
mainLayout.addView(table);
setContentView(mainLayout);
You don't seem to add hLayout to anything so it isn't being pushed off the screen but never added to the content view. Try something like
mainLayout.addView(table);
mainLayout.addView(hLayout);
setContentView(mainLayout);
Also, since the width is match_parent and LinearLayout has a default orientation of horizontal, you will either need to change the orientation of the mainLayout to vertical or change the width to wrap_content.
And is there a reason you are doing it programmatically? Creating layouts in xml is typically much easier.
Hi by doing java code i have done successfully add textview in relative layout like BOTTOM|RIGHT
but i want to put textview above bottom bar layout
here is the image
here u can see that there is a textview which has white background image and its back it has black bottom baar i want to put textview above's bottom bar
Below is my java code
LinearLayout bottomBar = (LinearLayout)findViewById(R.id.bottomBar);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(150, 70);
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params1.addRule(RelativeLayout.ABOVE, bottomBar.getId());
butAddText.setLayoutParams(params1);
By doing this i have added bottom left but last two lines is not working i can not add textview above the bottom bar..
can any body help me please
I suspect the problem is that you've set 2 conflicting rules.
Try to remove the next line:
params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
The reason: you've tried to set the view to the bottom of its parent, and also above another view.
Simplest way may be simulate nested layouts:
<LinearLayout ...>
<TextView/>
<RelativeLayout/>
</LinearLayout>
But it may decrease performance.
Edit:
First inflate your LinearLayout(it's orientation is vertically) then add your TextView and your bottom bar.Also if you want to align TextView right,you can use a RealtiveLayout instead of outer LinearLayout.
I'm trying to get a view center vertically in android, but I have a imageView at the top that is 75dps, so I need my centered view to be offset from the top by 75dps so it gets centered! Is there a way to do this? I tried using android:layout_marginTop="75dp" but it didn't work! Any ideas?
You could wrap your view into a ViewGroup (e.g. LinearLayout), then make the LinearLayout centered, and add the marginTop to your view. It should work.
Anyway, it would be helpful if you post also your layout xml.
I'm trying to add a scroll view inside another scroll view.
It should look like is this:
scroll view
linear layout
myprogramaticscroll view
myprogramticlinear layout
myprogramticbutton
end button
end layout
end scroll
end linear
end scroll
I'm trying to add scroll views inside of that. It goes in there, but I need to know how to set parameters correctly so I can see the whole button I have inside of my scroll view. I only see part of it, and I need to set the programatic linear layout and scroll view's width height and id. How do I do this? This is what I have so far:
//the layout I'm putting my scrollview/linearlayout/button in
LinearLayout l = (LinearLayout) findViewById(R.id.linearLayoutFavorites);
ScrollView scroll = new ScrollView(this);
LinearLayout nl = new LinearLayout(this);
ImageButton yourButton = new ImageButton(this);
nl.addView(yourButton);
scroll.addView(nl);
l.addView(scroll);
You CAN'T put a scroll view inside another scroll view, that behavior would be odd, and Android would not know how to handle your scroll on the views.
For setting layout parameters, take a look at ViewGroup.LayoutParams, there're actually quite a few subclasses of ViewGroup.LayoutParams, which are for setting layout parameters for different kinds of layouts.
You can use a Scrollview in an another Scrollview. But this is not suggestible. It will be an issue to both the user and android OS. It will leads to Memory issues and also touch issues while scrolling the views. If you are expecting the Two scrolls (Horizontal and Vertical) at a time, then it is preferble to go for TwoDSCrollView
If you want to set the Layoutparams you should look at ViewGroup.LayoutParams.
If you want to set width and height then no need to set the Layoutparams. You can get the existing params by using getLayoutParams() for the view and set width and height to that params.
If you want to place vertical in vertical scroll or horizontal in horizontal scrollview the you should set the height of the internal scrollview height to the actual height of the total childs.
I've been trying to center this custom view I have inside this other custom View that extends LinearLayout. This all needs to be done via code because it's all done at runtime.
I've tried the standard approach with setting the gravity:
this.setGravity(Gravity.CENTER);
That was done inside of my class that extends LinearLayout.
I've also tried the LayoutParams method:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
block.setLayoutParams(params);
this.addView(block);
This was done in the place where I add the block to the view (as you can see).
Both methods resulted in the block still being aligned in the upper-left hand corner of my view. What are my other options or, more importantly, what in the world am I doing wrong?
Try also setting the layout weight:
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER_HORIZONTAL;
params.weight = 1;
this.addView(block, params);
This (assuming your orientation is vertical) will allow your view to fill the remaining space in the linear layout and should center it horizontally.
I don't think there is a way to have a view child that is smaller than the LinearLayout cell that contains it. If the view is smaller than its "cell" then the LinearLayout will shrink to accomodate it. If the layout weight causes the "cell" to grow, then the contained view will grow as well.
If you really want the view to be smaller than its "cell" and centered, wrap it in a FrameLayout. Then you will be able to use a center gravity to your heart's content.
layout_width="fill_parent"
In xml (I know you can't use this directly, but it's easier to explain this way):
<LinearLayout orientation="vertical">
<FrameLayout layout_weight="1" layout_height="wrap_content">
<View layout_gravity="center" layout_{width,height}="wrap_content"/>
</FrameLayout>
... other views ...
</LinearLayout>
Unmarked layout attributes are "fill_parent".
Take a look at this: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Basically, android:gravity affects positions things within a View, whereas android:layout_gravity positions a View relative to its parents. So you need to use layout gravity to move the whole View.