I'm trying to center a custom view in relative layout. This image rotates so it needs to be in the center of the layout so it doesn't go out of the layout bounds. Right now its displaying in the top left corner. Here is my code:
container = (RelativeLayout) findViewById(R.id.lay_container);
bmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.charlie_sheen);
rotate_view = new RotationView(this, bmap);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
rotate_view.setLayoutParams(params);
container.addView(rotate_view);
Here is my relative xml layout
<RelativeLayout
android:id="#+id/lay_container"
android:layout_width="200dip"
android:layout_height="200dip"
android:layout_alignParentRight="true"
android:layout_below="#+id/txt_1"
android:layout_marginRight="45dp"
android:layout_marginTop="56dp"
android:padding="10dip" />
Do you have any ideas? I know that there is probably a simple solution to this problmem, but I can't seem to find the answer. Setting the LayoutParameter to center should center the view in the layour right?
Thanks in advance for any help or suggestions
Overall the code to add the custom view looks correct. This may be a weak suggestion, but to rule out any issues that may have been cause by the view customization you could try modifying the code in one of two ways.
Option 1: Use the explicit form of addRule(), i.e.
params.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
Option 2: Use the explicit form of addView() and don't set the params on the view itself, i.e.
//Omit the line above this one
container.addView(rotate_view, params);
Beyond that, perhaps some insight into the custom view, specifically how it measures itself (it's not trying to fill parent is it)?
HTH
Ok Devunwired I will do that.
Here is how I am centering the image now.
1) I am centering the image in my canvas using a BitmapDrawable setGravity(Gravity.CENTER) property.
2) I'm centering the RotationView in my Relative Layout:
<RelativeLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/txt_1"
android:layout_centerHorizontal="true" >
<com.mobicartel.acceldatatester.RotationView
android:id="#+id/rotate_view"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
I'll add the background and let you know. (Right now the image isn't displaying at all. Sometimes you have to take a step backward to take two in the right direction!)
Related
I have an Android Flow layout hash_tag_layout
<org.apmem.tools.layouts.FlowLayout
android:id="#+id/hash_tag_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/view_padding"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
</org.apmem.tools.layouts.FlowLayout>
The problem is I cannot change the margins on dynamically added views.
int pixels = convertDensityPixelsToPixels(5);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.setMargins(0,0,pixels,pixels); //<---<< This line has no effect
TextView tv1 = new TextView(ctx);
tv1.setPadding(pixels,pixels,pixels,pixels); //int left top right bottom
tv1.setBackground(ctx.getResources().getDrawable(R.drawable.hash_tag_bubble_format));//drawable
tv1.setText("# Asd");
tv1.setLayoutParams(lp);
layout.addView(tv1);
If I change hash_tag_layout layout to a regular linear layout, than I get margins!
<LinearLayout
android:id="#+id/hash_tag_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
So I am lead to conlude that Android Flow layout does not allow me to add margins to views added dynamically!
Any idea how to change the source code or find a way around this? Maybe add a transparent border around the dynamically added views?
Just change your LinearLayout.LayoutParams to FlowLayout.LayoutParams.
Here is an issue discussion on GitHub.
I see that you asked a question 3 years ago, but I have just faced the same problem :)
I currently have a RelativeLayout within my application which I dynamically move, resize, and hide. After moving it once, the layout is repositioned to a weird location afterwards.
Here is the layout that I am using.
<RelativeLayout ...>
<RelativeLayout android:id="#+id/moveable_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:visibility="gone" />
</RelativeLayout>
And here is the code that I use to relocate it
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(newWidth, newHeight);
m_movingFrame.setEnabled(true);
m_movingFrame.setLayoutParams(layoutParams);
m_movingFrame.setX(newX);
m_movingFrame.setY(newY);
m_movingFrame.requestLayout();
m_movingFrame.bringToFront();
m_movingFrame.setVisibility(View.VISIBLE);
And here is the code used to hide the layout
m_movingFrame.setEnabled(false);
m_movingFrame.setVisibility(View.GONE);
The first time that I relocate it, it moves to the correct position, but every subsequent relocation moves it to the wrong location. Can anyone tell me why it is behaving so erratically?
Thanks in advance!
First get the layout of the existing layout and then modify it.
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) m_movingFrame.getLayoutParams();
...
m_movingFrame.setLayoutParams(layoutParams);
I have to add an overlay (ImageView) so that it's a bit shifted to the left of the containing layout's left boundary.
What is the best way to do this?
Tried something simple, like putting the ImageView inside the layout and use negative margin
android:layout_marginLeft="-20dip"
This made this:
(Correction: Text in the image should be 20dip not 20px)
AbsoluteLayout is deprecated. Is there something like z-order? Or what do I do?
Thanks in advance.
Edit: I tried using relative layout instead. Same effect. Here's the xml reduced to a minimum:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clipChildren="false"
android:paddingLeft="50dip"
>
<ImageView
android:id="#+id/myId"
android:layout_width="60dip"
android:layout_height="60dip"
android:layout_marginLeft="-30dip"
android:clipChildren="false"
android:src="#drawable/pic" />
</RelativeLayout>
Result
Also happens when the containing layout has a background image smaller than the screen instead of padding.
Using RelativeLayout instead of LinearLayout (to allow overlapping) and adding this to the RelativeLayout fixed it:
android:clipToPadding="false"
set "android:clipChildren = false" in xml
Instead of
android:layout_marginLeft="-30dip"
try with
android:paddingLeft="-30dp"
Use a transparent(android:background="#00000000") imageview to the left of linear layout with width = 30dp. And make myId as aligning left in case of relative layout. If you are using linear layout make orientation as horizontal and let the transparent imageview be the first entry in it.
I have a wierd problem.
I define a LinearLayout with vertical orientation.
I define an ImageView and another custom View.
if I add the custom one and then the image, all is fine and I see both of them.
if I add the image one first, I see only the image.
I try any variation of LayoutParams and nothing worked.
what I'm doing wrong here?
Edit - I even tried some default button to check its not my custom view that causing this
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ImageView image = new ImageView(getApplicationContext());
image.setImageDrawable(getResources().getDrawable(R.drawable.image_strip));
Button button = new Button(getApplicationContext());
button.setText("test");
layout.addView(image, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(button, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
setContentView(layout);
}
I found a way, but its still has an issue.
I used RelativeLayout, and aligned the button to the bottom of the parent (the layout) and I set the image to be above the button.
now I see both of them, but the problem now is that the image width also shrinks
How big is your image? If its bigger than the view area, its going to push the button off screen. If the button is first, you'll see it. If the button is after the image, you won't. That would be my first guess. You could wrap the whole thing (the parent LinearLayout) in a ScrollView to see if its doing that. If so, you'll need to clip the image somehow.
If the image is bigger than the screen size, try the following. Create the parent LL as you have it, then create a ScrollView and add the image inside that. Set the layout_weight of the ScrollView to 1 (or whatever), and DON'T set the layout_weight of the button. This should force the ScrollView to take up as much space as possible, but leave room for the button, and allow you to scroll to see the image.
I don't have good code off hand for doing this in code. I do most layout in XML (and not to pontificate, but I'd suggest the same for you ;)
<ProgressBar
android:layout_height="50dp"
android:layout_width="50dp"
android:id="#+id/myprogress"
android:layout_gravity="center_horizontal"
android:visibility="gone"
style="#android:style/Widget.Holo.Light.ProgressBar.Small"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/title" />
<ImageView
android:layout_height="165dp"
android:id="#+id/imageView1"
android:layout_width="125dp"
android:scaleType="fitXY"
android:visibility="invisible"
android:layout_marginTop="-30dp"
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
Check for the above layout of progressbar and imageview. What I wanted was to overlap the imageview over progressbar and I achieved the same by setting android:layout_marginTop="-30dp" to imageview
I need to implement the layout as in the picture. Parent and Sibling are in a vertical LinearLayout. So I need to make a child view to overlap it's parent. Can I do that in android?
If:
sibling is a sibling of parent
parent is a ViewGroup
and you really want child to be a child of parent
then maybe you could consider using android:clipChildren set to false on parent.
I was actually just looking at an example of a FrameLayout that had a TextView overlaid on top of an ImageView. So, there are obviously multiple ways to get it done. Your next question might be which one is best ... to that I have no idea, but here's a guy that might:
http://www.curious-creature.org/2009/03/01/android-layout-tricks-3-optimize-part-1/
Just contain them all within a RelativeLayout, and remember the draw order is top to bottom, so put the top most view on the bottom of the XML definition.
If you use a RelativeLayout you should have no problem achieving this effect. By default it will stack all of its children on top of each other in the top left corner if you don't supply them with android:layout parameters. So it will definitely support overlapping children. You'd just have to figure out what the best way to tell it where the child should go on the screen relative to something else.
There are at least two layouts that can do that. AbsoluteLayout and RelativeLayout. I suggest that you put your views in a RelativeLayout and add them with LayoutParams that specify their offset form the top and left of the parent:
RelativeLayout.LayoutParams rlp;
label = new TextView(ctx);
label.setBackgroundColor(0x00000000);
label.setTextColor(0xFF7ea6cf);
label.setTextSize(13);
label.setGravity(Gravity.LEFT);
label.setText("Examples:\n- Fentanyl\n- Dilaudid 2 mg PO q 4 hours prn moderate pain");
rlp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,100);
rlp.topMargin=189;
rlp.leftMargin=30;
rlp.rightMargin=30;
rlParent.addView(label,rlp);
In my case, I have to set android:clipCildren to be false on the parent of parent.
i.e.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:id="#+id/parent1">
<FrameLayout
android:id="#+id/parent2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="64dp"
android:background="#android:color/holo_blue_bright">
<View
android:id="#+id/This_is_the_view_I_want_to_overlap_parent2"
android:layout_width="160dp"
android:layout_height="80dp"
android:layout_gravity="top|start"
android:layout_marginTop="-40dp"
android:background="#000000" />
</FrameLayout>
</FrameLayout>