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?
Related
I have to get this type of card design with semi-circle on both side of cardview along with shadows on both card view and semi-circles.
Thanks in advance.
I found a solution here that create a custom view called TicketView.
It provide various option to set the radius of the arc and divider options.
You can use it as custom view.
<com.vipulasri.ticketview.TicketView
android:layout_width="match_parent"
android:layout_height="160dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:id="#+id/ticketView"
app:orientation="vertical"
app:scallopRadius="10dp"
app:showBorder="false"
app:scallopPositionPercent="50"
app:showDivider="true"
app:dividerType="dash"
app:ticketElevation="14dp"/>
It's not possible using Elevation API. A shadow is generated using view's ViewOutlineProvider with a convex path outlining the view. Your view is not convex, so you cannot make a proper ViewOutlineProvider, so you cannot get a nice, generated shadow that way.
The easiest way is to use a 9-patch with these cutouts drawn on it.
Why all my controls(buttons, textfields) are all in upper left of my emulator when I test run it?
When you first add widgets to your App, it automatically goes to the Top-Left of the screen. Try adding some Constraints to your widgets.
To make a View position in the center of your App, add these lines of code to the bottom of a widget in your XML:
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
Example:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
/>
It Looks like you may be using a constraintLayout. Make sure that in your layout you have a vertical and horizontal constraint set on each control.
Your views are not setup up correctly in your layout. First of all, if you could give the code for your layout (activity_main.xml or something similar) it will be easier to solve your problem.
The likely culprit is either that you are using a RelativeLayout and haven't set the view to be in relative position to another or, more likely, that you are using a ConstraintLayout and haven't linked your views correctly.
If you are using constraint layout the easiest way to link your views is from the Design tab, looking at the UI of your app. Click your TextView/EditText/etc and drag from the little squares that appear on the sides to literally link to either the side of the screen or to another view.
There is some closeable view in my app and it has a close button in header. I want make this button borderless and small.The code:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:layout_margin="0dp"
android:padding="0dp"
android:src="#android:drawable/ic_menu_close_clear_cancel"/>
In result the button is borderless but has much empty place around cross image (button is hitted on screenshot to make empty space visible)
How could I fix it?
You should use:
android:background="?selectableItemBackgroundBorderless"
Added
android:minHeight="0dp"
android:minWidth="0dp"
android:padding="2dp"
The result
Also I'll use negative margins to place the button closer to corner.
You can use the following on your ImageButton to remove the 'border':
android:background="#android:color/transparent"
or
android:background="#null"
If you would like change the background when the user clicks the button you can create a selector. Example ImageButton doesn't highlight on click with Transparent background.
With the introduction of ConstraintLayout, you can use constraints to reduce the width of the borderless ImageButton, that's way too wide by default, by constraining it to be as wide as the height:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_closeable_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
style="?android:attr/borderlessButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:contentDescription="#string/action_close"
android:src="#android:drawable/ic_menu_close_clear_cancel"
app:layout_constraintDimensionRatio="w,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
I don't recommend you to make it any smaller than this because the touch target needs to be at least 48dp x 48dp, as for Google's Material Design guidelines, otherwise a user would have a hard time trying to touch it.
If you really need it to look like it's closer to the edges of the view, you can always use an image that's not centered (has transparent padding on one or two sides), but I'd try my best to avoid doing this workaround and try rethinking my app's design in order to accommodate the button as it is.
use textView for that..
and use textview.OnTouchListener
finally onTouch change the color of text.. thats it
or else in your code just use
android:background="#null"
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
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"