When using a 9-patch image as a background, the padding seems to be always derived from the 9-patch image. Even if you do not use a padding bar in the 9 patch image it uses the drawable.
If the padding lines are not included, Android uses the left and top lines to define this drawable area.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
However you can override it in the XML by using android:padding=0dp or =Xdp.
Unfortunately using android:paddingLeft=Xdp does not work. So you are stuck with uniform padding.
I tried doing this:
android:padding="2dp"
android:paddingLeft="20dp"
It had no effect on the padding on the left. Placing them in a styles.xml produced similar results.
The only hack I have seen to get around this was to set the padding in code.
if (android:padding presented) it overrides all other padding values
else if (android:paddingXXX presented) it overrides bg drawable paddingXXX (XXX = Left|Right|Top|Bottom)
else if (view has drawable bg) padding values from this drawable (nine-patch in your case) will be used
else default padding will be applied (zero usually)
So do no use android:padding="2dp". padding property overrides everything. Just use paddingLeft = 20dp, paddingTop = 2dp, paddingRight = 2dp, paddingBottom = 2dp.
Or you can set paddingLeft = 20dp and other padding values will be taken from bg drawable.
Related
When you assign a drawable to a view using the xml with a stroke width, it works fine.
When I try to assign it dynamically during runtime and change the stroke width, it doesn't seem to work.
You can find the result and the code as snapshots here
Basically,
Left view is initialized with a background drawable with a stroke width of 40px through the xmls.
Center view is just a basic view with a background color RED and a height of 40px
Right view is a basic view that is assigned a background drawable programmatically and the stroke width is changed to 40px on the press of the button.
You can clearly see in the result that the left view's border width is the same as the center view's height while the right view's border width is about half the size of the others... what am I doing wrong? Or is it a bug in android's sdk?
Thanks!
Question is too old but i hope i wll help to someone. I think there is only one solution. You should set size of GradientDrawable again using this method:
gradientDrawable.setStroke(100, Color.RED);
gradientDrawable.setSize(gradientDrawable.getIntrinsicWidth(),gradientDrawable.getIntrinsicHeight());
I am trying to create a drawable XML file for a LinearLayout divider (see here if you are unfamiliar with LinearLayout dividers).
What I am after is a 1 dp line, with a 12dp top and bottom padding. This way, my items will be nicely spaced out and will have a line separator. The <shape> drawable does have padding, but this doesn't affect the height of the drawable itself, hence using that does nothing unless it is inflated into a view. Is there any way of doing this with Android's XML drawables?
Thanks in advance
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 got a widget and when I use a color as a background, all the components inside have a 0 padding when I use android:layout_alignParentTop="true" (same for other sides).
But when I use a 9 patch image as the background of the parent, it seems to have a padding that is equal to the non stretchable size of the 9 patch image when I align them on their parent.
Is this normal ?
There are two main parts to a nine-patch: the stretchable areas and the content-defined areas. The top and left pixel border define the stretchable area, as I'm sure you're aware. The bottom and right, however, define the CONTENT area. If you want the padding to go away, you need to make the bottom and right bar extend all the way to the edge of the artwork (not all the way to the corner pixels, though!). Basically, the right and bottom pixel border define your padding.
I was just wondering if there was a way to change the opacity of the background image for a View (ie. TextView, etc.).
I know that I can set the background image like this:
android:background="#drawable/my_drawable_image"
Or I can set a specific background colour with an alpha setting like this:
android:background="#10f7f7f7"
Is there a way I can control the opacity (set the alpha) if I'm setting the background as a drawable image? And I want to do this in the XML Layout. I already know that I could grab the Drawable object and programmatically set the alpha, but I want to see if I can do it in the layout.
I ended up just going with the programmatical solution, since it doesn't look like it can be done via the XML layouts.
Drawable rightArrow = getResources().getDrawable(R.drawable.green_arrow_right_small);
// setting the opacity (alpha)
rightArrow.setAlpha(10);
// setting the images on the ImageViews
rightImage.setImageDrawable(rightArrow);
This might make your Work simpler
View backgroundimage = findViewById(R.id.background);
Drawable background = backgroundimage.getBackground();
background.setAlpha(80);
Alpha Values 0-255, 0 means fully transparent, and 255 means fully opaque
from: This Answer
You can also use XML to change the transparency:
android:alpha = "0.7"
The value of alpha ranges from 0 to 1
You can embed the image in xml, so you'll be able to see it in the Graphical Layout
<LinearLayout
style="#style/LoginFormContainer"
android:id="#+id/login_layout"
android:orientation="vertical"
android:background="#drawable/signuphead">
And change the code like this to make it transparent:
Drawable loginActivityBackground = findViewById(R.id.login_layout).getBackground();
loginActivityBackground.setAlpha(127);
The answer you gave didn't exactly answer the question you asked. Here's what I did.
Drawable login_activity_top_background = getResources().getDrawable(R.drawable.login_activity_top_background);
login_activity_top_background.setAlpha(127);
LinearLayout login_activity_top = (LinearLayout) findViewById(R.id.login_activity_top);
login_activity_top.setBackgroundDrawable(login_activity_top_background);