Position text inside button vertically - reduce margin? - android

I am having trouble formatting the text inside my button. I have tried reducing the margins and padding and using gravity, but I still seem to have a some kind of margin around the text inside the button.
I have currently positioned the text in top, but as you see it stays very low and even goes out of the button.
Here is the code I use:
<Button
android:id="#+id/counterValue"
android:layout_width="283dp"
android:layout_height="144dp"
android:layout_marginStart="24dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:onClick="countIN"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:minHeight="0dp"
android:minWidth="0dp"
android:gravity="top|center"
android:text="0"
android:textSize="140dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
As you see, the big 0 is at the very bottom of the box. I would like it to be right in the middle and with very small margins.
Any ideas of what am I missing in my code?

I suggest you change the text size from "dp" to "sp" as in 140dp. If you want to keep this size tho, you might want to change the size for your button
The line in problem is:
android:textSize="140dp"

You only have to change the Button's height to wrap_content:
android:layout_height="wrap_content"
You can't have fixed height for the Button and fixed textSize and expect the text to be centered correctly.
Edit
Set this attribute:
android:includeFontPadding = "false"

Related

How to set text leading or shift a text position on a TextView

How do you set a text leading on a TextView? Or how do you shift a text position relative to a view that it is in?
I want the height of the textview to be less than what wrap_content would set at default without cutting the text from the bottom.
I've tried using the layout_height tag on its xml layout but it would clip the text from the bottom just like the image I've provided on the link below, and I also have tried using the lineSpacingExtra tag but it seems to only work for the distance/leading between two lines.
Here is my xml file:
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:text="clipped text"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:lineSpacingExtra="-18dp"
android:text="a text longer than usual to demonstrate line spacing"/>
Image for clarity: https://i.stack.imgur.com/QGV4B.png
Why you can use wrap_content? Or can you share what are you trying to achieve?
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="clipped text"/>
Did you try to do it using padding or paddingTop ? Try it with different values to get your desired result.
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:paddingTop="0dp"
android:text="clipped text"/>

How to prevent text from exiting the screen

My question is simple:If i have a text view and the text is too long,it just goes past the screen limit,how can i make it to split on multiple lines in order for it to fit in the screen
<TextView
android:id="#+id/movie_cast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="#id/movie_image"
app:layout_constraintTop_toBottomOf="#id/movie_rating_bar"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:textColor="#color/black"
android:textSize="12sp"
tools:text="Cast: Ryan Reynolds,Dwayne Johnson,Gal Gadot"/>
And also,why does some text fit on my emulator screen and when i run the app on my phone it doesn't fit,why isn't it resizing acordingly.Look at how the text just gets out of the screen without going on the next line
Looks like you are using ConstraintLayout , instead of android:layout_width="wrap_content" use android:layout_width="0dp" and fix start and end of layout according to constraints.
add this attribute to textview
app:layout_constrainedWidth="true"
like this
<TextView
android:id="#+id/movie_cast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true" // this line
app:layout_constraintStart_toEndOf="#id/movie_image"
app:layout_constraintTop_toBottomOf="#id/movie_rating_bar"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:textColor="#color/black"
android:textSize="12sp"
tools:text="Cast: Ryan Reynolds,Dwayne Johnson,Gal Gadot"/>
You're constraining the start but not the end of the view. Constrain the end as well, so it knows when to end the textview. Without a constraint on the end, and with a wrap_content length, it will make the text view as large as wide as necessary to fit all the text.

Is there a way to omit layout_height in order to have two views have identical heights?

I wish to accomplish having the height of a view conform the the height of the another view, in other words have them exactly the same. I've tried using constraintview, but this requires omitting layout_height in order for it to work.
The following gives me a crash and requires layout_height:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="#drawable/img"
android:id="#+id/bg"
android:adjustViewBounds="true"
android:layout_marginTop="8dp" app:layout_constraintTop_toBottomOf="#+id/dateText" app:layout_constraintStart_toStartOf="parent"/>
<TextView
android:gravity="center_vertical"
android:text="Test text"
android:layout_width="wrap_content"
android:id="#+id/textView"
android:textColor="#color/bluetext"
app:layout_constraintBottom_toBottomOf="#+id/bg"
app:layout_constraintStart_toStartOf="#+id/leftGuideline"
app:layout_constraintTop_toTopOf="#+id/bg"/>
Is there a simple way to make the heights exactly the same without using a fixed height for both views?
Add android:layout_height="0dp" (a.k.a., "match constraint"), to say that you want the height to be determined by the top and bottom constraints that you have set up.
Note that you may need to adjust the android:gravity attribute of your TextView to your liking. The TextView itself will take up the desired height, but the text may be smaller (or larger, if it has lots of lines).

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...

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"
/>

Categories

Resources