Scaling images in Android not working like supposed to - java

I have a long vertical image (224x1600). I want it to fit the image width, and scroll vertically. It needs to be scroll-able. I have tried everything and can't get it to work :\
Here is what I have
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android1:id="#+id/tab3"
android1:layout_width="fill_parent"
android1:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
Second question is: Is there a way to PREVENT image scaling? The image is 224x1632 and shows that way on my galaxy nexus. On the Nexus 7 (much larger screen but lower DP) it goes to 199x1440. A weird scaling of 88%. Can I prevent this? I need it to display 224x1632 on all machines and scale the width accordingly.
Many thanks!

If you need to avoid scaling entirely, you should place your drawables in the res/drawable-nodpi folder. Keep in mind that the physical size will be much different on devices of different densities, but as long as you understand the risks, that is the solution you should pursue.
On your first question, I'm inclined to think you'll need to do some manual work in code, but although I can't test it right now, this also might work:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/tiles"
/>
</ScrollView>
Give it a shot and let me know how it goes.

It should work... I think the problem is that you have android1: and it is suppose to be android:
Something like:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/tiles" />
</LinearLayout>
</ScrollView>
As for the second question, you can try and use android:minWidth="" and android:minHeight="" to see if that solves your problem.
EDIT: try android:scaleType="fitCenter" it will display the image as is.
Hope that helps.

Related

Right to left in android studio

I'm using Android Studio for application development and I have a problem with placing elements in the screen.
For example: I'm trying to place a button to the right of the screen and in the studio its shown to the right of the screen but when I install the app in my smartphone i see it in the left of the screen (my smartphone is right to left configured).
How can i resolve it? (I use RelativeLayout if that matters).
Thanks!
illustration image
It would be easier to answer your question if you included some code. However, assuming your button is inside a relative parent layout which fills the screen, adding this to your button's xml code will make it align to the right:
android:layout_alignParentRight="true"
ie.
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_alignParentRight="true"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp" />
Or to make it really easy, replace your entire XML with:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
android:layout_alignParentRight="true"/>
</RelativeLayout>
The code for those who requested:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"><![CDATA[
android:splitMotionEvents="true"
tools:context="tomer.newapp.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
]]>
<Button
android:id="#+id/btn"
android:layout_width="145dp"
android:layout_height="75dp"
android:layout_column="2"
android:layout_row="2"
android:onClick="btnclick"
android:text="Click!"
android:textSize="30sp"
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
android:layout_marginLeft="11dp"
android:layout_marginStart="11dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Note that the absolute values that you are using are in the tools namespace - this means they are not compiled into your app.
tools:layout_editor_absoluteX="223dp"
tools:layout_editor_absoluteY="140dp"
That is probably why you are getting this kind of behavior.
Remove them and then see if the button moves to the left of the screen.
More about tools namespace - here
You mentioned "(my smartphone is right to left configured)".
This could be a dud answer, but...
With the layout_editor, for either the "gravity" or "layout_gravity" properties, there is a "right", "left", or "start" and "end". In English, one starts writing at the left, and ends at the right. In Arabic, for example, one starts at the right and ends left. Any chance your property says "end", instead of "right"?

How do I make images overlap in android using relativelayout

Consider the image above.
The red lines indicate the image file borders, the black lines indicate the actual "useful" pixels of the image, and the white = transparent.
How would I go about getting these images positioned this way?
I'm not really sure this would make a difference in the way the above is done, but I am planning on implementing basic animations to each image (make each image rotate in respect to their respective centers).
This is what I have so far (positioning the 3 elements in the center, overlapping each other):
<?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:layout_centerInParent="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/img_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription = "#string/img1"
android:src="#drawable/img1"
<ImageView
android:id="#+id/img_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/img2"
android:rotation="45"
android:src="#drawable/img2" />
<ImageView
android:id="#+id/img_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/img3"
android:rotation="200"
android:src="#drawable/img3" />
Thanks!
/e 1 - Thank you for editing in the image
2 - I solved this problem through trial and error, thank you anyways!

Android ImageView scale type centerCrop

I'm creating an activity on Android. I want to show a background image in it. I put a ImageView into my main RelativeLayout.
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:src="#drawable/bg4"
android:scaleType="centerCrop" />
</RelativeLayout>
It makes view like this.
I need to make view like this
How can I do it?
Try set ImageView attribute ScaleType=fitEnd ?
use this code.it's give the out-put as you want.
Code
<?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" >
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="150dp"
android:background="#android:color/background_dark"
android:layout_height="150dp" >
<ImageView
android:id="#+id/bgImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:scaleType="centerCrop"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
one advice if u give the imageview width fill parent then it's contain whole space according to main container.
Best Luck
Set the android:background="#drawable/bg4" in the RelativeLayout like that:
The whole XML should look like that:
<RelativeLayout
android:id="#+id/relativeLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:background="#drawable/bg4
android:orientation="vertical" >
</RelativeLayout>
First, I believe there are a couple of things wrong in the xml:
RelativeLayout does not make use of an orientation, you should remove that
Is this your full xml file? If so then the alignParentLeft in the RelativeLayout doesn't do anything because that is an attribute you use if it's parent is a RelativeLayout.
fill_parent is the deprecated name of match_parent, you should use that.
I also believe that code you have would center the image on the screen putting anything that is to big evenly on both sides and not at all what you said it does.
If you just want a background you can just use android:background="#drawable/bg4" in the RelativeLayout
EDIT: Changing the layoutparams of the ImageView to wrap_content both height and width and removing the scaleType alltogether might make it like you wanted it to be.

Left align text inside a button in Android

I want to align text of a button to the left, I don't know how to do this, please help me how to do this in the xml file. I didnĀ“t find the properties for this.
Maybe this will help you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:text="Button" />
</LinearLayout>
You probably want both
android:gravity="left|center_vertical"
and then a little bit of space
android:paddingLeft="5dp"
to keep the text from running up against the left edge of the button.
(Adjust the amount of padding_left as appropriate for your button art.)
android:gravity="left"
Hope This will help
android:gravity="left|center_vertical"
It will work but now you may need to add some padding left according to your requirement.
android:paddingLeft="10dp"
This is combination from above that worked for me, icon on left and text left aligned with some padding
<Button
android:id="#+id/butt_labs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/dt_labjob"
android:paddingLeft="10dip"
android:gravity="left|center_vertical"
android:text="#string/l_labs" />
You probably need BOTH the android:gravity AND the android:layout_gravity to align text to "left".
As everyone else has mentioned, "left|center_vertical" works, but I have found that if left or right is not explicitly stated, most properties default to left.
So putting the following should be enough to get your text left-justified and vertically centered:
android:gravity="center_vertical"
If you have to support many different languages it is better to use start or end to position text. That way for right to left (RTL) languages it will still work.
Example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="start|center_vertical"
android:text="Button" />
</LinearLayout>

How to place a text between two images in Android

I want to create layout where I have two images at left and at right and text in center.
I have tried to do it with relative layout but unfortunetly it was unsuccessfully. Could anybody provide me an example?
Have you tried something like?
<?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="wrap_content">
<ImageView
android:id="#+id/img1"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:src="#drawable/image1"/>
<ImageView
android:id="#+id/img2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:src="#drawable/image2"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/img1"
android:layout_toLeftOf="#id/img2"
android:layout_alignTop="#id/img1"
android:text="I'm between!"/>
</RelativeLayout>
If you don't need more things on your view, you could want to use LinearLayout instead, since it's more easier to implement. In that case, you just have to play with the layout_weight attribute.

Categories