how to move 20dip margin left from center? - android

i want to move a seekbar 20dip margin left from center.
android:progressDrawable="#drawable/progress_vertical"
android:thumb="#drawable/seek_thumb" android:layout_height="80dip"
android:layout_width="20dip" android:layout_marginBottom="50dip"
android:layout_alignParentBottom="true" android:visibility="gone"
android:layout_centerHorizontal="true" android:layout_marginLeft="20dip" />
but the above xml code shows it center only.

I made a workaround with a TextView in the middle that has no content (and is not visible).
<TextView
android:id="#+id/viewMiddleInvisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btnAtTheTop"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp" />
<TextView
android:id="#+id/tvAtLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/viewMiddleInvisible"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#id/viewMiddleInvisible"
android:text="Some text" />
You would obviously have to replace tvAtLeft with whatever you want. Same principle if you want to have something to the right of the center.

android:layout_centerHorizontal and android:layout_marginLeft don't work together.
A workaround is to use an invisible view, such as Space:
Space is a lightweight View subclass that may be used to create gaps
between components in general purpose layouts.
So, for your case, inside your RelativeLayout, this should do:
<Space
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/blankspace" />
<ProgressBar
android:progressDrawable="#drawable/progress_vertical"
android:thumb="#drawable/seek_thumb"
android:layout_height="80dip"
android:layout_width="20dip"
android:layout_marginBottom="50dip"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/blankspace"
android:visibility="gone" />

The cleanest answer, which doesn't require to pollute your XML with additional views is simply:
Add a 40dp padding on the right.
Rationale:
Padding belongs to your View, so it counts towards its final size. If you add a 40dp padding, your View will be 40dp wider without padding.
And because you are centering, For every 2dp of padding, your View will move just 1dp to the left, so your View will move only 20dp, as needed.

Remove centerHorizontal = true.

Related

RelativeLayout background drawable overlap content

I have a 9-patch drawable(date_time). And i want to place this drawable behind Relative Layout content, so all child views should be drawn on top of this image.
Here is xml Layout:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_below="#id/tv_map_address"
android:layout_marginTop="11dp"
android:layout_marginBottom="12dp"
android:background="#drawable/date_time">
<ImageView
android:id="#+id/iv_map_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"
android:src="#drawable/ic_map_distance"/>
<TextView
android:id="#+id/tv_map_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/iv_map_distance"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:textSize="11sp"
android:text="2,7 км"
fontPath="fonts/Roboto-Medium.ttf"/>
<ImageView
android:id="#+id/iv_map_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/tv_map_distance"
android:layout_centerVertical="true"
android:src="#drawable/ic_map_path"/>
<TextView
android:id="#+id/tv_map_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/iv_map_path"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:textSize="11sp"
android:text="3,2 км"
fontPath="fonts/Roboto-Medium.ttf"/>
</RelativeLayout>
But somehow this background overlaps all child content.
Here is shown what is happening.
But if i replace
android:background="#drawable/date_time"
to
android:background="#0000FF"
Everything is fine and the output is next:
Can you explain me what i am doing wrong?
Update:
Here is my 9-patch drawable.
Exactly as I thought your content indicators are incorrect (unless you wanna introduce such a content padding with your 9-patch). Try this instead:
Right and bottom guides, determine which part (or how much) of your drawable should be occupied by your content. You've set this region to really small space, and set a fixed height of your Layout. In other words: 9-patch forced your Layout to let its children occupy only a small, region of your view (which was limited by you not letting your Layout stretch).

How to android text alignment in a button when gravity:center is not working

I have the following :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
</LinearLayout
I want to get my button's text to align left. Right now its aligned in the center. The textview's text is aligned left without any problems. Is there something else I need to add? I don't really have to close and reopen my IDE because I use maven to build. Any suggestions?
ANSWER
Figured it out : set android:paddingLeft="0dp" . Done. No need for gravity there.
Instead of wrap_content give some value. Actually its working but you can't see because your width is set as wrap_content
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
When you set an object's gravity property, you're telling that object where you want its contents to be aligned. In your code, you're trying to align the text in a box that only as big as the text itself, which does nothing.
There are 2 ways to solve this problem.
A) You can set the button's width to be not wrap the content.
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Button"
android:gravity="left|center_vertical" />
B) You can set the width of the text-view to not be wrap content.
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Text"
android:gravity="left|center_vertical" />
Otherwise, what you're doing right now should have some text in a button where there is no space between the edge of the text and the outside of the button. If there is space there, make sure you're not setting padding or margins in the button or text.

Relative Layout giving a lot of trouble

Here's the code:
<RelativeLayout
android:id="#+id/tc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/tc"
android:layout_marginLeft="185dip"
android:layout_marginTop="25dip"
>
<ImageView
android:id="#+id/tc_icon"
android:background="#drawable/count_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/tc"
android:layout_mar
/>
<TextView
android:id="#+id/tc_icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="1"
android:layout_gravity="bottom"
android:textColor="#f7e906"
android:textStyle="bold"
/>
</RelativeLayout>
I have somehow managed to make the image and the text views overlap each other (which I wanted). Now, I want them to be placed to the bottom left corner of their parent RelativeLayout (with id 'tc').... However, they just wouldn't move.
If I used alignParentBottom.... the entire relative layout stretches across the screen and aligns to its parent relativeLayout.
Please help. Thanks! :)
If you wanna overlap an image and a textview, simplw use a android:drawableleft or ony position where you want.
For example:
<TextView
android:id="#+id/tc_icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:text="1"
android:layout_gravity="bottom"
android:textColor="#f7e906"
android:textStyle="bold"
android:drawableleft="#drawable/yourimage.jpg"/>
This will give you an image on the left of your textview. Check it out

Double center text issue in TextView and RelativeLayout

I've a problem centering a text in an already centered TextView:
That's the layout:
<RelativeLayout>...
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/w0"
android:layout_margin="10dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/temperature"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignBottom="#id/image"
android:layout_alignTop="#id/image"
android:layout_toRightOf="#id/image"
android:text="15º"
android:textSize="60dip"
style="bold"
android:textColor="#color/blue"
android:gravity="center"
android:layout_marginLeft="7dip"
android:background="#color/green1"
/>
<TextView
android:id="#+id/min_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="M 21º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange1"
/>
<TextView
android:id="#+id/max_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#id/temperature"
android:layout_toRightOf="#id/temperature"
android:text="m 10º"
android:textSize="20dip"
style="bold"
android:textColor="#color/blue"
android:layout_marginLeft="5dip"
android:background="#color/orange2"
/>
And that's the displayed layout image:
What I don't understand is why the 15º isn't centered in the TextView.
The problem is due the TextView's forced center using alignTop and alignBottom but I can't see why it doesn't works.
I know I can solve this using some nesting layouts but I prefer to get a solution using only the RelativeLayout or if it isn't possible understand the reason.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
Also is there a solution to align the maximum and minimum TextViews with the top and bottom of the 15º text (not the TextView) as there is now.
I don't think so. You can only make things relative to the actual box that the View represents. And in the case of TextView the box is larger than the Text. Any relations that you do make are going to use the position of the Box no matter where the text is at or what it looks like.
Try using
android:layout_centerVertical="true"
on your temperature TextView in addition to the gravity you have now.

Android - How do I position views in an offset relative to the center/top/bottom (etc.) of their parent?

I have a RelativeLayout with views aligned relative to it (their parent), using (e.g.):
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setLayoutParams(layoutParams);
and now I wonder, how can I position the View relative to the bottom of its parent, but at an offset (e.g. 100 pixels above the bottom, 20 pixels from the left, etc.), or similarly with the center (30 pixels under the center)?
I've tried setting the margins of the View (e.g.):
layoutParams.setMargins(140, 0, 0, 0);
before applying it to the textView, but that didn't work.
It seems like an extremely useful way to align views for various screen sizes.
It's important for me to achieve this in code, without using xmls.
Thanks!
you have 2 ways, the easies is using a anchor empty view positioned at the center
<View
android:id="#+id/anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#id/anchor"
android:layout_marginBottom="20dp"
android:text="hello world!"/>
Or you can center your view and use pading por positioning (you must double the padding used because it overflows below the center of the screen)
<TextView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingBottom="40dp"
android:text="hello world!"/>
I just started Android programming tonight, so this may be old or a bad idea... but I was tinkering with the above answer and found positioning an element below my other element was causing a problem - either the padding would be in the way or something. Here's what I ended up doing.
Using the anchor technique, I also gave the anchor a height (double what you need), and then aligned my element with the top. That way it'd be in the middle of the view minus half the height of the anchor!
<View
android:id="#+id/anchor"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/heading"
android:text="#string/heading"
android:textColor="#android:color/white"
android:textSize="24sp"
android:layout_alignTop="#id/anchor"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/subheading"
android:text="#string/subheading"
android:textColor="#android:color/white"
android:textSize="12sp"
android:layout_below="#+id/heading"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The other answer helped a lot but I just wanted to give my 2 cents!

Categories

Resources