i am changing my buttons' backgroundDrawable programaticaly:
myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.card_button_blue));
After changing the background, the text in the button is not aligned any more like it was before changing the background.
(source: dsliga.eu)
target SDK version is 10.
Thanks.
Looks like the setText() method messes up the alignment. After changing the text, re-applying the gravity and padding did the job:
myButton.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
myButton.setPadding(12, 12, 12, 12);
Since you are using a 9patch, you need to specify with bottom and right side lines where the data is inside your drawing, you probably did not draw a full line at the bottom, and right side of the 9patch drawing
this is why the text inside your button stopped being centered, because the bounds of the 9patch are NOT what you think they are
your 9patch should (normally), leave only 1 pixel at the top and bottom of the right side line, and 1 pixel at the left and right of the bottom line
redraw the 9patch, remove the gravity and padding code, and see if it works
see below image
Related
I created a custom drawable for the background of an EditText. I need the text to be left and bottom aligned. It renders fine on screen but the text is not aligned to the right nor the bottom of the drawable. I also tried gravity:bottom and tried setting the padding on top in hopes it would move it down. Is there anything else I can do?
The right and bottom lines define the content area.
The line you draw on the right spans the top half of your image. That's why the text won't align to the bottom.
You should draw a slightly longer line.
Check out the image used by default. (I changed it to blue here though).
This will give the same result you are having. The text won't be aligned to the bottom.
Now if I extend the right line to the bottom, I would obtain the below image.
This will give you the expected result.
I want to know can I draw this type of line (Shadowed line) for my bottom border any help???
Create a 9 patch of the same image, mark the expandable area as everything above this border, and set this 9 patch as the background of the view to which you want to have this border.
You could also, create a LinearLayout with required height (2-3 dp?) and use this as the background image. Put this layout below the view for which you want this border.
I Need to shift an ImageView, which is a direct child of a LinearLayout, a bit to the left.
What happens: The View is shifted (can see the "bounding box" is shifted, with android layout manager) but the drawable keeps in the same place.
Also the drawable is cut on the right side, if I set margin low enough.
Why is this? Any advice?
Clarification: I have to shift the Image to the left. That means a bit ouside of the bounds of the containing layout.
It seems elements are always clipped when they get outside of parent's bounds (also with clipChildren=false).
A solution is to create an additional container-layout for everything besides of the item to be shifted. And then shift the container-layout in the opposite direction.
For example, to shift -10dip:
<Original container ...>
<Item to be shifted/>
<New container with margin 10dip>
<Previous content of Original container ... />
</New container with margin 10dip>
</Original container>
If your container have enough padding you can set its clipToPadding to false and use negative margin!
Why are you using negative margin? use a positive a bit more to the opposite side of the ImageView to the direction you want to shift the image, i.e. if you want to shift the image to the left, use more positive margin to the right.
I am trying to center a spinner vertically, but it does not work since the spinner seems to have default margins that are not symmetric (the bottom margin is a bit larger than the top margin). If I set any margins to the spinner element, they are added to the default margin.
What is the recommended way to center the Spinner element vertically?
I think that bigger bottom margin exist because of the drawable(9 patch PNG) used for the spinner(you can check the drawable in the SDK):
A solution to this is to make your own spinner's drawable(9 patch PNG) that has equal space on top and at the bottom.
:) I'm having the following problem: I have a view and i want to add borders to it. What I'm currently trying to do is to set padding to the view (padding from all the sides) and set background color to it which will fill the padding. The thing is that it seems to me that it's possible to set padding either only from top and left or from bottom and right but not from all of them together. I.e if i write
view.setPadding(border,border,border,border)
this will set padding only from top and left. In order to set padding from bottom and right I have to write:
view.setPadding(-border,-border,0,0)
which won't leave left and top padding and so on. If I try to use margin it moves the whole block(the view + the padding area), but not only the view, so this doesn't seem to work either. Any ideas on how to do it without having to use a wrapping layout? Thanks!
What exactly happens when you use the first example?
The four int parameters for setPadding() are for left, top, right, and bottom, respectively. So, calling setPadding(4, 5, 6, 7) should give you 4 pixels of space for the left edge, 5 for the top, 6 for the right, and 7 for the bottom. What result are you getting when you do this? Can you show a screenshot?
What is the content of your view? If it's an image or something similar, perhaps it's not being centered or scaled properly. Try calling setGravity(CENTER);.