How to remove TextView top margin? - android

I do have a problem with TextView. I don't want to have any margin/padding above it.
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ptp_hour"
android:textColor="#color/black"
android:textSize="100dp"
android:height="100dp"
android:layout_marginTop="0dp"
android:paddingTop="0dp"
android:includeFontPadding="false"
android:maxLines="1"
android:text="10"/>
My TextView looks like this and despite the textSize and height are set to the same value, there is a space above font. It bothers me because I want to put another view relatively to the top of the font. Is this spacing included into font itself?
And another question: If I found out that margin 20dp from top and 7dp from bottom works perfectly on my device, can I rely that it will behave in a similar way on other screens?
(these margins are for buttons)

using android:includeFontPadding="false" helped me a lot in a similar situation.

I had the same issue where setting android:includeFontPadding=false did not help. The best solution I could find in reasonable time was to override the TextView's onDraw method and to adjust the canvas for the difference between the font metrics' top and ascent values:
FontMetricsInt fontMetricsInt;
#Override
protected void onDraw(Canvas canvas) {
if (adjustTopForAscent){
if (fontMetricsInt == null){
fontMetricsInt = new FontMetricsInt();
getPaint().getFontMetricsInt(fontMetricsInt);
}
canvas.translate(0, fontMetricsInt.top - fontMetricsInt.ascent);
}
super.onDraw(canvas);
}

What you need to do is to put the other view relative to the top of the font, and give it a negative android:layout_marginBottom in dip, such that it matches the top of the font. If the font has a margin, I don't think there is a better way of doing it.

Yes this space included by default. You are not able to remove that space as per my search area. So you need to have to implement some logic to have such view.
See below Image:
Its not a good idea but you can do like below code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffff">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ptp_hour"
android:textColor="#android:color/black"
android:textSize="100sp"
android:lineSpacingMultiplier="0.8"
android:scrollY="-100dp"
android:scrollX="100dp"
android:text="10"/>
<TextView
android:text="10"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#00FFFFFF"
android:shadowColor="#000000"
android:shadowDy="-50"
android:shadowRadius="1"
android:textSize="100dp"/>
</LinearLayout>
Here, first "10" is of your properties and second one is as i have set for you.
Enjoy. :))

Related

TextView: Remove spacing and padding on top and bottom

I have one textview which size is 50 dp. I got this result like the picture below.
I want to remove bottom spaces in my textView ,because its height depend on the text size. And I want to receive background only into my quotation mark.
Is there any way to remove the unused spaces in my textView?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="top"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp" />
Try this:
<TextView
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="top|center"
android:text="“"
android:fontFamily="#font/arialbd"
android:background="#ff0"
android:textColor="#E8E9EF"
android:includeFontPadding="false"
android:textSize="50dp"
android:gravity="top" />
This solution works on my case. To remove bottom padding. Set the layout_height the same size as your textSize or 1dp or 2dp less.
Set includeFontPadding="false". Set android:lineSpacingExtra="0dp"
In your case you have to implement custom textView. check this example for reference.
U can try setting your bottom margin to a negative value.
Something like this:
android:layout_marginBottom="-10dp"
Let me know if its works, that is the simplest way but can be anothers...

Customizing a textview

I want to create a label like this:
Requirements are:
The rectangle should grow with the text
the length of the red line is dependent of a property
What I tried was to create a 9ng image for the background of the text and use the padding property for the length of the red line. The red line is drawn by overriding the onDraw method.
The result is that the red line is correct but the background is expanded including the padding like this:
What's the best way to do this? Building a custom view with a label included? a custom view drawn 100% programmatically (in this case, what's the method to resize a 9ng image?)?
Thanks
Julien
try this one
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="The text"
android:textColor="#000000"
android:background="#drawable/<your_drawable>"
android:textSize="16sp" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#f00"
android:layout_weight="1" />
</LinearLayout>
Hope it will help you
Best way to do this is probably using two different controllers.
You set a simple textView with your rectangle as background and with the width set to wrap-content.
After this you create the line positioning her where you want (to the right of the controller or to the end of the page or where you please) and you resize it as you want.
For any doubt I'm here

How to add padding between each item of a row in Android?

I am having a table with each row containing 3 buttons.
Now, I want to add some padding between each buttons in a single row.. How can I do that..?
When i add this statement in my code,
tableRow.setPadding(20,20,20,20);
I am able to observe padding but between each row.
I want to have padding between each button...
Note: I want to do it from java programmatically, not from xml..
You can do it in this way:
<Button android:id="#+id/myBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="Click Me"
/>
padding is the margin inside the button between the edges and the text.
This will increase button size not the distance between buttons
You need to use margin like this in your XML inside your button tag
android:layout_margin="10dp"
this will set all margins to 10dp
if you need to set margins only for certain side use :
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"
I hope this helps you out.
Regards
you need to add padding to your buttons.
btn1.setPadding(left, top, right, bottom);
btn2.setPadding(left, top, right, bottom);
.
.
.
also make sure to take units in dp, hard-coded values will result in uneven padding on devices with various resolutions.
The TableRow class, as with all View subclasses, indeed has a setPadding method.
However, since you mention that you found setMargin, I believe you are looking at the TableRow.LayoutParams instead of the TableRow itself.
Margins are set in a View's LayoutParams, whereas padding is set on the View.
You need to use margin for your buttons instead of padding for the whole row. Because padding is actually a space between the button text and border, e.g. inner spacing, while margin is the space between button border and the it's container (row) e.g. outer space
Depending on what look you are trying to achieve, use something like this
<Button android:id="#+id/yourButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button1"
/>
<Button android:id="#+id/yourButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button2"
/>
<Button android:id="#+id/yourButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Button3"
/>

How to center a text vertically in a narrow textview in Android?

I have a TextView which has a height of 30px and a textsize of 40px. Since the text is taller than the View, I only want to display the middle part of the text.Like this:
But with android:gravity="center_vertical", I only can display the upper part of the text with some bottom part cut off.
And this is my code:
<TextView
android:layout_width="wrap_content"
android:layout_height="30px"
android:textSize="40px"
android:text="ABCDEFG"
android:gravity="center_vertical"
/>
Anyone knows of any way to do that? Thanks!
Can't you simply wrap it in a LinearView set to the appropriate size and adjust the margin within the textview to about half (depending on padding) the font size?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#android:color/white">
<TextView
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:text="Sliced in half"
android:textSize="60dp"
android:layout_marginTop="-30dp"/>
</LinearLayout>
Resulting in:
http://i.stack.imgur.com/jEdxx.png
I highly doubt whether you can do that. All the options that I have come across while dealing with TextView are for when the Text is smaller than the View. This is the first question that I have come across which has the requirement otherwise.
One option which I had used before was android:scrollHorizontally="true" where in you specify whether the text is allowed to be wider than the View.
Interesting question. It would be great if you could add some code though.

Android TextView offset downwards

I have an activity with two Buttons and a TextView in a LinearLayout. My TextView is offset downwards and the text doesn't fit inside the box. Can you explain what is happening? I think it is related to padding, and I've read several discussions about the perils of TextView padding, but that doesn't explain why the text is cut off at the bottom.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#800080">
<Button
android:text="This"
android:background="#drawable/button_red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="That"
android:background="#drawable/button_green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
/>
</LinearLayout>
This code produces this display:
The purple is the LinearLayout, the blue is the TextView. As you can see, the TextView's top is below those of the buttons and its bottom is below the bottom of the LinearLayout. As I add text to the TextView, the LinearLayout increases its height appropriately, but because the TextView is offset, I always lose the bottom of the last line.
I ran Hierarchy Viewer and it gave me this wireframe:
Which shows the vertical offset at the top, but misses the bottom of the TextView. The same wireframe with the LinearLayout selected looks like this:
According to Hierarchy Viewer, the top of the buttons is at 0, but the top of the TextView is at 7. I've tried various fixes, mostly culled from this site:
android:paddingTop="0dp"
android:background="#null"
android:includeFontPadding="false"
None of these fixed my issue.
Set android:baselineAligned property of your LinearLayout to false.
From documentation:
When set to false, prevents the layout from aligning its children's
baselines. This attribute is particularly useful when the children use
different values for gravity. The default value is true.
give the layout_gravity of the Textview to be center_vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#800080">
<Button
android:text="This"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="That"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
android:layout_gravity="center_vertical"/>
</LinearLayout>
Try setting the layout_gravity for the TextView like this:
<TextView
android:text="Copious amounts of text that overflows onto several lines on a small screen, causing the TextView to dangle below the buttons. Why it does this I can't imagine. I only hope someone can help me with this."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#533f93"
android:layout_gravity="top"
/>
If you have this problem with several TextViews you can add android:gravity="center_vertical" to LinearLayout

Categories

Resources