I am making a lot of ImageButtons where the image aspect does not matter. That is, I do not care if it is stretched. They are invisible buttons to go on top of a background.
Is there an easy way to layout and stretch my ImageButtons through the graphical editor? I do not want to have to test out each possible padding in my xml. I just want to stretch the sides of the images as though it were an Office Word.
Ask if clarification is needed, please.
I'm not really sure to understand your question, but you can use android:scaleType="fitXY" to stretch your images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling.
I'm not 100% sure about your question neither. If you meant you want to do it in IDE, eclipse allows you to define the button with dragging.
Related
I have a very simple question.
I have been asked by my company to make the actionbar just a simple imageview. So no icon, no title and no buttons at all. I've created a custom actionbar, and the imageview is up there just fine. I have attached a link to an image of my layout, and the emulator on the right. They want the imageview to be like it is in the layout. But in the emulator it comes up small.
My question is simply: is there a way I can make the imageview fit like it is in the layout there, and of course have it fit for different devices too - or, must the graphic designer simply create a banner that fits the size of an actionbar. If so, I looked at android's iconography and saw the height should be 48dp. but what about the width? And won't different devices have different width? Or will i be able to make sure it is stretched across the whole width of the screen. I've tried match parent in my xml but of course it won't do anything now because it doesn't want to stretch the height of the actionbar.
Thanks alot
https://www.dropbox.com/s/1tek8ptar3j76d4/Screenshot%202015-02-27%2009.55.30.png?dl=0
Get your graphic designer make you a 9 patch png that will expand in the middle while keeping its proportions then simply add this line of code:
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.your_nine_patch_png));
More on nine patch images here: http://developer.android.com/tools/help/draw9patch.html
Here is the trick!
yourImageView.setGravity(Gravity.FILL_HORIZONTAL);
Happy coding! ;D
I've been trying to find an answer to this all of last evening with no luck so I decided to come ask here. I just started getting into front end dev for Android apps and I'm trying to do something really simple that just doesn't work. All I want to do is add an image on the screen and be able to resize it EXACTLY what size I want regardless of proportions. This is the code I'm currently using inside a relative layout:
<ImageView
android:layout_width="400dp"
android:layout_height="200dp"
android:id="#+id/imageView"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/statsus_logo" />
My image is a horizontal rectangle. When I first add it, it shows up inside a small square centered with a bit of padding all around. When I make the width 400dp that square appears to strech almost 100% across the screen however the logo stays the EXACT same size, centered vertically and horizontally inside this imageview container. When I increase the height of the imageview, the logo increases its size but almost as if it was taking the height and using it as a width. I feel like what its trying to do right now is use the height as its width and the only time when its width is the value i put in, is if the height is also the same value and even then there's some extra unwanted padding.
Again, all I want is for this damn image to be the size I tell it to, so if I want it 5dp wide and 100dp tall, it does just that. Can anyone please help? Thank you very much in advance.
Try to add this attribute to your imageview
android:scaleType="fitXY"
Scale type reference
android:scaleType="fitXY" is the way to scale your image however you want without keeping your proportions however my issue was a little bit different... I'm using android studio and for some reason the settings I was using to "add a new image asset" was making my image act the way I described it above... if I just add an image manually by just click and dragging into the folder, then the image works the way I want it... so long story short, the issue was with how I added my new image asset
thanks to everyone for all of your answers and help!
I'm going to have an application which will look like this:
As you can see, I'll have three buttons (including the envelop icon) aligned in a circle. This is rather easy to achieve in Photoshop, but NOT in android layout XMLs. Which layout class do you think is best in this case?
One more thing, how can I have a completely transparent button with an image in the middle. When I set
android:alpha="0"
the image also disappears which is not what I want. Any idea?
This is rather easy to achieve in Photoshop, but NOT in android layout XMLs.
Not really - you feel it's easy in Photoshop because you know how to do this in Photoshop. Once you have a fair idea of Android layout, that too will become easy for you.
Which layout class do you think is best in this case?
If I were to do this, I would have used RelativeLayout as the position of the two smaller buttons are relative to the larger one. Similarly the buttons as a whole is relative to the screen. So, may be 2 RelativeLayouts - 1 nested inside the other.
how can I have a completely transparent button with an image in the middle
Use transparency as the background.
android:background="#android:color/transparent"
I would like to make a simple Android game where a large background image is displayed and some other images are displayed in specific locations over it, where the other images may be clickable.
Here's a quick sample image of what I'm talking about:
The user should be able to tap the soccer player or the moose (ah, the classic "soccer player moose problem"!)
How should I render this screen (which layouts and views?) so the user can interact with it and it will scale properly on different devices?
I would use a RelativeLayout.
You can set the you background image to the layout (fill_parent for height and width).
You can then put your ImageViews, containing your moose and soccer player down on the layout relative to the top or sides of the sceen, or relative to each other (making sure to specify "dp" units for everything). Set the backgrounds of your ImageViews to be transparent, and there won't be a bounding box problem (and/or you can also set your ImageViews alignment to be relative to each other, ensuring they don't overlap).
I think this is the simplest way to do this - it is then super easy to attach onClickListener to your ImageViews in your Activity, and you are done.
This type of layout will work the same on all devices and screen sizes.
There are some small gotcha's with RelativeLayouts, but they are pretty simple once you get into them, and provide fast rendering (since the view hierarchy is usually shallow). Good Luck.
ImageView for the clickable elements seems like a fine choice to me. For the background I would just set your image as the background of the parent layout i.e. RelativeLayout
SurfaceView for the whole thing (with your field as a background) and regular *ImageView*s for added elements. You can easily recover the click coordinates from the SurfaceView and thus know what element has been touched.
SurfaceView might offer you additional possibilities anyway.
For most images, I'd use an ImageView for each one, like FoamyGuy said.
If they're close enough for overlapping bounding boxes to be an issue, you can still use an ImageView for each, but with a variation of this answer, testing alpha for each ImageView in range.
I would agree with both FoamyGuy and Booger that if your only goal is to place static images onto the screen that do something when you click them, RelativeLayout and ImageViews all the way.
But...
If you are looking to randomly spawn multiple images onto the screen in intervals and have them move around for the player to interact with while explosions are going off and maidens are being kidnapped, you should look into SurfaceView, Canvas, Drawable, TouchEvents, and FrameBuffers.
I am looking for a way in Android how to place some transparent buttons over a (background) image so I have good control to position the buttons and they stay were they meant to be also if the screen is much larger.
As you can imagine the image contains also the button art...
The best thing would be if I could position the buttons by using percentage, but sadly this is not possible in Android.
This is my current base of the code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/art_main_background" >
As an alternative, I could also extract the button art and place them over the image, but this would lead to the same problem, how can I control the position if the buttons are not in a 'linear' kind of order, i.e. rather random.
You should definitely make the button art separate pieces, and then place them in as ImageButtons. You can use a FrameLayout to stack them on top of the image (actually, if it's set as a background image, you shouldn't even need the FrameLayout) but I think you'll have more problems than that if I understand you correctly.
Are you trying to make a single image as your controls, and just map buttons to specific positions? Considering the number of variations in screen resolutions and sizes, this is just a bad idea on Android. Consider rethinking your layout to be a bit more flexible. If you do need absolute positioning, you can use a FrameLayout, and just specify left and top margins to position them, but keep in mind what might fit perfectly on one resolution won't necessarily fit into another properly.
If you can post a sample of what you've got in mind, we might be able to give you some ideas.