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
I have an ImageView in top of RelativeLayout but image in RelativeLayout has space than right and left of page(this space is a little). I don't know how can solve this :( if i set image with more width, again image has space than right and left of the page. Why?
<?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">
<ImageView
android:id="#+id/registerAction"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:src="#drawable/testing"
android:adjustViewBounds="true"/>
</RelativeLayout>
I test my program on the Emulator. Do this problem has for Emulator? If i test program on the phone, the problem will be solve?
Thanks.Cheers
Please try it with Device, it will solve your problem.
And please add one attribute for your ImageView, android:scaleType="fitXY".
Please check your image. It might have transparant or white space along the border.
Aligning images depends upong your image content too
I see many applications that use a full-screen image as background.
This is an example:
I want to use this in a project, the best way I've found so far to do this is to use an image with a large size, put it in a ImageView and use android: adjustViewBounds="true" to adjust the margins
The problem is that if a screen with a very high resolution, the image falls short.
Another option I thought of is to use the image in a FrameLayout, with match_parent in width and height as background... this stretches the image, but I think the result is not very good.
How would you do it?
There are several ways you can do it.
Option 1:
Create different perfect images for different dpi and place them in related drawable folder. Then set
android:background="#drawable/your_image"
Option 2:
Add a single large image. Use FrameLayout. As a first child add an ImageView. Set the following in your ImageView.
android:src="#drawable/your_image"
android:scaleType = "centerCrop"
Another option is to add a single image (not necessarily big) in the drawables (let's name it backgroung.jpg), create an ImageView iv_background at the root of your xml without a "src" attribute.
Then in the onCreate method of the corresponding activity:
/* create a full screen window */
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.your_activity);
/* adapt the image to the size of the display */
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
getResources(),R.drawable.background),size.x,size.y,true);
/* fill the background ImageView with the resized image */
ImageView iv_background = (ImageView) findViewById(R.id.iv_background);
iv_background.setImageBitmap(bmp);
No cropping, no many different sized images.
Hope it helps!
You should put the various size images into the followings folder
for more detail visit this link
ldpi
mdpi
hdpi
xhdpi
xxhdpi
and use RelativeLayout or LinearLayout background instead of using ImageView as follwoing example
<?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:orientation="vertical"
android:background="#drawable/your_image">
</RelativeLayout>
It's been a while since this was posted, but this helped me.
You can use nested layouts. Start with a RelativeLayout, and place your ImageView in that.
Set height and width to match_parent to fill the screen.
Set scaleType="centreCrop" so the image fits the screen and doesn't stretch.
Then you can put in any other layouts as you normally would, like the LinearLayout below.
You can use android:alpha to set the transparency of the image.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/image"
android:alpha="0.6"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There"/>
</LinearLayout>
</RelativeLayout>
What about
android:background="#drawable/your_image"
on the main layout of your activity?
This way you can also have different images for different screen densities by placing them in the appropriate res/drawable-**dpi folders.
If you want your image to show BEHIND a transparent Action Bar, put the following into your Theme's style definition:
<item name="android:windowActionBarOverlay">true</item>
Enjoy!
use this
android:background="#drawable/your_image"
in your activity very first linear or relative layout.
In lines with the answer of NoToast, you would need to have multiple versions of "your_image" in your res/drawable-ldpi,mdpi, hdpi, x-hdpi (for xtra large screens), remove match_parent and keep android: adjustViewBounds="true"
Add android:background="#drawable/your_image" inside your Relativelayout/Linearlayout
Worked.
If you have bg.png as your background image then simply:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"/>
</RelativeLayout>
Working. you should tryout this:
android:src="#drawable/img"
three step for put background
1)you should choose your like picture. for example :enter image description here
2)Then you copy this picture in drawable.
warning: you should choose types short for name picture.
enter image description here
3)you go to page xml Intended and write :
android:background="id picture"
for example my picture id is #drawable/download.
enter image description here
finish.
The easiest way:
Step 1: Open AndroidManifest.xml file
You can see the file here!
Step 2: Locate android:theme="#style/AppTheme" >
Step 3: Change to android:theme="#style/Theme.AppCompat.NoActionBar" >
Step 4: Then Add ImageView & Image
Step 4: That's it!
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.
I'm having trouble getting an image to scale and center properly in an ImageView.
When up-scaling, there are no problems, but when down-scaling, the image no longer appears to be centred, and the bounds of the ImageView are wrong, like this:
problem.png
<?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">
<ImageView
android:src="#drawable/stereomood_splash"
android:scaleType="fitCenter"
android:id="#+id/imageView1"
android:layout_width="100dip"
android:layout_height="200dip"></ImageView>
</LinearLayout>
Is this a bug, and has anyone found a workaround?
You should set android:adjustViewBounds="true" as in this answer.
You also need to set either android:layout_width or android:layout_height to be wrap_content, otherwise I believe your hard-coded bounds will override any view bounds adjustments.
Edit: Put this here as it's relevant to those encountering the same symptoms.
This problem is only present in the XML editor - on a real phone or emulator the image is sized and positioned correctly. Depending on the particular image and ImageView dimensions, the editor preview will show the image incorrectly resized and/or out of the view bounds.