I've created a 9-patch background for EditText.
XML code:
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#drawable/searchnew"
android:ems="10"
android:hint=" Search recorded files..."
android:textColor="#646464" >
</EditText>
XML Preview:
Real device:
9-patch image:
To be honest it isn't even bothering me that much, but I'm still wondering why the hell is it happening? Why is the EditText background wider and higher on real device?
Bonus unrelated question: any idea how could I get rid of the "ugly" corners? You can see pixels individually if you look closely, but I want a pixel perfect design.
Just found the reason after doing some more research. The text inside the EditText has some default padding. That's why the 9-patch was stretched more when I ran it on real device. All I had to do was to set padding to 0.
Setting top padding to 0 example:
android:paddingTop="0dp"
Do it like this for top, bottom, left, right and background will stay fixed :)
Related
Okay this is probably a newb question but I can't find any solutions on google.
I'm a new android programmer and I made a simple hello world type program. I didn't do any funny configurations in the layout xml file but for some reason on certain devices the screen looks really zoomed in and almost kinda pixelated.
Is there some kind of common reason for this? All I have on my linear layout is an EditText widget, a textview widget, and two button widgets.
My Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/txtUser"
android:layout_height="wrap_content"
android:layout_width="200px"
android:text="" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Submit"
android:onClick="userSubmit" />
</LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Clear Screen"
android:onClick="clearScreen" />
<TextView
android:id="#+id/txtMain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16px"
android:text="Ready\n\n"
/>
</LinearLayout>
It's probably some version of the screen compatibility mode which attempts to make old apps compatible with newer large displays, scaling the UI up as necessary. Scaling causes pixelated look as well.
If you don't specify a targetSdkVersion in your manifest, it defaults to 1, enabling all compatibility modes.
Solution: Specify an actual target SDK version you're developing your app on. Usually it should be the highest one available.
It is because you should use DP dp units instead of Pixels px since DP works with the screen density. Remove things like android:layout_width="200px" and change them to android:layout_width="200dp"
PS: note the following:
Generally you do not want to set dp values, you should do pretty well using wrap_content or match_parent, since the LinearLayout calculate widths and heights for you. Try to resist the temptation of setting fixed values to views, unless is extremely necessary. As an experience Android developer I tell you that cases you wanna set fixed width/height are cases like:
You are downloading images from the Internet and you want the ImageView that will show it to be displayed as a placeholder until the download completes.
You're creating an horizontal gallery and you need to give it a maximum height so it wont surpass the screen.
In resume, complicated stuff like that. If you're starting, try to learn to use wrap_content and match_parent, it will save you a lot of headache later on :)
Am running into something that I have spent hours trying to get working. I have tried searching but what stumbles me is how to search a resolution for this as it is a strange and apparently not-so-common issue?
All I want is to be able to stretch a 'dash of paint' vertically using the y-axis scalable area marked as a single pixel on the left. My 9patch file is below:
I have gotten this to HALF-work as can be seen (ignore the top horizontal line, this is a separate imageview):
As you can see, I am missing what I call the 'Non-scalable end' which is basically the static part of the 9patch file (unmarked on left y-axis scalable area)
The code in the layout is simple:
<ImageView
android:id="#+id/repeater"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/PlayersList"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/PlayersList"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/repeater" />
(file is named repeater.9.png and stored in res/drawable-xhdpi)
Note: For some reason this works exactly as required in the 9-patch tool, but doesn't seem to work in eclipse (earlier screenshot):
Is this the effect you were looking for ?
I have attached images for all drawables let me know if it worked.
You can use http://android-ui-utils.googlecode.com/hg/asset-studio/dist/nine-patches.html
to generate 9 patch images instead of making it form default tools.
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/ic_example"
android:scaleType="fitXY"
android:contentDescription="#null"
android:adjustViewBounds="false" />
I'm trying to make a 9-Patch image to use for Android application.
Here is a screenshot from 9-Patch tool with my image:
I'm adding on layout this image:
<Button android:id="#+id/tiny"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:text="Text"
android:textSize="8sp"
android:background="#drawable/arrow_active" />
(Image is located in "drawable" folder and image name is "arrow_active.9.png" )
Here is a screenshot from eclipse:
On real device I also don't see the arrow of image.
I'm tried with ImageView and Button, layout is TableRow.
Also I tried with smaller image:
On real device I see black pixels added by 9Path tool.
Try this tool...
It is graphically well, and auto generate images for all buckets. You can set streatch regions and content padding from it..
I am trying to use the 9-patch technique to create the button.
It looks almost great in the Layout Window of Eclipse
When I run it in the emulator it shows up like this:
Here is the actual PNG image
Selector XML
<item android:drawable="#drawable/splash_facebook_button" />
Button Code in Layout
<Button
android:id="#+id/button1"
android:layout_width="208dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dip"
android:background="#drawable/splash_facebook_button_selector"
android:text="Sign up with Facebook" />
Looks to me like it's not treating the image as a 9-patch. Have you saved it with the .9.png extension?
Edit:
Had a better look at the actual image and I don't think you've quite got it set up correctly. The bottom and right border define where the content will go (in this case you want it over to the right, with a bit of padding).
The top and left define the areas that will be stretched when the image is scaled. In this case you want it to be areas of constant blue that get stretched, rather than the logo. I've updated the image with an example of how to do this:
This works for me!
I have a list view with a custom layout. In the layout I display a series of comments. The problem is the image I've set as the background gets stretched when the comments are too long.
I know I could probably use a nine patch but the question is would it really work? If not what alternative do I have?
Could someone recommend the best of handling this.
<TextView android:id="#+id/txtCommentBody"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="" android:textColor="#color/black" android:paddingTop="25dip"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:layout_marginTop="10dip" android:background="#drawable/comment_bg">
</TextView>
A nine-patch would work provided you pad the top of your text so it's below the part that won't scale. Alternatively, truncate all your comments to some reasonable length that will fit, and let people click them to read the whole thing.