Android Creating Custom Buttons - android

I have quite a few buttons in my application, they vary in sizes based on the text inside. I want to customize the buttons but I am not sure if I should use
A) Ninepatch pngs
B) Android xml created
C) Photoshop pngs
I want the buttons to scale nicely and therefore not look distorted based on screen size. What are the positives and negatives of the above methods? Is there an ideal way to go about this?

It depends on what they should look like. Rounded corners? Complicated images? Drop shadows? Plain colors?
If you want simple buttons that are monotone in color, xml drawables are the best solution. You can do rounded corners through xml, so if plain is what you are looking for this is the best option.
If you are working with an image, then a 9 patch is the way to go. Play around with the draw9patch tool that comes with the sdk to get the hang of it. You also don't necessarily only need to leave the corners in tack, sometimes you may want to break up the resizable area depending on the image.
PNGs are the last resort as they are the least flexible of the 3 options.
By the way, you should also consider using a state list for all of your buttons, regardless of how you implement the drawable that the button uses. See the link below:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Related

How to make image and text closer together in a button?

In my project, I have a 3x3 TableLayout consisting of nine different Buttons. Each Button has an image drawable above it and a small string of text below it. To be clear, the drawable and text are the Buttons using the "drawableTop" and "android:text" variables defined in XML.
I'd like to know that if it would be possible to move the drawable and the text closer together because the UI looks a bit off on big screened phones.
App on Nexus 4
App on Nexus 6
I realize that I will have to make different XML layout files for small phones, phones, small tablets and big tablets. But Nexus 6 is, as I classify it, a phone so I will probably not be making a separate layout file for it.
Any feedback helps unless you tell me that I need to use a LinearLayout with an ImageButton and TextView in it because I've already tried that and it didn't turn out that well.
When it comes to this, I usually use two views, one for the text and one for the image and put them into a ViewGroup, which then gets assigned onClickListener. This gives me more freedom and customization with the downside of lowering performance a bit.
But to answer your question, you can use android:drawablePaddingin conjunction with android:paddingTop to control the space between the image and the text.
Well the simple answer is probably mess around worth the text view's top padding. If that's not working for you the other solution is using the relative layout which is intended for such cases. That way you can keep the visual effect you're looking for while keeping the layout structure flat which is good for performance.

Android: 9-patch image vs Shape vs Multi icon

Some people from our company create some rule about UI design for Web, Android and iOS. They give us a bunch of icon for each platform. But when i look at Android platform icon, they using multi icon to create 1 icon. For example, button background, they use these 3 icon, 1 for left side, 1 for right size and 1 stretchable for middle.
I think this is how web normally design button background. But i don't think i should do the same for Android (Maybe iOS also don't do it this way but since i only work with iOS for a short time, i not really sure). Normally i would use a Shape or 9-patch drawable for this kind of background. So here i have 2 question:
Which 1 should i use for button background in this example. A shape? A 9-patch drawable? Or multiple icon like this? And which one is worst to use?
If i must have to use multiple icon like this, how to put multiple icon like them into single background?
If their way of doing thing here is incorrect, i need some good reason to talk to them and make them change their mind. So any1 please help me clear about this problem.
Thank you!
9-patch should be the first choice and then is shape. Don't use 3 icon.
9-patch is more easy to use and easy to change if the further design changes. When using shape, you need to define the corresponding XML. If in next version, the background color changes, you need to modify the XML, but with 9-patch you just need to replace the resource file.
The other benefit is that, 9-patch can be created in UI side, programmer's don't have to re-manipulate it if the resource is correctly created. But with shape, programmer needs to know the RGB color, and need to code the XML.
3 icon is not suitable here. First, you have other better options. Second, 3 icon means you need to create 3 views for each of them. Third, the button should have different effect when user click it, with other two options you can simply define selectors, but with 3 icon, there's no elegant solution.

Nine-patch versus Vector graphics

I am learning about Android UI and am unclear why people use nine-patch, when you could use vector graphics, because those are scalable without any pixel degradation. I am a beginner in Android, so I hope I am not missing anything here, but it seems like it would be easier to build vector graphics and use those. You would not need the special editor to build them.
Can anyone explain the advantages to using nine-patch over vector? (Don't just explain advantages of nine-patch, as that is already done on StackO., but rather the advantages that vector does not have). Because it seems like Android recommends nine-patch. Thanks.
In vector graphics all side are scale or stretch when we set it to any background whereas in 9-patch we can define which sides can scale or strech so at runtime only those side scale which we set it to scale in 9-patch tool.
from this
-> The advantage of using 9-patch images is that when using it as a background, for instance, the image won't stretch and loose proportions in different screen sizes. the center 'patch' will remain as is and the 'borders' patches will be stretched to fit the screen/view size.
let say you have this image.
and a button with fill parent width. if you set this image to button background it will scale completely and your image gets blur (mean t will expand to button width)and it will not look good. so what 9 - path tool do that you define that online scale some part of image let say if width is fill parent. dont scale whole image . let say we set that after t (in image). scale whole area, so t will not get blur. so this will make good your button.hope you got my point..:)
after making your image 9 patch and setting to button background. your button look like this.
instead of t(in image). whole area expand and fill the buttons width.
Imagine a button with rounded corners. How do you scale it? If you scale it only horizontally, you will have elliptic corners, which would be ugly.
This is what 9-patch is for.
i hate 9patches. i am quite attentive when doing my artwork. i don't even use photoshop. i go with illustrator.
i do everything right when exporting my artwork, i used to do my 9 patches with insane zoom on, maybe check afther that in photoshop for misplaced pixels...
** sad trumpet ** when put on a view, if i used some subtle round corners like 6px or 10px and a stroke everything looks awful at mdpi.
So I solved my problems by marrying the two. I wrote my own 9patch which uses vectors. :)
Everything looks like it's been touched by baby Jesus. Perfect corners, strokes and, best of all, you can use one asset for all the screen sizes, densities while, of course, no more transparent borders, wicked errors because 9patch won't stretch inward, so on and so forth.
I use vectors for icons too. While there sometimes are issues with various effects, these are minimal and easily avoided if you do some reading on how to avoid them.
Best of luck to all you guys!
This is a very basic example.
You can do whatever strikes your fancy. Because of the performance impact of svg's on an app, when first run [or when the user changes appearance options, i like to save the newly generated bitmaps as pngs, if possible.
You don't get any more "best of both worlds" than this.
You can define content are on a 9-patch image which means (for example) text will always be placed in that specific area, I think you cannot do this in Vector image. Android has built-in support for 9-path but for vectors you have to use a library.
9-patch rendering is easy to implement and efficient. If you have an image that can be scaled by stretching horizontal or vertical lines (e.g. buttons or rectangular icons), then use a 9-patch. If you have some icons that don't scale well, then create multiple versions at different resolutions and use Android's resource management to handle it. Both of these approaches are much easier and more efficient than vector graphics.
If you have large images or scenes to render, and you don't want to take up a ton of space with bitmaps, thats when you start thinking about vector graphics.

What's the proper way to scale/size background images for Android?

I want to have an image cover the background of my Android game, but I'm new to Android development - are there conventions for what file type such an image should be?
What is a conventional size?
What's the proper way to scale/refit this image to different devices (right now I just added the imageresource to the back layout, but this scales the image and distorts it)?
I'm planning on using photoshop and drawing out the image there, is there a more recommended method?
Thanks
"Are there conventions for what file type such an image should be?" The image can be any common file format. PNG is common, probably because it's loss-less and supports transparency, but I don't know the details.
"What is a conventional size?" You will have to produce an image of various resolutions to support all the screens on which your game may be used. You can create new directories in your project tree that look like res/drawable-{screen-type}, where {screen-type} is a screen size/resolution/minimum dimension. For details on how this works see Supporting Multiple Screens.
"What's the proper way to scale/refit this image to different devices?" See #2. To avoid distorting your image, you will need to produce different sized/resolution images to fit various screen sizes.
With regards to Photoshop -- if you are producing a background with a complex design/graphic, you will probably want to use the graphics editor you know best, such as Photoshop. However, if you are making a background that will just be a simple pattern that repeats, or even just a solid, consider making just a small bit of the image, and then using an Android Shape Drawable or Nine Patch Drawable to tile/repeat/stretch your image. This will reduce the number of different-sized images you need to produce for different screens, and will also reduce the space your app takes up, since you will have fewer resources to package. For info on the Drawable resources that Android supports, see Drawable Resources.
The background drawable is stretched by default. If you want to change that, you can create an XML <bitmap> drawable resource referencing your image and set its gravity attribute to something different than fill.
The result can be better if your image is decomposed in different smaller images aligned properly. For exemple, a main background color, a top part and a bottom part. You can then combine the different layers with a Layer list drawable or using a custom layout.

Should I draw or load an image?

I am wondering what would be the best practice for android. To draw or to load an image?
For example I want to have a circle that is green filled with the text 25 in it.
-Should I have it in .PNG image file and just load it to the imageview placeholder when I need it?
OR should I load it in Java (by determining the position in java and load it from my res folder)?
OR Should I draw it (draw a circle, fill color, add text field inside)?
On another note, to have a checkmark, what do advise me to do?I don't even know how to draw that
I really need your help in this
Thank you so much
I'd actually argue that there is a single right answer. You should use a shape with a set color and text view inside it. It is easy to do what you've described in xml, see: Oval Gradient in Android for an example (there are a number if you google for android shapes). This will be less CPU/memory intensive than loading a bitmap, and more importantly, it will work for and look good on every screen size. As an added bonus of the user has changed their default font sizes, a textView/shape can respect that and a static image can not. Just set the background of your TextView to be the shape drawable (either in xml or programatically).
To get a bitmap to look good on multiple devices (a must for android development) , you will need a number of different versions of the same bitmap at different resolutions (XHDPI, HDPI, MDPI... etc).
Check marks are harder because they don't conform to a standard shape. Those you probably will have to make pngs for.
It depends on what you want to do.
Rendering a bitmap will be faster, but you cannot manipulate it after it is drawn.
So, for your case, you have a green circle with 25 written it. Do you also need a green ball with 23 and another with 24 in it? What about other colors?
It really depends on the amount of variation you need. At a certain point the overhead of using dozens of images will not be worth the evert of drawing to a canvas. Or, depending on how complex the images are, it will be.
The question is not "Which is better?", but instead, "Which is better for what I need to do?"
I'm not an expert on Android development but after seeing your question I have done a bit of reading and came across this article on displaying images with android that is relevant and includes some code to possibly help you out. http://www.javacodegeeks.com/2011/07/android-game-development-displaying.html
As for your image of a check mark, why dont you just go into photoshop or paint and use the line tool to draw a checkmark and then fill it in with color and save it as a .png perhaps?

Categories

Resources