Problem Description:
I have an ImageView which changes in size due to changing heights of different devices.
Aim:
I am looking to keep the bottom edge of the drawable in line with the bottom of the image view and crop the rest from the top.
What I've Done:
I've tried using centerCrop but it only crops from top and bottom:
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/llHomeScreenButtonBar"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/bg_home" />
I've also tried copying the Extended ImageView class from: Chris Arriola
However, I've encountered an error from eclipse saying:
Custom view CustomImageView is not using the 2- or 3-argument View constructors; XML attributes will not work
The XML code fragment looks like this:
<com.misc.CustomImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/llHomeScreenButtonBar"
android:layout_alignParentTop="true"
android:src="#drawable/bg_home" />
What's the best way to achieve this?
Thanks!
Implement the 2- and 3-argument constructors.
The one-argument constructor public CustomImageView(Context) can't be used by the XML inflation process.. XML inflation uses one of the other two constructors, with one or two more arguments.
see https://stackoverflow.com/a/9195858/357951
Related
I have a CardView aligned to the top of the screen. Inside the CardView, there is an ImageView and I just want to adjust the height of the right-hand side of the CardView just like in the image below. Is this possible?
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintWidth_percent="1"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:cardElevation="10dp"
app:cardCornerRadius="0dp">
<ImageView
android:id="#+id/bngCover"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="#drawable/explore2"
android:scaleType="centerCrop"/>
</androidx.cardview.widget.CardView>
Asides the radius, CardViews are usually rectangular in nature; therefore, this cannot be easily achieved with a CardView.
Best advice would be to create your own resource file. This way, you can define how exactly you want your view to look and still give it the properties you want (elevation, radius, etc).
To learn how to create a drawable resource file, check this tutorial out. You can also read up on the Android documentation on ShapeDrawables here.
CardView doesn't have the property to change on side height only. In this case, you must create a custom trapezoid shape instead, like this:
How to build trapezoid shape in xml android?
I'm using a library named AHBottomNavigation and what I want to achieve in my layout is add an ImageView on top of the bottom bar.
So I wrapped the bottomBar inside a FrameLayout and added an ImageView :
<FrameLayout
android:layout_width="match_parent" android:layout_height="wrap_content">
<com.aurelhubert.ahbottomnavigation.AHBottomNavigation
android:id="#+id/bb_bottom_bar_navigation"
android:layout_width="match_parent" android:layout_height="wrap_content"/>
<ImageView
android:src="#drawable/icon"
android:layout_gravity="center" android:contentDescription="Take Photo"
android:layout_width="32dp" android:layout_height="32dp" />
</FrameLayout>
and while it seems to work (and I can confirm that on three devices I have already tested I can see the image) when I test it on devices that run android 5.0 or later the image is not there!
I have tried different pngs, adding background color to the ImageView but still no result.
If I use uiautomatorView I can see the View is in the position I want it and takes the space I have already defined in the xml.
Obviously there's something that intercepts the view from drawing, but I don;t know such a mechanism on android. I tried digging through the code of the library but don't knowing what I'm searching for didn't help.
Any ideas ? (cause I'm out of ideas)
I'm drawing an ImageView (a 50% transparent icon) over an ImageButton, but the icon disappears when I click the ImageButton (an Activity transition gets triggered at this moment). That looks pretty awful...
What is the best approach to do this, without actually putting the icon into the src-File of the ImageButton? Is there a way to do it with setting drawables as background or foreground? And what exactly are those two properties for? I can't find any documentation about android:foreground...
BTW: I'm using the new Lollipop shared element transition and testing on a nexus 9.
<Relative Layout...>
<ImageButton
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_width="400dp"
android:layout_height="250dp"
android:src="#drawable/call1"
android:foreground="#drawable/phone" <!-- Does this make any sense?-->
android:transitionName="#string/trans_call_pic"
android:onClick="clickCall"
/>
<ImageView
android:id="#+id/imageButton1"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/phone"
android:layout_alignBottom="#id/imageView1"
android:alpha="0.5"
android:layout_marginLeft="14dp"
/>
</RelativeLayout
The android:foreground attribute is for a foreground selector (added to a FrameLayout). It's not intended to simply draw an image.
One solution would be to wrap the button & the image in a container view and put your transition on the container. This way the two child views would be treated as one.
Try adding a foreground selector to your view. You can extend ImageButton, override onDraw(), and draw the foreground selector after calling super.onDraw(). This will ensure that the foreground selector is drawn on top of the entire ImageButton. More detailed instructions can be found here.
I'm trying to achieve the following layout: a fixed width TextView aligned to the left of its parent, with the text inside it aligned to the right side of that TextView (that's why fixed width, can it be done other way?) and the rest of the parent is filled with a drawable (simple line). Like this:
It's a ListView containing 2 types of rows and the layout for the rows with lines is quite trivial - LinearLayout with TextView and ImageView (I can post the exact code later if needed). And I'm getting a warning that it could be replaced with a single TextView with compound drawable.
I'm all for optimization so I really tried to follow that advice. Unfortunately I wasn't able to get the same result - the line is either constrained to TextView's width or text is aligned to the right side of the ListItem, now to fixed position.
Am I missing something?
Edit: Apparently it is not actually possible and since there are some other complications (the drawable is now a level-list drawable, which is not always a line and sometimes it has a non-fixed height that I have to set) I will leave it as it is now - linear layout, containing one TextView and one ImageView.
I don't think that you're missing anything. The TextView compound drawable features are not very customizable and in general are not worth the time you spend trying to get them to look right. Some lint warnings are a little overzealous and premature.
The optimization that the lint refers to is something that is better attributed for a fixed size image. In your case, the line has to stretch the rest of the screen length and as such it is not something that can be done with a textview with compound drawable. This kind of lint warning is more of a suggestion rather than something that MUST be done and is detected by just checking for a linear layout with only a textview and an imageview rather than checking what would need to go in the image view. If you already have it working the way you did it I think you should leave it alone.
Your view create from this -
<?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="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/time"
android:layout_width="#dimen/today_time_width"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="right"
android:layout_marginRight="5dp" />
<ImageView
android:layout_gravity="center"
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="#dimen/today_current"
android:src="?attr/item_boundary" />
</LinearLayout>
There is no way to achive this using only standart TextView. If you really want to reduce view count you can create your custom TextView class, set layoutWidth to matchParent and draw line from text end to right border. But it's not worth to be doing. Some extra views won't slow your list.
I am not sure if you will be able to achieve what you really want to , but then you could change the linear layout in the link you posted to something like this:
<RelativeLayout
android:id="#+id/relTrial"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtTime"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:gravity="right"
android:text="12:45 AM"
android:layout_alignParentLeft="true"/>
<LinearLayout
android:id="#+id/lnrSep"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:gravity="bottom"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_centerVertical="true"
android:orientation="horizontal"
android:background="#android:color/darker_gray"
android:layout_toRightOf="#+id/txtTime"
android:layout_alignParentRight="true"></LinearLayout>
</RelativeLayout>
This way the time text will be right aligned although being at the left side, and the line will also be visible.
Hope that helps.
If I got you right, you want to add bottom border to list view item?
What about to try this:
android:drawableBottom="#drawable/line"
I am trying to have an image be fitted, and have a layout below it with some black background and whit text. My problem is that the layout ends up leaving space between the image and the text itself, and I don't understand why:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_below="#+id/image" >
<TextView
style="#style/text_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Couple more elements -->
</RelativeLayout>
</RelativeLayout>
I would want this second RelativeLayout of 15dp touch the bottom of the image, but unless I change the image height to something small, it leaves some space. This layout specifies how to display an image + some text below it, but I have a total of 4 images that use this layout to get loaded on the screen, in a 2x2 display. (Each image takes 25% of the screen).
Any idea how to make the RelativeLayout align exactly with the bottom of the image please?
I do not fully understand your question though I think you might have a look at the launcher layout for my Newspaper Puzzles app...
http://code.google.com/p/newspaper-puzzles/source/browse/np/res/layout/launcher_layout.xml
or perhaps from the Open Sudoku Game look at the number pad layout found here:
http://code.google.com/p/newspaper-puzzles/source/browse/np/res/layout/s_im_numpad.xml
Use the ADT tools to get the right layout is probably best if possible but I know sometimes it is difficult to use to get specific results I still recommend using the xml tools included in the Android Development Tools.
http://developer.android.com/tools/help/adt.html#graphical-editor
I would recomend using a compound drawable if you're trying to put text directly below an ImageView.
See the following question for more details: How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView