ImageButton resizes automatically in gridlayout - android

I'm asking this (propably) pretty easy question since I can't figure it out by myself and googling in for more then an Hour doesn't give me proper results.
So the question is: How can I make my items(generally those are only 16 image buttons) so they can stay in scale (comparing to gridlayout) when my picture is really large (So in short way: I load 16xImages as src on buttonImages, each Image is 640x640, and I want to have those 16 buttons in grid layout scaled comparing to grid layout width/height and column/row count). I hope this is possible to write in XML since I don't want to refactor my java code :/
Ps. I'm newbie to Android programmig
Ps2.Here is my GridLayout with button example , but it's completly wrong :/
<GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="4"
android:rowCount="4"
tools:context=".GridXMLActivity"
android:layout_below="#+id/Tx1"
android:layout_above="#+id/restart"
android:layout_marginBottom="50sp"
android:layout_marginTop="50sp"
>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:src="#drawable/clear"
android:id="#+id/But1"
android:scaleType="fitXY"
android:onClick="but1"
/>
[EDIT]
I've even made such layout , but there are some problems. 1st is that my grid layout doesn't scale at all!(So when I take screen with lower or higher resolution it won't fit). That's why I wanted to make this button some kind of resizeable (in this screen each button has width and height at 60sp which I know it shouldn't)
Another problem is that I'm working on button backgrounds and I wanted to make it src.
Genereally I want to achive same thing like on the screen , but making it other/more flexible way (So the resolution and orientation won't affect my gui and sizes of it's element). If you need whole XML file I'll post it here but it's a bit mess since I've copy-paste 16 times Image Button

If you want something to scale, keeping aspect ratio and you have one dimension set to match_parent, the other must be wrap_content.
So, presuming you want the button to always fit the width but stay at the correct aspect:
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
... />
To make all items fit in grid rather than all items fit their images, you should use weight. Weight with grid layout requires the support library before API21
<GridLayout ...
android:columnCount="2">
<ImageButton
app:layout_gravity="fill"
app:layout_columnWeight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
... />
...

Related

Exact view position on top of ImageView - android

I'm trying to make an interactable imageView by setting some invisible image buttons on top of it, the user can then click different parts of the image and do things with it, the problem is that i need this image buttons to be in very exact positions in relation to the image view, if the phone size varies even a little, the image buttons will get off the positon i want them to be.
The xml where the image view is:
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".fragments.Offroad18nitro4wdbuggy_frontFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="OffRoad 1/8 nitro 4WD buggy front" /> <!-- Here just to show the programmer which fragment is showing -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true">
<ImageView
android:id="#+id/ivLosi4_offroad18nitro4wdbuggy_front"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#drawable/losi4_18scale_front"/>
<ImageButton
android:id="#+id/ibTest"
android:layout_width="50dp"
android:layout_height="41dp"
android:alpha="0"
android:background="#color/colorGreen"
android:layout_alignParentEnd="true"
android:layout_marginEnd="40dp"
android:layout_marginTop="5dp"/>
</RelativeLayout>
As you can see in the second screenshot, the image button is out of place, my phone is the Samsung Galaxy S6.
Screenshot of the image button in the exact position i want it to be
Screenshot of the app being emulated to my phone
I've dealt with this exact problem before. The workaround that I managed to accomplish is to divide the screen up into several sub-layouts so it looks like a grid of layouts. First divide the screen in half at the center point, then place an empty textview with no dimensions in the center of the top and bottom layouts, which will allow you to divide those layouts into four. Then do the same thing for those four to create sixteen if you have to. Then place your image buttons along the sides or the attached to the center point of whichever of these layouts contains your desired area.
The point is to create a grid that is fixed relative to the center of the screen. That way, if the screen size changes, the grid and the buttons attached to it will adjust accordingly. Its not a perfect solution but it worked for my purposes. Here is a link to a sample xml file from the project that I used this strategy in:
https://github.com/ribalding/AdventureAdventure/blob/master/app/src/main/res/layout/activity_kitchen.xml

Android: How do I align textview on a image background that dos not strech in xml

Ok here is the problem...
I have a image background that need some text and additional graphics on it. The background image needs to be in the center of the screen and may not stretch. Here is what i need to do:
The problem is that i need to align the text to the background image.
I've tried to wrap it all into a relative layout - something like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bg_image"
android:layout_centerInParent="true"
android:src="#drawable/member_card"/>
<TextView
android:layout_height="wrap_content"
android:layout_alignLeft="#id/bg_image"
android:text="#string/membercard_info"
android:layout_marginTop="40dp"
/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignLeft="#id/bg_image"
android:layout_marginTop="200dp"
/>
</RelativeLayout>
This will not work since android adds additional padding to the image to avoid it from stretching.
Here is what happens in two different cases:
So how do I align the text to the background image?
I've solved this problem in the past in code by baking it all into one image,- but would like to do this in xml.
If you want to remove padding, you can use manually set it. However, if you want overlapping elements, you generally want to use FrameLayout. Look here: http://mobile.tutsplus.com/tutorials/android/android-sdk_frame-layout/
Set a gravity inside the frame layout to align it.
if you want an ImageView with additional layers drawn on top of that, see this thread: How to maintain multi layers of ImageViews and keep their aspect ratio based on the largest one?
There a padding around the image because you set the imageView size to fill its parent ( using match_parent )
You should try to set it to wrap its content :
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
EDIT : If your picture is bigger that the screen size, you need to have it scaled keeping the aspect ratio.
To do this, use match_parent in vertical with a scaleType to FIT_CENTER
and keep the wrap_content setting for the width ( since we want the image view left/right bounds stuck to the image content )
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitCenter"
.../>
Is this better ?

Creating a Bookshelf view in Android

I have seen many e-book readers with the Bookshelf design where books are placed like iBooks for Apple or iReader for Android. I am trying to build a similar bookshelf UI in Android. I have prepared the graphics for the layout namely shelf_left (34*180), shelf_right (34*180) and shelf_center (652*180). Currently I am targetting 720*1280 pixel density devices, so I have kept the total width as 720px. I am using dp as my length unit.
I am using RelativeLayout as my root layout. When I am putting all my images in the layout, the images are not getting aligned properly. The shelf_center gets extra top and bottom padding and the side images looks too big.
I am not able to figure out how to control this behavior. Below is the code for my main.xml layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/shelf_left"
android:layout_width="34dp"
android:layout_height="180dp"
android:adjustViewBounds="true"
android:contentDescription="#string/button"
android:src="#drawable/shelf_left" />
<ImageView
android:id="#+id/shelf_center"
android:layout_width="652dp"
android:layout_height="180dp"
android:layout_toLeftOf="#+id/shelf_right"
android:layout_toRightOf="#+id/shelf_left"
android:adjustViewBounds="true"
android:contentDescription="#string/button"
android:src="#drawable/shelf_center" />
<ImageView
android:id="#+id/shelf_right"
android:layout_width="34dp"
android:layout_height="180dp"
android:layout_alignParentRight="true"
android:adjustViewBounds="true"
android:contentDescription="#string/button"
android:src="#drawable/shelf_right" />
</RelativeLayout>
This is how it looks:
http://img210.imageshack.us/img210/2885/shelf.png
Please help me figure out what am I doing wrong here.
Seems like the center image is too wide to fit the view and is scaled down. Note that the vertical wooden sides in the side views don't match to the image of the bottom view. Have you tried using android:scaleType so that the images don't get scaled?
Also, double check that the measurements in pixels (you mention the images' sizes in pixels) match the values in dp set in the XML.

Maintain circle image ratio for button on varying screen resolutions

I have an image button with a background image .png of a circle. I am testing it on different resolution screens and it looks different on every one. Most of them distort the circle shape by stretching it on one dimension.
What is the correct way to handle this? I am familiar with the 3 density levels needed for the highest quality image, but I think the problem is with the layout type attributes on either the image button itself or the parent container.
Snippet from main.xml...
<LinearLayout
android:id="#+id/buttonArea"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical"
android:padding="30dp">
<ImageButton
android:id="#+id/button1"
android:background="#drawable/button_inactive"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>
removing the layout_weight attribute from the ImageButton fixed most cases, but not all. It seems that the padding is still changing the ratio of the circle. Scale types have no effect. Is it because my image is set as the background and not the src?
I think android:layout_weight="1" in your ImageButton is the cause of this. It will make your ImageButton the same size of your screen, no matter what size the screen is.
Try to remove that attribute. If that doesn't fix your problem, have a look at android:scaleType attribute
<ImageButton
android:id="#+id/button1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center"
android:background="#null"
android:src="#drawable/button_inactive"
android:scaleType="centerInside"/>
You can set the scaleType attribute of your ImageView, to one that keeps the aspect ratio of your image.
They all behave slightly different, so you'll have to use one that suits your needs (CenterInside is a good one to start with).
If this doesn't work, you can always specify set heigh/widths of your image (e.g. layout_width="128dp).

Limit the width of buttons in Android

I have two buttons positioned horizontally next to each other and want to limit the width of the buttons when the screen resolution goes up. For mdpi, the two buttons should be equal length and take up the entire width of the screen. If you then switch to a higher resolution, the two buttons can grow in width if necessary but only up to a maximum width of say 100dp. I have not been successful in accomplishing this. The two buttons end up being the same size but they fill the entire width of the screen. I have tried putting the buttons in a table, in just a LinearLayout and have tried using the maxWidth on various combinations but to no avail. If I use a table, I have tried stretching the columns and not stretching them. That didn't help either. Any suggestions?
You say you've tried maxWidth...but you don't show any examples of what you tried, so here's my suggestion, which I'm nearly certain should work for you:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:maxWidth="100dp"
android:text="Button 2"
/>
</LinearLayout>
You haven't put your xml view here. How can we find an error?
Probably, you have used background image and width set to wrapcontent ? Change the source image size to smth small
You can try creating different layouts for different resolutions. This is not the best way of solving your problem, but this will surely work. Good luck!

Categories

Resources