At last i finished my work and now i need to hand it to designer. App has some buttons and checkboxes.
So the question how big button and checkbox should she draw for each screen size(ldpi,mdpi,hdpi,xhdpi) or she could draw just one image for button and anotherone for checbox and it could be scaled?
Thank you already.
It really depends on how big your buttons are. U can check the following link for sizes:
http://developer.android.com/images/screens_support/screens-densities.png
But I agree with Mikelsreal (create a 9 patch image).
Create the button image in whatever size u like.
Add black pixels above the image to set where the image should be stretch horizontal.
Add black pixels on the left side of the image to set where the image should be stretch vertical.
Add black pixels on the right side where the content can be placed vertical
Add black pixels below the image to set where the content can be placen horizontal
rename the image like : yourImageName.9.png
and it should work.
for more info check (developer.android.com/tools/help/draw9patch.html)
Related
I want to achieve the following:
a picture, as a background, which resize according to the screen size
I want to put button on top of this picture, while maintaining a proportional position to the picture width and height.
See the example:
The background is an image with the fill_parent settings for width/height.The yellow squares are buttons on top of this picture.
So, how can I keep the button on the exact same position on top of the picture with different screen size, using relative layout (or others layout)
The solution was to use the "Percent Support Library" as stated by #subhash
In Android I need to have a simple layout to be designed using XML layout (rather than programming):
I need to have the following :
1- small image view on the left (centered vertically)
2- Three image views a bit larger beside each others (also centered vertically)
3- Small image view on the right side (centered vertically)
so the 5 images are adjacent horizontally and centered vertically.
This is simple, anyhow the question is how to specify the image views to resize as percentage of the screen height ?
So if I specify the three image items to be 60% of screen height, I expect the imageView to resize (including the source image) to be 60% of screen height.
Is it achievable?
Thanks
Yes, it is, Take a LinearLayout(Horizontal) and then to all the childs(Imageviews here) give layout:height="0dp" and layout:weight=".6" , try this first and update questions if you have any further
I would like to know why do we use nine-patch ? I know is to shrink or stretch images but if I want to resize an image can't we just do it on a dedicated image editor like gimp for example ?
What is 9-Patch?
9-Patch images are stretchable, repeatable images reduced to their smallest size; users draw a right and bottom, solid black 1 pixel border to tell the system how to place the content within the image.
The 9-Patch is a PNG image with an added extension to the file name (filename.9.png), which allows the Android system to determine how the image can be stretched and contorted to meet the specific layout constraints.
The Android operating system reads the borders of these images to understand how to properly stretch the image itself and the content within the image such as text and effects.
9-Patch Theory
9-Patch gets its name from the fact that the overlay created breaks the image up into nine defined regions. Each region has specific stretch properties:
Corner Regions (1, 3, 7, 9)
These regions are fixed and nothing inside them will stretch.
Horizontal Sides (4, 6)
The pixels in these region will stretch vertically when necessary.
Vertical Sides (2, 8)
The pixels in these region will stretch horizontally when necessary.
Center (5)
The pixels in this region will stretch in both horizontal and vertical directions equally.
here is Google docs
Nine patch image is very useful because it reduces your resource and one can maintain the curve shape which get stretch in normal .png.
Reduces resource : One can make a small NinePatch image and can stretch it as more as he can by repeating Pixel
Maintained border corner even if display size changes.
No need to give padding programmatically, you can reserve text area in NinePatch directly.
The top and left pixel border define the stretchable area. 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.
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
http://www.developer.com/ws/android/programming/Working-with-NinePatch-Stretchable-Graphics-in-Android-3889086.htm
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.
one more and biggest advantage is memory. Same small size memory can be reused for different screen size devices.Well-designed 9-patch images are less error-prone and have high reusability. I had hard time optimizing the UI for different resolutions until I knew that Android supports 9-patch.
For padding as #hotveryspicy said you can use the padding box ( where your text button will be filled) to define your paddig values and they are defined like this:
padding-top: distance between the top edge of the padding box and the
top edge of your button
padding-bottom: distance between the buttom edge of the padding box and
the buttom edge of your button
padding-right: distance between the right edge of the padding box and
the right edge of your button
padding-left: distance between the left edge of the padding box and the
left edge of your button
Hope this will help you to have a clear idea and how important 9-patch drawable are
Nine-patch is used for dynamic stretching and shrinking of an image at runtime. That's the reason why it cannot be compared to statically resizing an image using an image editor.
Nine-patch is used for things like borders that dynamically size according to the content, so they have to stretch dynamically.
9-patch images aren't just scaled up; they're "stretched" in a defined way. The classic case is a button with rounded corners. If the button was just scaled, the radius of the corners would be enlarged too. With 9-patch images, the corners can be defined to stay the same size while the lengths of the edges are increased.
have you worked with css. if not then there is one property called repeat which gives you ability to repeat 1px image in to the width of 1040 and even more with out starching
9 path do the same, some time due to the different resolution of the images rather creating separate image for each phone create 9 patch image
Hope that help
Nine-patch allow you to strech just a part of an image, and not the whole image. It can be useful to design for example custom buttons, EditTexts, etc...
You can lean more here: http://developer.android.com/tools/help/draw9patch.html
Nine-patch is to do the stretching on the run time... If you use an Button with a custom background for example and say width-> fill_parent... there is a lot of different devices out there with different resolution how are you going to prepare images for all of them... you give a nine patch and its stretch on the run.
I am trying to use the attached nine patch image as the background for a View in my app (the image is from the android-15 sdk resources). As pictured, the image has two vertical dots to the left of image, two horizontal dots above the image, and a horizontal line below the image. These dots/line from the images show in my app.
I copied the file from the sdk folder into my resources directory, and set the drawable resource as the background for my view. What is the proper way to use this image as the background of a view, such that the dots/line do not show?
By stretchable patches, you're basically telling Android which rows and columns of pixels in the image that you want to repeat. When you stretch a standard image, there are two possibilities: One, it is scaled proportionally, but still loses sharpness due to interpolation; Two, it is scaled disproportionately, and loses not only its sharpness, but its shape as well. An example of a disproportionate scaling is below:
So the purpose of the black lines it tell Android what areas of the image are safe to repeat. The top corner defines the column(s) that it can stretch, while the left corner defines the row(s) that can stretch. The bottom and right corners just define the actual content area (e.g. where the button is allowed to place text), you can reserve extra space to pad the frame. In the image below, you can see that the two black pixels on the outside of the frame define rows on the image, while the one on top defines a column.
And below, this shows the result of a 9-patched stretched out to various sizes. If it's enlarged to be wider, the halves of the image on the left and right of the defined stretchable column are aligned to the left and right of the new size, and the defined column is repeated to fill the space in between. Same thing happens with the defined rows; if you use multiples (I don't believe you can use more than 2 stretch rows/columns) it just evenly pads the space with both of them; in this case I used it to keep the gradient evenly split down the middle.
The lines and dots are what make this a NinePatch. The dots on the left side and the top define the area that can stretch, while the other two lines define the bounding box for drawing things inside the NinePatch.
If you set such an image as a background - those lines will not be shown. To create these images yourself, you can use the draw9patch tool in the android sdk (http://developer.android.com/guide/developing/tools/draw9patch.html).
One thing to remember, is that the file type for a NinePatch image has to be, for example: image.9.png. Otherwise those lines will be drawn.
Do you use the draw9patch app located in your tools folder? This app can be used to simply create a nine patch image.
I'm trying to create an EditText that has the following background images (aligned right) when a user inputs valid/invalid text:
How can I achieve this in Android? I've tried setting the background image using:
editText.setBackgroundResource(R.id.icon_tick), but when I do this, the whole image gets stretched to fit the EditText.
editText.setCompoundDrawables(left, top, right, bottom) where left, top, right and bottom are either drawables or null.
Why does it have to be a background image? Just make it a standard image and position it accordingly in your layout. Otherwise, you'll need to make a large image that actually takes up the entire background of your view (with your icons positioned where you want them). You'll also need to size this image accordingly for the various screen resolutions.
An alternative approach can also be found here:
Background Image Placement
Possible duplicate post.
You can make the drawable as nine patch image. Have some transparent area to the left of the tick mark and define it as stretchable area. That should do the trick.