I have a gradient view which animates from left to right. I have a XML that describes the circle inside, but the borders of XML are actually rectangular as you can see, how can I make the overflow hidden of the outside of the XML.
It looks like that only the background is a circle but not the the shape itself ,I thought that the solution is by using PorterDuff.Mode but it doesn't help.
this is my circle.xml file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="2dp"
android:color="#android:color/white" />
<corners android:radius="7.5dp" />
<size android:width="327dp" android:height="211.5dp" />
</shape>
And in my layout i use it like this :
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="327dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
i don't want the scanner to go outside the borders
You need to set square dimensions for it to appear as a circle.
Your width is larger than your height. Try this
<RelativeLayout
android:id="#+id/white_rectangle"
android:layout_width="211.5dp"
android:layout_height="211.5dp"
android:layout_gravity="center"
android:clipChildren="true"
android:adjustViewBounds="true"
android:background="#drawable/circle"
>
<View
android:id="#+id/scanner"
android:layout_width="123.5dp"
android:layout_height="123.5dp"
android:layout_gravity="center"
android:background="#drawable/scanner"
android:visibility="gone" />
</RelativeLayout>
The solution for me was to use CardView and give it cardCornerRadius.
found it here How to make a view in android with rounded corners
try this library. Use same height and width for view
https://github.com/hdodenhof/CircleImageView
Related
How can I make an EditText with a drop-down in Android?
For reference i have added an image. I am trying to combine an edittext with spinner but I am not able to achieve the same.
Can anyone add suggestion or xml mockup?
Well, You can use a LinearLayout with a horizontal orientation and create a custom drawable for the rounded border.
Implementation
First, you need to draw the rounded border by creating a new Drawable resource file.
In your res/drawables folder create rounded_border.xml
rounded_border.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="4dp" />
<stroke android:width="2dp" android:color="#2E2E2E" />
</shape>
</item>
</selector>
Then, you can add the LinearLayout to your layout as follows.
your_layout.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#drawable/rounded_border"
android:orientation="horizontal">
<EditText
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#null"
android:paddingHorizontal="16dp"
android:text="(504) 596-3245"
android:layout_weight="2"/>
<View
android:layout_width="2dp"
android:background="#2E2E2E"
android:layout_height="match_parent"/>
<Spinner
android:layout_width="0dp"
android:layout_height="match_parent"
tools:listitem="#layout/spinner_item_text"
android:layout_weight="1"/>
</LinearLayout>
Remarks
You can Customize the border width, color, and radius from the rounded_border.xml.
Final Results
could refer this lib demo : https://github.com/z2wenfa/SpinnerEditText
could use the lib directly
dependencies {
compile 'com.github.z2wenfa:SpinnerEditText:1.0.1'
}
or could refer: https://github.com/z2wenfa/SpinnerEditText/blob/master/spinneredittext-lib/src/main/java/com/z2wenfa/spinneredittext/SpinnerEditText.java
I'm using an image view with its source pointing to a shape drawable for the background for my app. I want it to look like this which is in the layout preview (blue on the top, orange on the bottom).
However, whenever I run it, it looks like this. It's basically the portrait version, so it isn't being rotated. I use android:screenOrientation="landscape" in activity tag in the manifest to force it into landscape.
This is the code for the image view in my layout, as well as the src
<ImageView
android:id="#+id/background"
android:layout_width="0dp"
android:layout_height="0dp"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/background" />
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#color/light_blue_background"
android:endColor="#color/orange_background"
android:startColor="#color/teal_background" />
<size android:height="16dp" />
<size android:width="32dp" />
</shape>
I've tried to use the rotate attribute in the layout to set it to 90 degrees, but it doesn't come out quite as well.
Anyone know how to try to get my background looking like it's supposed to in the preview?
<ImageView
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:importantForAccessibility="no"
android:scaleType="centerCrop"
android:background="#drawable/background" />
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:centerColor="#color/light_blue_background"
android:endColor="#color/orange_background"
android:startColor="#color/teal_background" />
</shape>
Had to manually adjust the gradient angle in the shape drawable XML itself. Set angle to 90, swap start and end gradient colours.
I need to create a stack of overlapping ImageViews in Android like Spider Solitaire in Windows. Also, I need to be able to dynamically add more ImageViews. Please suggest methods to do so. Can I use a LinearLayout (Its not necessary. Whatever works is fine)? If so, how do I accomplish the overlap?
Note: All the ImageViews will have the same amount of overlap, so no need to worry about the unopened cards in the give screenshot.
Thank You
User LayerList
A LayerDrawable is a drawable object that manages an array of other drawables.
Each drawable in the list is drawn in the order of the list—the last drawable in the list is drawn on top.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" >
<bitmap android:src="#drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" >
<bitmap android:src="#drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
Note : the below images uses item attributes as android:left="10dp", android:left="20dp".
You can use FrameLayout. Following layout is an example with 3 ImageViews:
<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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="300dp"
android:background="#f00" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:background="#0f0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:background="#00f" />
</FrameLayout>
I don't think you can achieve this using Linearlayout only.
But you can use LayerList
A LayerDrawable is a drawable object that manages an array of other
drawables. Each drawable in the list is drawn in the order of the
list—the last drawable in the list is drawn on top.
with help of LayerList you can achieve the following:
Syntax
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="#[package:]drawable/drawable_resource"
android:id="#[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension" />
</layer-list>
Is it possible to specify border in Android button in the main.xml?
[p.s.
without the 'separate xml file containing stroke tag' but in the original file where I define the button and also
without the 'dynamically by programming' solution
and 'images' solution]
<Button
android:background="#FFFFFF"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:text="Player 3"
android:layout_x="0px"
android:layout_y="0px"
android:id="#+id/p3"
android:layout_weight="1"
/>
here I am changing the background dynamically but the problem is, for 2 buttons there is no border.
Try to use shape
my_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:radius="0.1dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#E8E6E7" />
</shape>
And Button
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_shape"
android:text="Button" />
Screenshot
I hope this will help you.
This is not the recommended way to do it because it causes overdraw and adds unnecessary views, Gunaseelan has the proper method.
There's no concept of borders as an attribute. The accepted way is to use a separate drawable as the background to the View (using stroke as you've mentioned and as #gunaseelan writes).
The other (not recommended) way is to enclose your Button in another View like a LinearLayout, set the background color to the desired border colour on the encapsulating View and padding on the outer View too.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#342334"
android:padding="5dp"
>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="whatwhaaat"
/>
</LinearLayout>
The value of the padding will indicate the thickness of the border. This method is not recommended as you end up with an extra View in your layout, and another layer of overdraw (it draws the LinearLayout then the Button on top).
In my application, I want to display videoview as a rounded corners. I have tried placing videoview/surfaceview inside linearlayout with rounded corner set to linearlayout. but it does not work perfectly. I can not set rounded corner to videoview/surfaceview. I want to set view as below image:
Anyone have idea how to do this?
Not sure why there are so many bad answers when the solution is really simple (as long as you're min sdk > 21). Creating a shape and putting it overtop of the video won't work because you obviously want the background to be transparent to see the views behind it.
I found the answer here Android View Clipping. You just put the video view in a frame layout, add a rounded background to the frame layout, add an outline provider and clip the frame layout to the outline.
The background rounded_video_background:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#000000"/>
<corners android:radius="16dp" />
</shape>
The frame layout and video view inside of it:
<FrameLayout
android:id="#+id/video_view_container"
android:layout_width="90dp"
android:layout_height="120dp"
android:background="#drawable/rounded_video_background"
android:outlineProvider="background">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</FrameLayout>
And the final step is clipping to the outline (didn't see a way to do it in xml so I did it programatically):
video_view_container.clipToOutline = true
Its worked for me,
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_width="match_parent"
card_view:cardCornerRadius="25dp"
android:layout_height="wrap_content">
<VideoView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="215dp" />
</android.support.v7.widget.CardView>
You can make it using a FramLayout & an XML drawable
FramLayout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<VideoView
android:layout_width="match_parent"
android:layout_height="#dimen/dp_240"
android:layout_margin="#dimen/dp_24"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/rounded_corner_video_bg" />
</FrameLayout>
XML Drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="#dimen/dp_24"
android:color="#color/md_white_1000" />
<corners android:radius="#dimen/dp_24" />
</shape>
You could try layering different views on top of each other to create the rounded corners you are looking for. Try placing four ImageViews over the VideoView in each corner, to achieve the desired rounded corners. I have had success using a RelativeLayout to accomplish this, but you could also try using a FrameLayout to hold the Views together.
It is directly not possible, but you can do this by draw a imageview on top of videoview and set an image having transparent from between and solid color in rounded shape on the corners.
check this: Click here
Its simple,
1. Create a drawable with rounded corners as mentioned by #sunil kumar
2. Set that drawable to your layout as a background
3. When using that layout set layout(your layout item name).clipToOutline = true
This is XML code of rounded VideoView.
<androidx.cardview.widget.CardView
android:id="#+id/videoCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="20dp"
card_view:cardBackgroundColor="#color/white">
<VideoView
android:id="#+id/relativeVideo"
android:layout_width="match_parent"
android:layout_height="225dp"
android:paddingTop="-10dp"
android:paddingBottom="-10dp" />
</androidx.cardview.widget.CardView>
Negative padding is important otherwise height of VideoView is smaller than cardview by half of cornerRadius in both top and bottom side. You can set height whatever you want but negative padding should be half of cardCornerRadius all the time. Purple in the image is a video preview, not related to xml.
Have a nice day!
put rounded.xml in your drawable folder and set on framelayout of video like android:background="#drawable/rounded.xml"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFFFF" />
<corners android:radius="7dp" />
</shape>
Create an xml file in drawable folder called shape_video.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="6dp" />
</shape>
Change the radius according to your rqmnt and in the background attribute of your videoview, give
android:background="#drawable/shape_video"