Dynamically put textview above another view in relativelayout android - java

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.

Related

How to display multiple TextView without changing their positions?

I have some TextView(s) with fixed positions. Top left, Top right, Bottom left, Bottom right of the screen. The program worked if one TextView is viewed individually.
I tried RelativeLayout and LinearLayout to collect them all, and used setContentView. But none of these actually worked, and I don't want them to re-arrange the text position unnecessarily.
How to make the code work? For example, this code just makes all the text placed on top of each other.
RelativeLayout views = new RelativeLayout(this);
views.addView(tv_1);
views.addView(tv_2);
views.addView(tv_3);
views.addView(tv_4);
setContentView(views);
Note : I used Gravity to align my text (that is why they have fixed positions and I want to display all of them without changing where they originally are).
I need to initialize layout for each TextView. Not sure what it is for, but it works.
tv_1.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tv_2.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tv_3.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
tv_4.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

How to Add Layout on the Top of another Layout

How to bring layout or view on the top of another view or layout
enter image description hereas shown in the picture
What you want exactly?
See you can do it in two way:
Take a android.support.v7.widget.Toolbar inside it take a Relative Layout then take a Textview and RelativeLayout with an ImageView inside it. Align the RelativeLayout with ImageView on the right side and the TextView to the left of it.
Take a android.support.v7.widget.Toolbar and below it take a RelativeLayout with ImageView and align it to Right and Top

Android: Prepend LinearLayout programmatically

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.

Setting Layout Parameters Programmatically

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.

Android Gravity Issues

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.

Categories