Android Button Icon with Background Color - android

I want to add an image(the user image) in the centre of the button and also maintain the background colour(grey) of the button.
I tried to use the background attribute along with the padding but it seems to have no effect.
Can anyone please guide me how to do it ?
Should I be using the attributes drawableTop etc in my xml ???
Please help.

Do not set the image as the background. Instead, use an ImageButton and specify your image as the source (src attribute).
That way you can specify things such as scale type, padding, etc. Keep in mind that using padding sometimes doesn't work properly on Android version 2.2.

Related

background drawable does not fit in bound of TextView in 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.

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

png image showing up with grey background in android

I have some png image buttons that I use in my app. But they are showing up with grey backgrounds on my page. Does android always show a grey background for a transparent one? Is there a work around?
This is how it showed before.
This is how it shows after I made #FF000000 edits
This is how I want it to show.
EDIT:
Error when I use #FF: Color value not valid -- must be #rgb, #argb, #rrggbb, or #aarrggbb (at 'background' with value '#FF'). main.xml
#android:color/transparent. Phoenixblade9 was close.
If the image is transparent, there should be nothing shown behind the image.
The default colors vary from distribution to distribution. The color scheme/theme for an HTC phone will be different than from a Motorola phone, or the emulator.
If you want to ensure that a view has a transparent background, set the following in your XML code for that view:
android:background="#android:color/transparent"
This should ensure the background is completely transparent.
The code line in the ImageView or ImageButton in the XML file is actually:
android:background="#android:color/transparent"
I am writing this to help someone who is not familiar with the platform and won't connect the two answers above.
I found this happens if you have android:src="#drawable/mypngimage" in your xml file. Instead if you use background then the gray area goes away and it behaves as expected. android:background="#drawable/mypngimage"
Please check whether your image is 32 bit depth or not. After that insert this code in your image view: android:background="#android:color/transparent".
To make your image transparent open Adobe Photoshop or Illustrator, open file (i.e. your image) and click file and click to export - save as web (legacy), tick transparent and save it.
Also you can use #50FFFFFF (aarrggbb). in this format, aa is alpha channel to be transparency frequency. So If you use above color, you get a 50% transparent white background.

Android Transparent png doesn't show as transparent

I'm using an image with transparency as a ImageButton in my Android app - but when it appears on the android screen - it still has a whitish surrounding border.
I even changed the image to one of the template ones supplied and that still is not transparent (although it is in photoshop).
Am a missing some declaration or something ?
I tried in both the emulator and also running on my phone and its still shows as white on the black background.
Cheers
ScottyB
The whitish color IS the button part of the image button :)
Use regular Button and set your image as the android:background of it. Don't set any text.

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