ImageView source will not scale - android

This should be easy, but I could not find a solution. I have three ImageViews inside a RelativeLayout. These ImageViews will represent three buttons (I tried using ImageButton as well, same problem there).
Thing is, the three sources, bluered, green and blueyellow are *.png files of some size (in this case larger than the buttons/ImageViews size). I want these sources to scale to fit into the ImageViews and use the dimension of buttonsleftRightMenu. The sources are quadratic images, so this should be easy. Where do I go wrong?
xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#integer/leftMenuID"
>
<ImageView
android:layout_width="#dimen/buttonsLeftRightMenu"
android:layout_height="#dimen/buttonsLeftRightMenu"
android:src="#drawable/bluered"
android:id="#integer/leftMenuButton1ID"
android:clickable="true"
android:contentDescription="#string/leftMenuButton1ContentDescription"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
/>
<ImageView
android:layout_width="#dimen/buttonsLeftRightMenu"
android:layout_height="#dimen/buttonsLeftRightMenu"
android:id="#integer/leftMenuButton2ID"
android:clickable="true"
android:src="#drawable/green"
android:background="#color/transparent"
android:contentDescription="#string/leftMenuButton2ContentDescription"
android:layout_below="#integer/leftMenuButton1ID"
android:scaleType="fitCenter"
/>
<ImageView
android:layout_width="#dimen/buttonsLeftRightMenu"
android:layout_height="#dimen/buttonsLeftRightMenu"
android:id="#integer/leftMenuButton3ID"
android:clickable="true"
android:src="#drawable/blueyellow"
android:background="#color/transparent"
android:contentDescription="#string/leftMenuButton3ContentDescription"
android:layout_below="#integer/leftMenuButton2ID"
android:scaleType="fitCenter"
/>
</RelativeLayout>

This was just a stupid mistake. The code works perfectly, just remember to inflate the corrext xml file.

Related

Android: Problems with adjusting view size in an ImageButton

I'm trying something that should be simple, an imageButton and some text that says something about it. My problem is, the view of the imageButton is bigger than the image itself. I'm certain that the actual image is not like this (I've tried with several images and the result is the same). By searching online, the solution I've found is to insert the attribute "adjustViewBounds". That seemed reasonable but it didn't work. Here I leave my code and an image of how the result looks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top">
<ImageButton
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitXY"
android:scaleX="0.2"
android:scaleY="0.2"
android:src="#drawable/icon1"
android:padding="0dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DEFAULT!"
android:textSize="20sp"
/>
</LinearLayout>
image
The problem is using scaleX and scaleY to adjust the size. The button changes it's visible size, but the rest of the layout acts as if it were the original full scale.
I'd suggest using a different method of defining the size, for example:
<ImageButton
android:id="#+id/imageButton"
android:layout_width="48dp"
android:layout_height="48dp"
android:background="#android:color/transparent"
android:scaleType="fitXY"
android:src="#drawable/icon1"
android:padding="0dp"/>

Bring an ImageView in front of another?

Is there anyway to bring an ImageView in front of another in android . For example , i have a big image and how i put a small one in the top left of the big ones. Something like the small one will overlap the bigs one . thanks for helping
Wrap the images in a RelativeLayout. Place the image you want on top last in the xml, like so:
<RelativeLayout
...>
<ImageView
android:background="#drawable/backgroundimage"
... />
<ImageView
android:background="#drawable/foreground"
... />
</RelativeLayout>
Use framelayout as your root layout and then place your big image and then your small image
like this
<framelayout
......
......
......>
<ImageView
// your big image
......
.....
.....>
</ImageView>
<ImageView
// your Small image
......
.....
.....>
</ImageView>
</framelayout>
For me frameLayout is best choose ,
Than you will need just to call this line
ImageViewSmall.bringToFront();
no need even for this line , just create from code imageViews first big after small. position x y also with :
ImageViewSmall.setX( float X );
ImageViewSmall.setY( float X );
I used this to create a video "play" overlay over a thumbnail as suggested by Randy.
Here's some slightly more complete code which also makes sure the images are centered in the parent
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/ivBackground"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:src="#drawable/downloadplaceholder"/>
<ImageView
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
android:id="#+id/ivPlayOverlay"
android:src="#drawable/play_button_overlay"/>
</RelativeLayout>
You can use RelativeLayout; you can test this code, I test it and it works fine:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/a_t1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:background="#000000" />
<ImageView
android:id="#+id/a_t2"
android:layout_width="34dp"
android:layout_height="34dp"
android:layout_gravity="top|left"
android:background="#c12fff" />
I have tried to do a similar thing with an edit text in front of a linear layout, I found that just placing the XML for the edit text after the XML for the linear layout brought it on top when the app was run. Hope this helps.

Handle ImageButtons with different image sizes

My layout contains 3 ImageButtons, arranged vertical in a LinearLayout.
Now I want that each of them has the same height and together fill the device's screen height.
Works fine with the attribute android:layout_weight="1". But if the image of one ImageButton is too big, it won't work (this button is higher than the others), despite setting android:scaleType="center_inside".
Any advices/tricks?
If you need any code, let me know. But there is nothin special.
If you have given weights correctly than this should work. The size of the image doesn't matter than. Just one thing to keep in mind while using weights is that attribute for which you are giving the weight(height/width) should be assigned value "0dp" in the xml, only then the weights will work correctly. Here is the example code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:weightSum="3">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:src="#drawable/drawable1"
android:layout_weight="1"
android:scaleType="centerInside"
/>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/drawable2"
android:scaleType="fitXY" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/drawable3"
android:scaleType="fitXY" />
</LinearLayout>
Just use this xml and replace the drawables according to your needs. Let me know if any issues.

Does the image in a LinearLayout must be the same size?

i have wrote a program to test the MediaPlayer class, and it has three ImageButtons——"play","pause" and "stop". at the beginning, i uses three different size .png pictures for the three ImageButtons, and the program cannot be run on my AVD, and then i changed them to three .png picutres that have the same size, then it runs correctly this time.
does the image uses in a LinearLayout must be the same size??
here is the latyout file:
<?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="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/play">
</ImageButton>
<ImageButton
android:id="#+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/pause">
</ImageButton>
<ImageButton
android:id="#+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/stop">
</ImageButton>
</LinearLayout>
</LinearLayout>
I have tested your code it is fine but problem was in images you remove .png in your image it will work. Cool because .png is already in image and you also add .png in images so that remove .png from image.
does the image uses in a LinearLayout must be the same size??
No. The wrap_content parameter will make the encapsulating LinearLayout only as big as the the largest ImageButton.
The problem may lie with the names of the drawables. Make sure your all your .png images follow android naming conventions (names can contain only lower case letters, the numbers 0-9, or an underscore).
Also check the images themselves aren't corrupt.

layout with buttons in a circle?

Its a challenge for all..
How we can put buttons in relative or linear layout or any other layout in a circle same as in this image. We can do it using xml or using code. Please help.
Thanks in advance
This method is the layout equivalent of plotting arbitrary X and Y coordinates.
Features:
The child objects are automatically centered in the RelativeLayout whether you use it as the top-level layout or embed it in another.
For N images your layout contains only N+1 views.
This example layout shows how to place an ImageView in each quadrant of an XY grid with origin at the center:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/center"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:background="#FFFF0000"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FF00FF00"
android:layout_above="#id/center"
android:layout_toLeftOf="#id/center"
android:layout_marginRight="100dp"
android:layout_marginBottom="25dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FF00FF00"
android:layout_below="#id/center"
android:layout_toLeftOf="#id/center"
android:layout_marginRight="100dp"
android:layout_marginTop="25dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FF00FF00"
android:layout_above="#id/center"
android:layout_toRightOf="#id/center"
android:layout_marginLeft="100dp"
android:layout_marginBottom="25dp"
/>
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#FF00FF00"
android:layout_below="#id/center"
android:layout_toRightOf="#id/center"
android:layout_marginLeft="100dp"
android:layout_marginTop="25dp"
/>
</RelativeLayout>
I don't think this could be done with a layout different then AbsoluteLayout. How exactly it should be done, you have to try yourself, because it is kinda tricky, but possible!
This is what I had in mind and by the way not sure on 100% but with some efforts you could set it up to be proportional - to pass on different resolutions.
In case you copy&paste the code sample from the chosen answer, you are using Android Studio and it doesn't work, here is the solution: Replace #id/center with #+id/center
Hope it helps somebody. As my reputation is less than 50 I cannot add this as a comment.

Categories

Resources