developers out there,
I want to make a GridLayout, which contains several imageViews, but the size of each cell of the GridLayout isn't set yet, because the GridLayoutchanges its size depending on the screen size (ConstraintLayout).
Now I've got the problem that when the Image is bigger than the cell, it is shown on the device (in the android studio it is shown).
I already tried things like setting the image as background, background-tint or change android:background="#drawable/inventarslot" to app:background="#drawable/inventarslot" which was said in other posts in this forum.
This is my GridLayout:
<android.support.v7.widget.GridLayout
android:id="#+id/gl_equipeditems"
android:layout_width="0dp"
android:layout_height="75dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView8">
This is my ImageView:
<ImageView
android:id="#+id/iv_slot1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Profil"
app:layout_column="0"
app:layout_columnWeight="1"
app:layout_gravity="fill"
app:layout_row="0"
app:layout_rowWeight="1"/>
Thank you for your help
Greetings Pumpanickel
Since you're displaying the images based on the screen size, I'm assuming you're also calculating the image size as well.
First, try to set your ImageView scale type to fitCenter, or any scale type that fits your needs. Example of scale types are shown below, taken from here:
Later, programmatically change your ImageView width and height based on the calculations you made.
This way you'll be able to display all the images.
Related
I have a multi level LinearLayout to show 4x6 ImageButtons for an advent calendar. But when I want to set the background of the ImageButton, it is too big. How can I set the image background to fit the ImageButton size? I want to show 24 pieces of present boxes in a nice grid.
<ImageButton
android:id="#+id/buttonOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="25"
android:background="#drawable/present"
android:fontFamily="#font/christmas"
android:onClick="playOne"
android:text="1"
android:textColor="#android:color/white"
android:textSize="#dimen/button_text_size" />
I think that what you want to do can be done changing/adding 2 things:
Its an ImageButton, so dont use the android:background property, but the android:src with the same drawable
Then add the property android:adjustViewBounds="true" which basically adjusts the image to fit the view while maintaining aspect ratio
I think that should do the trick, else you can also try
Use the android:scaleType property, I would try using fitXY but here are all the possible scale types: https://developer.android.com/reference/android/widget/ImageView.ScaleType
EDIT: Also, you might need to change your width and height properties, either specify a size in dp, use constraints, or wrap your ImageButtons in a GridLayout
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"
... />
...
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how do i keep the aspect ratio on image buttons in android?
i have 5 square dice as image buttons lined up near the bottom of my layout. i would like them to take up the whole width of the layout, but i can't seem to get them to properly keep that aspect ratio, while taking up the entire width.
currently my code looks like this:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_margin="1dp"
android:layout_gravity="bottom"
>
<ImageButton
android:id="#+id/die1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_weight="1"
android:layout_margin="2dp"
android:scaleType="centerInside"
/>
<ImageButton
android:id="#+id/die2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:layout_weight="1"
android:layout_margin="2dp"
android:scaleType="centerInside"
/> .....
and looks something like this:
if i don't include the
android:layout_height="60dp"
then the dice become skewed and look like this:
however the problem with leaving the "60dp" part in arises when i try to display on a tablet device or something with a high resolution it ends up looking like this:
where am i going wrong?!
You're going to have to do 1 of two things:
1) Provide ldpi, mdpi, hdpi and x-hdpi versions of your dice images. Put those in their respective drawable folder and android will take care of the rest.
Find out how here: http://developer.android.com/guide/practices/screens_support.html
Or
2) Get the screen size and set the imageview sizes manually java. You can get the screen size using:
Display d = ((android.view.WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
screenWidth = d.getWidth();
The rest is up to you. Good luck!
-edit-
Setting ImageView (or any other View's size in Java)
myHeight = screenSize/5; //These are in pixels not dp's
myWidth = screenSize/5;
LinearLayout.LayoutParams myLayoutParams =
new LinearLayout.LayoutParams(myWidth,myHeight);
myView.setLayoutParams(myLayoutParams);
Hope it fares well.
try
android:layout_height="wrap_content"
&
<LinearLayout> attribite is android:layout_height="60dp" replace with android:layout_height="wrap_content"
Set the width of the ImageButtons to fill_parent and use scaletype fitStart for the images that hug the left margin, and fitEnd for the ones on the right. Should do the trick, at least as far as your example image goes. You may have some spacing issues if the proportional width of the images exceed the screen width, but it should work for you.
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!
Hi
I am reading XML from web and displaying them in a listview. The xml contain an image location so I am displaying the image as well along with text.
The image size is 48*48 px as suggested here. But when I see the list view in actual device, the image looks very small. Any idea about how to make that image a little bit big?? is there any standard image and text size??
This is my layout:-
<ImageView
android:id="#+id/logo"
android:layout_gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/name"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#2B2B2B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:layout_toRightOf="#id/logo"
/>
use dip instead of px. when you use dip, it appears more adjustable and good on different screens.
Use an ImageView like this:
Modify the width and height values to your liking, but use the dip measurement. With the scaleType property you can tell the framework to stretch your image.
Try adding
android:scaleType="center"
to your ImageView XML. This will not scale your image, but center it in your view.
It's bad practice on Android to declare layout size by pixels. This page is straight from Google on the subject: http://developer.android.com/guide/practices/screens_support.html I would do something like this
android:layout_height="wrap_content"
android:layout_width="fill_parent"
and see how that looks. Don't forget to test on multiple screen sizes before you finish. You can do that by creating different AVDs for the emulator.
You can use pixels if you really want to, but remember that its going to look different on different devices.
android:layout_height="96dip"
android:layout_width="fill_parent"