In my new android app I need to give border to imageview by using something like .9.png.
The border size should change with respect to image which I have given to imageview and If possible I need to give a background image to imageview as I'm going to apply a transparent png image to imageview.
Should I create a new custom view for this?
I think you should create a custom view. Do a LinearLayout with the background as the border and have the ImageView centered inside the LinearLayout. Use 9-patch to get a correct stretch and create a content area so that the border is showing (use draw9patch in android SDK/Tools).
Example:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/border">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
Then 9-patch your border (border.9.png in this example). You can have the same stretching as content area.
Related
I am trying to set a SVG Icon as the background of an Android ImageView.
The SVG Icon will receive some padding and colors are being changed a little.
<ImageView
android:id="#+id/passwordLabel"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#android:color/black"
android:padding="30px"
android:tint="#android:color/white"
app:srcCompat="#drawable/ic_lock"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="524dp" />
Is there a way to make the ImageView circular now?
I have tried other solutions but am confused, as they demand me to change the background.
You can't make the imageView circular, because it just displayed the image that already set.
Use CircleImageView library instead
Wrap your ImageView with CardView and make the radius half of its width/height
Change your image shape to be circle instead
1- you can use this library
https://github.com/hdodenhof/CircleImageView
2- use cardview
put imageview inside it and make radius half of width or height
(as suitable for your use case) also see this link
In the image above, the black speech bubble and the red background behind it are a single ImageView that spans the width of a vertical phone screen. The "Hello" is a TextView and the layout is relative. Using margins, I was able to position the "Hello" inside the speech bubble in my Android emulator -- but the positioning is off when I emulate a different phone.
Is there a better way to position my TextView
is there a way to make it responsive (so that the Hello is always in
the speech bubble, no matter what the device)?
Here's my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainGraphic"
android:src="#drawable/finished2"
android:layout_marginBottom="97dp"
android:layout_above="#+id/enterValue"
android:layout_alignParentStart="true" />
<TextView
android:maxWidth="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/helloText"
android:textColor="#FFFFFF"
android:layout_marginEnd="29dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="45dp" />
</RelativeLayout>
I would use a FrameLayout instead of the RelativeLayout with same size like the image.
Then use the attributes android:layout_gravity="center" and android:gravity="center" for the TextView.
Try this:
...
<TextView
android:maxWidth="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="#+id/helloText"
android:textColor="#FFFFFF"
android:layout_alignTop="#+id/mainGraphic"
android:layout_alignBottom="#+id/mainGraphic"
android:layout_alignLeft="#+id/mainGraphic"
android:layout_alignRight="#+id/mainGraphic"
android:gravity="center" />
...
Best approach is to use 9-patch image & set it as the background of the Textview. If you use a normal png file, we can't guarantee that it will align correctly on all resolutions & it can become blurred when stretched.
9-Patch image automatically resize to accommodate the contents of the view and the size of the screen. Android SDK provides a tool for creating 9-patch images which is inside SDK sdk/tools directory. See https://developer.android.com/studio/write/draw9patch.html.
You should use a 9-patch for the message shape. What you can do is make the root view of your layout as a FrameLayout and then add a TextView to it. Set the message shape Nine-patch as the background of the TextView.
For creating the 9-patch visit https://romannurik.github.io/AndroidAssetStudio/nine-patches.html by Roman Nurik. There just upload a png file of the image asset and choose the xhdpi definition for best results. The Nine-patch has 3 main properties -
Stretch regions : Defines which areas of the asset can stretch for accommodating different screen densities.
Content Padding : Defines the padding area for the content (text) that is going to appear inside the image.
Optical Bounds : Defines how much area should be optically visible around the asset.
Pros of using Nine-patches :
Scalable
Easy to use
Lightweight
Any amount of content can be put in it.
Hope this helps.
Ideally, you should be using a complete red background to the RelativeLayout, and a black speech 9patch image for the TextView.
But if you really want the RelativeLayout to have background as red with speech bubble, you have to set it at run time.
Calculate the height and width of RelativeLayout at run time say 150px and 300px respectively.
Carefully look at the background image, and determine the edge points of bubble from top, right, bottom and left. For example, if image height is 100px by 200px, and bubble top starts at 30px and bottom ends at 70px, right edge starts at 150px and left ends at 250px. Also calculate the width and height of TextView.
At run time, change the position of TextView based on the above figures.
I'm using this to crop an image and then i load it to this imageView:
<RelativeLayout
android:id="#+id/activity_edit_image_relative_layout_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/activity_edit_image_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff2299"
android:scaleType="fitCenter"
android:src="#drawable/phone" />
</RelativeLayout>
the color i'm giving to the background of the image view is pink ( so it will be easy to spot) and the relative layout will act as the view group and i will add other views to it at run time.
the pink background must only appear for the images that have transparent background (png files) like this (this picture is not cropped)
but when i crop an image and load it to the image view i get this
the pink background must not appear. the issue is i'm trying to figure out where on the image is the user taping (overloading onTouch for the imageView) now when i tap on the pink area around the Alien it tells me that i'm tapping the image which is not correct. whats the issue?
Add the following property to your ImageView:
android:adjustViewBounds="true"
I have an image (picture 1) that has a transparent area around it that extends above and below the image itself (picture 2). When I add this image as an ImageButton in my xml and use wrap_content, that transparent area in picture 2 is becoming part of the button rather than just the button itself (picture 1). Picture 3 is the result. Does anyone know how to solve this?
Here's my XML:
<ImageButton
android:id="#+id/resume_button"
android:src="#drawable/disabled_resume_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/new_checklist"
android:layout_centerHorizontal="true"
/>
Do this :
<ImageButton
android:id="#+id/resume_button"
android:background="#drawable/disabled_resume_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/new_checklist"
android:layout_centerHorizontal="true"
/>
the transparent area around your image, if its part of your image, contributes to the actual width and height of the image, imagine if the transparent part of the image was a coloured area rather than a transparent area and you can visulise how it would be part of your image, even though its transparent it is taking up an area that contributes to the height and width of the image
"wrap_content" attribute just set the height of yout button to the height of yout image.
If you want to keep only the colored part of your image, just resize it! ;)
I have a textview which has to use a 9-patch drawable as the background. But the 9-patch drawable has left and right paddings which make the background image not stretch properly to cover the whole text. I tried resetting the paddings for the textview itself but it doesn't fix the problem.
Would anyone have any idea how to make it work?
Thanks.
The black lines on the left and top defines the stretchable are, and those on the right and bottom marks the "content" area in a 9-patch image.
So, if you don't want padding means you want a full-length content area. You should mark full-width content area by drawing a full-length line at the bottom and right of the 9patch image.
In this image, the black lines on the right and bottom represent the content area. You can see the preview on the right side, and notice the content area in light blue color. You can fill the content area by extending the bottom and right lines.
Editing 9-patch file is not a good idea, because this method may deform the background image.
I used a trick to handle this:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<View
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView"
android:layout_alignTop="#+id/textView"
android:background="#drawable/your_9_patch_image"/>
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="a custom text!"/>
</RelativeLayout>
I have set the 9-patch background drawable for a View behind my TextView in a RelativeLayout. So the there is no unwanted padding :)
Theoretically you cant really change the padding of 9 patch image programatically.
So you have two options:
1) Have several 9 patch images in your drawable folder for each resolution: drawable-hdpi, drawable-xhdpi etc
2) Embed a inner layout:
<RelativeLayout
android:background="#drawable/nine_patch_image_without_padding"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_margin="10dp" // Your padding goes here
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="How you doing"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
It worked for me. I use the second options as it is simpler and quicker to use :)