I have a button. It seems to have bottom padding I cannot get rid of:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foo"/>
In the resource editor, I can click the button, and you see the padding below the bottom edge of the button there. This seems to block me from properly centering the button vertically in a parent RelativeLayout.
I tried setting padding=0dip and layout_margin=0dip, no effect. That bottom padding persists.
Thanks
The padding is in the 9-patch of the buttons themselves.
I'd advise against trying to compensate because these graphics resources can change without notice. You'd be better off building your own 9-patch or editing the existing one to remove the padding.
setting android:layout_marginBottom="-5dp" for the button worked nicely for me.
I am late to answer this but thought to share if someone come across similar use case( Removing all the inner and outer padding in Button )
<Button
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="-5dp"
android:layout_marginLeft="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="-5dp"
android:minHeight="-2dp"
android:minWidth="-2dp"
android:text="TextView"
android:textSize="12sp" />
Hmn really strange. Never noticed that. Probably because u usally want a little space around your buttons.
Also tried margin/padding=0dp and did in fact not work.
You could set android:layout_marginBottom="-10dp" however :).
Related
So, this question is a duplicate, but the answers I have read doesn't work for me. If fact, they don't seem to have worked for other people either, so I'll give it another shot and see if someone knows the answer.
When Adding a textView, it looks like
As you can see there is definitely a lot of space between the blue layout-marking and the actual text. When placing one textView on top of another the space becomes quite noticeable. Note that this is after I have added the XML code lines:
android:includeFontPadding="false"
(Which works to a small extent) and:
android:lineSpacingExtra="0dp"
(Which is said to solve the problem according to other threads, but does nothing for me for some reason).
The full XML for my textView is:
<TextView
android:id="#+id/healthTextId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:includeFontPadding="false"
android:lineSpacingExtra="0dp"
android:text="Test"
android:textAlignment="center"
android:textColor="#color/colorHealth"
android:textSize="100dp"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
The problem persists on both default and custom fonts. Any ideas?
This is one of the threads I read that unfortunately didn't solve the issue for me: Android: TextView: Remove spacing and padding on top and bottom
This problem is very annoyying. Even if you remove all paddings, the padding of the font itself remains. Try this answer. This is a good approach.
Just android:padding="0dp" should work.
I want the text centered in the Button. But whenever I add android:drawableLeft xml attribute to the Button, it offsets the text. How can I fix that?
The only way I managed to fix it is by setting a negative number in android:drawablePadding:
But I'm looking for a cleaner solution to achieve it.
unfortunately that is the Button behaviour and I agree with you that negative padding is kind of a dirty hack, so I'll propose two other hacks that to me sounds a little less dirty because they rely on expected behaviour (negative padding although works it's not by design):
set android:paddingRight on the button to the size of the drawables
create a drawable with the same size of the left drawable but with all transparent pixels and put as right drawable.
So I ended up going with the lesser of the evils, and that's placing the drawable and the text inside RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/Button.ButtonGray"
android:id="#+id/share_button">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btn_share"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_centerInParent="true"
style="#style/TextNormalWeight"
android:text="#string/post_share"/>
</RelativeLayout>
The RelativeLayout then receives click events and acts and looks like a button.
android:gravity="center_vertical"
Unfortunately there's no clean way to do this. One fairly straightforward way is to set a transparent ColorDrawable of equal size on the opposite side of the button.
val drawableSize = button.height // adjust this as desired of course
realDrawable.setBounds(0, 0, drawableSize, drawableSize)
val emptyDrawable = ColorDrawable(Color.TRANSPARENT).apply {
setBounds(0, 0, drawableSize, drawableSize)
}
button.setCompoundDrawables(realDrawable, null, emptyDrawable, null)
It works with:
android:gravity="center"
I'm creating this in a XML file and want a text view to be placed in a specific spot.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="200dp"
android:text="Text"
android:textColor="#color/blue"
/>
It's underneath the last button that was placed so it's around in the middle of the screen. I want it to be at the bottom of the screen of to the right.
Easy if you're using a RelativeLayout as parent.
android:layout_alignParentRight
android:layout_alignParentBottom
(check the capitalization, I'm not anywhere I can double check).
Using a RelativeLayout would be perfect if you would be specifying each views' positions. It is the most flexible layout among the Android Layouts.
Take a look at this tutorial. It discussed how to position views in a RelativeLayout.
I hope you can get ideas from it in solving your problem.
I am trying to port my WP7 app to android.
Does anyone know how I can layout the text on a single button so that some text appears aligned left and other text appears aligned right? (See below). I need access to be able to dynamically change the percentage number on the right side using code but the text on the right is just static.
Anyone know the answer to this?
The image is here:
http://i.imgur.com/zW7YV.png
Yes you could make it two buttons.
Remove all padding and margin from between them.
Set the same background drawable.
And just ensure when the left is clicked it invokes the right's onPress method (so it looks as if they depress together).
Or wrap the buttons/imageviews/textviews in a layout and perform the onClick on that.
I would use a RelativeLayout for this.
<RelativeLayout
android:width="fill_parent"
android:height="wrap_content"
android:clickable="true"
android:background="#18a2e7"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align_parentRight="true"
android:text="0%" />
</RelativeLayout>
I'm trying to create standard button in android with a background and some text in front but some fairly specific alignment. I want the text to be centered vertically and on the left with 20dp of padding. The alignment works but the padding doesn't. I know I could probably get the desired effect by putting a few spaces in the text but that seems like a hack and next I want to do a similar thing but with the text at the top so I would prefer a more elegant solution.
Here's what I have:
<Button
android:layout_width="312dp"
android:layout_height="95dp"
android:id="#+id/gv_music_button"
android:text="Music"
android:textSize="30sp"
android:paddingLeft="20dp"
android:gravity="left|center_vertical"
/>
My mistake, padding was working correctly. Just didn't appear to be.