How to set dot in vertically center of textview example (. Set .) , I want both dots in vertically center of set text. i have been tried with gravity center but didnt get any success, You can refer attached image where dot show after just now with very light color, i want same , so please suggest how to achieve this
Use a bullet character, \u00b7
There is no way you can do this in the way you are trying to do it.
Remember, fullstop may look as just a dot, but in reality, there is empty space above the dot. From bitmap perspective, it is as tall as any other character.
You may, however, try below 2 approaches:
Use different character instead of fullstop. e.g. "\u00B7". It looks like ·set·
Change the font size of just . (not possible in simple textview, AFAIK. Possible in rich text edit boxes.)
Set margin from top in negative(like -10dp).
I tried both to align a few TextView objects and they gave me the same results. What exactly is the difference between 'baseline' and 'bottom' ?
To visualize the difference, I usually imagine two textboxes in Word or Photoshop.
alignBottom lines up the bottom of the textboxes. (The blue outline)
Text could be uneven, but the boxes they're in would line up on the bottom.
alignBaseline aligns the actual text within the box. This can help ensure that the texts line up on the bottom, regardless of font size or textbox size. (The green line)
What is a Baseline?
Baseline is a typography term that refers to the invisible line text is written on.
(As referenced in What is the baseline in RelativeLayout?)
Warning
If you're not careful, using alignBaseline could make your layout look like this:
Details: Watch That Baseline Alignment
I don't know if you're still looking for the answer, but I decided to at least put this out there since this was one of the first results.
Basically the other option I am considering is a RelativeLayout with a background. Then inside the layout, have the image to the left and the text to the right. I think I read somewhere that I can have a TextView with an accompanying image. So I am wondering if what I want is possible with one textview?
The background would be a rounded-rectangle of a certain color
The image would be on the left
The text would be on the right.
Yup, you can do this all in a TextView:
<TextView
android:drawableLeft="#drawable/my_image"
android:background="#drawable/my_rounded_corners"
android:text="#string/my_text" />
There's also a drawablePadding attribute to adjust spacing between the text and the drawable. There's not a lot of fine tuning of the position of the drawable, so if you need something more precise you'll have to go with individual views, but this works well for a lot of cases.
I have an app where I am trying to add text shadow to a TextView. The problem is: the shadow is always very thin. I'd like it to be thicker.
I am trying to generate "memes", as some of you might know from the "fun sites" on the internet. My goal is something like this font:
http://d24w6bsrhbeh9d.cloudfront.net/photo/4324188_460s.jpg
I am using the same exact font, Impact. The problem is, when I add a black border shadow, the shadow is not visible enough and it's not wide enough. It's barely barely noticeable.
I am defining a FrameLayout, with the picture on the bottom and two text fields, one on the top and one on the bottom. The shadow is barely visible for both of them.
I have been using the parameters, shadowDy, shadowDx, etc. I know the shadowRadius is the parameter that actually defines the border size, but I have been experimenting both with values above 1 and below 1, and I can't seem to get any good results. There are minimal changes in size and shadow density, but nothing useful.
I have considered another option, which is a last resort, which is drawing the text twice, a bigger black font in the BG and align the character spacing so that the black text becomes the shadow of the white text on the front.
Thank you in advance !
You won't be able to accomplish that effect with the basic TextView shadows. I'd look at adding a stroke instead:
https://stackoverflow.com/a/2151964/321697
http://developer.android.com/reference/android/graphics/Paint.Style.html
Demo of negative margin:
The scenario
Overlapping views by setting a negative margin to one of them so that it invades the bounding box of another view.
Thoughts
It seems to work the way you'd expect with overlapping of the layouts if they should. But I don't want to run into a bigger problem for unknowingly not doing things right. Emulators, physical devices, you name it, when you use negative margins everything seems to work correctly, one view invades another's views bounding box and depending on how it's declared in the layout it will be above or below the other view.
I'm also aware that since API 21 we can set the translationZ and elevation attributes to make view appear above or below other views but my concern basically comes from the fact that in the documentation for the layout_margin attributes it's clearly specified that margin values should be positive, let me quote:
Excerpt:
Specifies extra space on the left, top, right and bottom sides of this view. This space is outside this view's bounds. Margin values should be positive.
Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters)...
In the years since originally asking this question I haven't had any issues with negative margins, did try to avoid using them as much as possible, but did not encounter any issues, so even though the documentation states that, I'm not too worried about it.
In 2010, #RomainGuy (core Android engineer) stated that negative margins had unspecified behavior.
In 2011, #RomainGuy stated that you can use negative margins on LinearLayout and RelativeLayout.
In 2016, #RomainGuy stated that they have never been officially supported and won't be supported by ConstraintLayout.
In December 2020(v2.1.0, official release June 2021), negative margin support for constraints has been added to ConstraintLayout.
It is easy to work around this limitation though.
Add a helper view (height 0dp, width constrained to parent) at the bottom of your base view, at the bottom add the margin you want.
Then position your view below this one, effectively allowing it to have a "negative" margin but without having to use any unsupported negative value.
Hope this will help someone. Here is working sample code using ConstraintLayout based on #CommonsWare's answer:
Add an helper view (height 0dp, width constrained to parent) at the
bottom of your base view, at the bottom add the margin you want. Then
position your view below this one, effectively allowing it to have a
"negative" margin but without having to use any unsupported negative
value.
Sample code:
<TextView
android:id="#+id/below"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F1B36D"
android:padding="30dp"
android:text="I'm below"
android:textColor="#ffffff"
android:textSize="48sp"
android:textAlignment="center"
tools:layout_editor_absoluteX="129dp"
tools:layout_editor_absoluteY="0dp" />
<android.support.v4.widget.Space
android:id="#+id/space"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="#+id/below"
app:layout_constraintLeft_toLeftOf="#id/below"
app:layout_constraintRight_toRightOf="#id/below" />
<TextView
android:id="#+id/top"
android:layout_width="100dp"
android:layout_height="60dp"
android:textAlignment="center"
android:textColor="#ffffff"
android:text="I'M ON TOP!"
android:background="#676563"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/space" />
Output:
Instead of negative margins you can use:
translationX and translationY.
Example:
android:layout_marginBottom = -2dp
android:translationY = -2dp
UPDATE:
Have in mind that the whole view is translated.
In case you want use negative margin,set enough padding for container and its clipToPadding to false and set negative margin for it's children so it won't clip the child view!
It might have been bad practice in the past but with Material Design and its floating action buttons, it seems to be inevitable and required in many cases now. Basically, when you have two separate layouts that you can't put into a single RelativeLayout because they need distinctly separate handling (think header and contents, for instance), the only way to overlap the FAB is to make it stick out of one those layouts using negative margins. And this creates additional problems with clickable areas.
For me, and regarding setting a negative margin on a TextView (I realize the OP is referring to a ViewGroup, but I was looking for issues with setting negative margins and I landed here)... I found a problem with 4.0.3 (API 15) ONLY and the setting of android:layout_marginTop or android:layout_marginBottom to a negative value such as -2dp.
For some reason the TextView does not display at all. It appears to be "gone" from the view (not just invisible).
When I tried this with the other 3 versions of layout_margin, I didn't see the issue.
Note that I haven't tried this on a real device, this is using a 4.0.3 emulator. This is the 2nd odd thing I've found that only affected 4.0.3, so my new rule is to always test with a 4.0.3 emulator :)
I have success with reducing the bottom margin of a TextView by using android:lineSpacingExtra="-2dp" which works even though I happen to have android:singleLine="true" (and so I wouldn't have thought that line spacing would be a factor).
No, you should not use negative margin. instead you should use translate. Even if negative margin work sometime, when you change layout programmably, translate would help. And view can't overflow the screen wen you use margin.
I've only known that it was possible for a rather short period of time. But I see no problem with it. Just be aware of screen sizes and such so you are sure not to accidentally make to items that shouldn't appear overlapped on the screen. (i.e. text on top of text is likely a bad idea.)