In Android UI design using XML, how is it possible to put an ImageButton to align exactly with the background of the activity xml file.
Suppose, I have two images, one acts as the background image for the activity, and the second one acts as the image button source.
This is the background image.
http://i.stack.imgur.com/e1Ow5.png
This is the button image. http://i.stack.imgur.com/m0tUU.png
I will set the first image as background to the activity. My question is how will I properly place and align the second image,that is the button to be exactly inside the "central rectangle" of the background. The "central rectangle" is the place holder, and it can be anywhere in the screen.
Not: I tried using relative layout, but, couldn't really place the button depending on the background.
Edit:-
Actually the rectangle and the rounded-rectangle at the center of the background is just a place-holder. It can be anything, even nothing. Or it can be anywhere. It might not be at the center. Consider the whole image, I need to put the button image where the place-holder is. That is my intention. Say for example , consider a radio application, where there is a turning button acting as volume rocker. Everything else in the image is the background, and the volume rocker might be a different image.
The answer is its impossible the way your are trying to do it.
What you want to do is cut the background up further so the button is centered.
So using your images as an example, here is how the view hierarchy would look
->FrameLayout1
---->Framelayout2
-------->Button
FrameLayout1 would be the background without the inner square
FrameLayout2 would be the inner square
Button would the button asset and placed centered in FrameLayout 2.
There are other techniques on top of this you will need to make it look pixel perfect, like using 9 patch drawables.
Considering you are trying out in landscape mode( your images seems so)
You can try the following code.
bg : your background
img: your center 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"
android:background="#drawable/bg" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img" />
</RelativeLayout>
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/img_bg" />
</RelativeLayout>
drawable/img_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/bg">
</item>
<item android:drawable="#drawable/fr">
</item>
</layer-list>
or
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/bg" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageView1"
android:src="#drawable/fr" />
</RelativeLayout>
bg.png and fr.png are transparent image with same height and width.
Related
I just want to make a simple image view but when I run that app the unnecessary white background comes. I don't want to need that part. any solution.
and here is my xml file for that image view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:src="#drawable/pot1"/>
</LinearLayout>
In code use -
imgview.setScaleType(ScaleType.FIT_XY);
Or in xml Image view -
android:scaleType="fitXY"
remove the gravity attribute if you want to make it stick to the top of screen or add scaleType attribute fitCenter, cropCenter or fitXY if you want to make it fill the screen.
I want to set the logo of my app to the bottom left hand side of the screen and be in the background. the reason i want to do this is to allow the users to modify the background colors which would not be possible if i simply set the entire activity or layout background to an image to give same impression.
Another way of asking this is how do i set an image to bottom left hand corner and below other controls?
i don't know if i understand correct. but what you ask is pretty simple.
<FrameLayout 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="#bbbbbb"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="bottom|left"
android:layout_margin="10dp"
android:src="#ffff00" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00000000"
android:orientation="vertical">
<!-- Put your views here -->
</LinearLayout>
</FrameLayout>
you can change the frame layout's background as you wish and keep logo on bottom left corner.
Also you can set your LinearLayout (which you can use any layout. it is up to you) background to transparent so you will see background. and any extra view inside the linear layout (your view container) will be on top always.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_desired_full_background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher_logo"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
I have a RelativeLayout in my Android project. This has it's background set to a Bitmap:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/splash2" />
splash2 is a PNG image sized 2560x1440 pixels. I haven't set the background of the layout directly to the image, because the default scale mode (or gravity) is fill which stretches the image to fit the screen. With center it should take correct size image from the center and show it unscaled. In case of a vertical 1080x1920 screen, it should take that big piece and center it in the layout.
However, I have a problem. The image is bigger than any screen out in the market today. Still, with my Nexus 7, which has a 1920x1080 screen, it has borders around the image. The layout is set to full screen. The image is shrinked vertically.
How do I fix this?
You can use android:gravity="fill" to cover vertical & horizontal direction
For splash image try gravity="center|fill"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black" />
<item>
<bitmap
android:gravity="center|fill"
android:src="#drawable/fondo" />
</item>
</layer-list>
ImageView's scale type centerCrop was what I wanted. Unfortunately I couldn't specify this property for bitmaps. I changed my splash screen layout to FrameLayout and added an ImageView and TextView overlapping each other. This way I was able to achieve what I wanted.
<FrameLayout 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"
tools:context=".SplashScreen"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash2"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/roadSignName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#FFF"
android:gravity="center_vertical|center_horizontal"
android:padding="10dp"
android:text="My program"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="40sp" />
</RelativeLayout>
</FrameLayout>
Try adding this to the bitmap:
android:gravity="fill_vertical"
This should fix it.
P.S. Sorry for the first answer, I edited it now.
So I have a list of images that come from the web, I don't know which color are they and I want to place a text over the ImageView.
My idea is to place the ImageView, an image overlay with transparency gradient over that ImageView and the text above it. I want to mimic this behaviour:
Is there anyway to do this via XML?
When you write the XML for your list items which get inflated in the getView(...) of whatever ListAdapter you've written you can surely do this.
Something like this for the list item:
<?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">
<ImageView
android:layout_width="320dp"
android:layout_height="240dp"
android:background="#ACACAC"/>
<RelativeLayout
android:layout_width="320dp"
android:layout_height="240dp"
android:background="#drawable/gradient">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Here is your text"
android:textColor="#000000"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
Then you create that drawable/gradient. For that you can recycle the answer from here.
Thanks to adityajones I managed to get there :)
So although this is my right answer, I'll mark his as the correct one!
<?xml version="1.0" encoding="utf-8"?>
<ImageView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/image" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_image" >
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="6dp"
android:layout_marginBottom="18dp"
android:shadowColor="#000"
android:shadowRadius="7.0"
android:text="This is some random text"
android:textColor="#color/white"
android:textSize="22sp" />
</RelativeLayout>
I'd use a FrameLayout or RelativeLayout. The first View you add to either should be the background ImageView, then obviously you'll need some TextViews and Other ImageViews [or Buttons, or ImageButtons, etc]
Seems like a reasonable layout: a background image, and then one additional view in each corner.
For the gradient, you'll probably want a separate Layout/View at the bottom with a gradient drawable as the background, although I can imagine you might be able to get away with setting the background of one of your TextViews as the gradient.
You do not have to use a gradient drawable file or set it in your xml..
you can do this pragmatically using GradientDrawable Class as explained in this related Question (Create a radial gradient programmatically) then set it as a background for a layout that covers your ImageView, this gives you ability to use different colors and orientations
I created a simple 9-patch image for Android. Poured into a folder drawable-mpdi. Everything works, but the upper frame is lost. It is not clear why. See screenshot:
This is my 9-patch file:
https://dl.dropbox.com/u/30086473/input_login_active2.9.png
My XML layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/white">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#drawable/login_bg"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/auth_logo"/>
<EditText
android:layout_width="273dip"
android:id="#+id/login"
android:layout_marginTop="20dip"
android:layout_height="wrap_content"
android:background="#drawable/auth_edit_text"
android:hint="#string/auth_login"/>
<EditText
android:layout_width="273dip"
android:id="#+id/password"
android:layout_height="wrap_content"
android:background="#drawable/auth_edit_text_left"
android:hint="#string/auth_login_left"
android:layout_marginLeft="17dip"/>
</LinearLayout>
</LinearLayout>
What is the problem? How to recycle 9-patch, that there was a blue frame at the upper of element?
There is #drawable/auth_edit_text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/t" />
</selector>
Your pixel borders seem to be incorrect (reversed maybe). Try this instead:
With your original, you'd be stretching the phone icon as well, not just the bubble. Also, you had the content area and stretch area reversed for the top and bottom (I've inverted them in the attachment). While I don't know that that would specifically cause the problem you've shown, I would give it a try and see what happens.
EDIT: Try this:
EDITED ANSWER FROM COMMENTS: I think that may be your issue is that it's looking for the ldpi image, not finding one, and falling back to the default and scaling it down. Since the border is only 1 px, it simply disappears when scaled down.