Android image scaling Layout issue - java

I am trying to design an application and need to create a very simple menu bar with one image button i have created the image for the button for all the resolutions and an image bar of the same height for all the different resolutions but my problem is the image button which has to sit on top of the plain image is always taller and i cant figure out why, example under hdpi i have the menu bar image 1024x72 and another image for the button 72x72 but when rendered the image button is always larger I'm going crazy trying to find an answer any help would be greatly appreciated.
this is my current not working code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_menu_bar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<ImageButton
android:id="#+id/new_convo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignParentTop="true"
android:src="#drawable/ic_menu_new_convo" />
</RelativeLayout>

For a menubar a LinearLayout is the best choice. You have to specify a gradient or a 9-patch for the background, and then using your ImageButtons. On Android there's no guarantee that the window width is 1024px, so you must change your mindset and approach the UI design in a way different from other mobile platforms. If you provided a sketch of the desired result we could understand (and help) better

Related

Android game: remove padding either side of screen

I am relatively new to Android and am in the process of making a pretty basic game. I want to know how to remove the padding at either side of the main game screen, so that the game will take up the whole screen of whatever device is being used.
I originally thought it had something to do with the dimens.xml file:
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
I tried changing both of these to 0dp but the white padding remains on the screen.
Also - in activity_play_game.xml I have the following:
android:layout_width="match_parent"
android:layout_height="match_parent"
And I'm thinking this may have something to do with it? Does anybody know where I'm going wrong?
Many Thanks!
Add this as your Activit layout.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
</RelativeLayout>
You should see whole screen filled with black. Then try to add this inside
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:text="Margin Padding Test" />
And try to add android:padding="20dp" and android:layout_margin="20dp" into both RelativeLayout and TextView. When you try different combination of those attributes you should be able to update your layout to look as you need.
Margin change how layout is positioned in its parent.
Padding change how layout is positioned within itself.

Why image changes when imported to eclipse?

First I should say that I'm completely new to android so maybe my question is very ordinary for you.
Problem : I've designed the following image (png-24 with transparent background) in the Photoshop
and added it to the res/drawable in my project to use it as the source of a ImageButton but it appears like this:
As you see the rounded shape has changed completely and a gray background is added to the image.
Please tell me how can I fix this? Is the way that I've used this image wrong or the image format or some thing else?
Edit: I just resized the image(simple png) as I've added to my post and suddenly a corner of the image appeared,isn't it due to size or something like that?
This is the layout of the containing activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/showHistBtn"
android:src="#drawable/showhistory" />
<ImageButton
android:id="#+id/findWordBtn"
android:src="#drawable/find_aword" />
<ImageButton
android:id="#+id/insertNewWordBtn"
android:layout_width="315dp"
android:layout_height="200dp"
android:src="#drawable/insertnewword" />
</LinearLayout>
You can simply draw a 9-patch image with Photoshop drawing the shadows and put it into the drawable folders and set this in your ImageButton in xml:
android:background="#drawable/your_9patch_image"
I always do it by using Photoshop, it's super simple. For more details see this: Draw 9-patch
After our long discussion in comments, see what a 9 patch can do (it's just to give you an idea of what you can really achive with just 1 image):
By using this image (/res/drawable/my_btn.9.png):
I got this result
You'll notice that the text isn't well centered: it's because I was in a hurry, and didn't make it perfect (while resizing and black-bordering for 9 patch).
And the starting image wasn't high-quality.
I used a 16sp font, probably you'll use a 32sp or even a bigger one.
Note: mind the extension: .9.png
I just added these to the ImageButton tag in the activity layout xml file and it fixed :)
android:scaleType="fitXY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dp"
I found the answer completely by intuition but with trial and error found that none of the above statements should not get omitted (for future viewers).

Filling a background image to screen without changing aspect ratio

I'd like to display an image as a background for my activity, but would like to make it fill to screen without changing the aspect ratio of the image (something like this).
I tried scaling the image, but that doesn't seem to work (because I'm using the image as a background, not defining it in an imageview, or so seems the problem).
How do I go about doing that? I even tried to define the background image in its own xml file, but that still didn't work.
If you uses a RelativeLayout you can put a ImageView than fill all layout and put the rest over it. Something like that:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/background" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
// Your full layout
</LinearLayout>
</RelativeLayout>
AFAIK the only solution is the one with ImageView you mentioned. Why it is not appropriate for you? You can also use Bitmap.createBitmap() method to resize your bitmap do desired size.

ImageView within RelativeLayout is not matching parent

So, I'm trying to view a camera preview as a background with an imageview on top of it. Check this question out, that I've asked, to get a better idea of what I'm trying to accomplish. However, instead of using a FrameLayout, I found it easier to use a RelativeLayout (for the reason of placing views where I want on the screen). The problem is that I can't get the imageview (over the surface view) to take up the whole screen. No matter how I re-size the image it still displays in the center without filling the screen.
So, my question is, how would I make the imageview fill the entire screen over the surface view? Perhaps, I am making a simple mistake but if that is the case please point it out. Any help will be greatly appreciated! Thanks!
Here's the XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/relative" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/image"
android:src="#drawable/background" >
</ImageView>
<Button
android:id="#+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="Scan"
android:textSize="20dp"
android:textStyle="bold"
android:onClick="Scan" />
<Button
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/scan"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Search"
android:textSize="20dp"
android:textStyle="bold"
android:onClick="Search" />
<Button
android:id="#+id/importing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="Import"
android:textSize="20dp"
android:textStyle="bold"
android:onClick="Import" />
</RelativeLayout>
And here's the code where I place the views (bring to front so they are visible over the surface view):
mPreview = new CameraPreview(this, mCamera);
relative.addView(mPreview);
layout.bringToFront();
scan.bringToFront();
search.bringToFront();
friendImport.bringToFront();
EDIT:
Here's an example image of what I'm talking about (I am trying to get the white transparent image to cover the whole screen):
Am I correct that you want to stretch android:id="#+id/image" into your entire layout?
First check in layout view that the ImageView takes entire space as it is supposed to.
If so, then add the proper scaling type to your ImageView attribute list:
android:scaleType="fitXY"
Or as an alternative, apply the image resource not as "src" but as "background":
android:background="#drawable/background"
Good luck.
After experimenting for a while, I seem to have found a solution to my problem. My image was showing up similar to this picture:
Which is weird, because this is how a 9-patch image works and I did not declare my image as a 9-patch. But even when I called match_parent (or even fill_parent) it only filled to the fill area like in the image I posted.
So, with lots of trial and error, I took away any border that was on the image and took all the blur effects off. The image was, for some reason, a dramatically smaller file size. Then, I tried to place this as the background and... success!
I'm not too sure as to why it did not work to begin with, but, I have a theory that, perhaps, because there was a 1px border on the image it saved it as a 9-patch PNG instead of a normal PNG file. Or maybe it was due to the blur effect that was on the image. I'm not too sure, but, I do know that once I took off the blur effects and removed the border it did work.
I hope this is useful for anyone who might be faced with a similar problem!

need some help in android game developing?

I'm developing a game for android and it's like a match game it looks like that:
the game work perfectly but the problem is the size of the cards " the clowns in the picture "didn't change I've tried everything from changing the size of the image to change the size of the image view, the xml file is a Tablelayout and each row table contain something in the first row there is the image view that will display the card and this is the xml code
<TableRow
android:id="#+id/Row01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="#+id/ImageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom|center"
></ImageView>
</LinearLayout>
</TableRow>
As mentioned previously, you should probably avoid static values when designing your layouts as much as possible so they remain scalable from tiny-phone to mega-tablet.
This is a common problem for people who develop android and in this case it's probably a screen density issue. I would suggest making three different sized sets of your clown icon and putting them in the low, medium, and high resolution image folders in your "res" folder. That's the reason they have those folders, if your application is scalable across screen sizes the system will pick the appropriate resolution image from the folders when you make a call on that resource. You can also modify how the system chooses density in the manifest I believe.

Categories

Resources