background drawable does not fit in bound of TextView in Android - android

I made a TextView which has attributes of background, gravity and so on.
and i made a drawable resource file for the TextView's background.
and this is the xml.
and i added the drawable resource into background of the TextView.
but the problem is, the background doesn't fit in Textview's bound.
please look at this photo.
there is a gap very slightly.
the background color area is upper than TextView's area.
so i tried to use includeFontPadding attribute but it didn't work.
how can i fix it?

I would advise you to check this on real device/emulator, not on preview in AS. use Show Layout Bounds option from developer options for verification, make some full-resolution screen shot and check on it. it will be properly placed
content of any View, including its background, is always places inside its bounds. really, always, its just impossible to get outside with a single pixel (due to layout building and drawing mechanism)

Nothing is wrong with the code. In the Design Editor, it shows like this when we use any background drawable with any view. There is not any kind of a glitch.

Related

Gradient background scaled after resizing ViewGroup

In my app, I have ConstraitLayout with a gradient background. In a runtime, this ConstraitLayout can be resized. The problem I faced is that gradient drawable "isn't scaled". I will show an image to explain it better:
In the green circles, You can see that the color before and after resizing at the and of ConstraitLayout is different. Is this possible to force an app to set a gradient in the full range of colors?
The only way I have found to do it is to set ConstraitLayout background every time its changes. I think it is not the best way to do it, especially in my case when ConstraitLayout size was changing in on onTouch event, so my App has to change it so many times. But I didn't see any problems while using this solution.

Android How to change text color according to background image

On Android, I have a header with a background image (Random image according to API).
On this header I have texts with some data. My text is every time black but sometimes image is black too. So, we can't see the text.
I'm looking for library or snippet for resolve this problem.
Thanks.
You can use the palatte library for this. Please see the following:
https://developer.android.com/reference/android/support/v7/graphics/Palette.html
https://developer.android.com/training/material/palette-colors.html
I encoutered this problem months ago and was not really sure how to approach it. First of all you need to use a Layout where you can put View over View for example Relative or Frame. After that you need to make the ImageView thats behind the TextView to be a little bit Lighter or Darker (like a shade) at the place of your TextView so you can choose a color for your text which will always be readable since the shade will be in contrast with the text. What you can do is put something behind the TextView and the ImageView which will be Light or Dark and make the ImageView a little bit transparent using set.alpha(int) if i remember correctly. So at this point you will have transparent image with a light or dark rectangle behind it. It will be visible that the part where you have the rectangle is darker/lighter. Then you put your TextView there with contrast color to the Rectangle and you will always be able to see it. It is kind of complicated, but it will work. Hope it helps.
I think palatte is not available for android. So glide will be a better option. Link

Where are the options to control a Button's background?

I would have thought that there would be a variety of options in connection with a button's background from an image, for example the image could be tiled, or stretched or centred etc etc, but when I list a button's methods I can't see anything. Now I'm suspecting that it could be a two stage process, perhaps getting some kind of view first and then using a method of that view. Or maybe there is simply no control whatsoever concerning a button background. Please advise.
Note that any View's background is something that fills the area covered by that View, so you can't have it centered.
Stretching the the default behaviour, that's why a state list of 9-patches the the best thing to use for Button's background.
If you want tiled background, you may use XML Bitmap with tileMode="repeat". See also other kinds of Drawables on this site. You can for example make something that feels like centered background image using Inset Drawable.
And finally the functions are there: setBackgroundDrawable and setBackgroundResource.

Set semi-transparent drawable on top of TextView at runtime

In an application to manage the costs of a living community, an user can 'terminate' a month, which is then stored in a database.
In a specific Activity of this app, there are shown the last four months including their attribute 'terminated'. I want to show this to the user by putting a semi-transparent hook on top of the TextView containing this month. Consider this image as example:
It seems that this doesn't work properly in Android.
textView.setCompoundDrawablesWithIntrinsicBounds(...)
does put the image on top, and it's semi-transparent too, but the initial text of the TextView disappears. Additionally, since now I haven't discovered yet how to scale the image into that TextView.
This is the actual output:
Can anybody help me with that? Should I define the scaling parameters directly in the XML layout file?
You have to use a FrameLayout that wrap your TextView and an ImageView. The ImageView will contains the semitransparent drawable. In this way the will overlap

Convert an image to Color object which can be added to background of a Layout

I am having the following requirement.
1> I have a background image(png) (containing various shades) of color with stripes.
Is there any class which is already available on Android using which I can convert the image to Color object & set on the layout as background color?
Will the ColorMatrix class help ? I want to avoid using the image as background , as with different screen resolutions , even after having separate drawable folders , there can be cases when the background image can get stretched , giving a wrong view .
Kindly provide your inputs/sample code. If my approach is incorrect , kindly provide the correct logic.
Thanks in advance.
Warm Regards,
CB
This is a confusing question...what kind of background image is this? If it's just a gradient, you can create one using ShaderFactory programmatically. And if it's something that doesn't have a lot of fine detail, you can just use a single image, and just have it scale to fit the screen. Rather than using the android:background attribute, a trick you can use is to wrap your layout in a FrameLayout, and add an ImageView behind the rest of your layout, with the background set as your ImageView's source. Set the scaleType to something like centerCrop, and it will scale proportionally.
Can you give an example image?

Categories

Resources