My problem is there isn't any rounded corner at the left bottom region of the rectangular, even though I defined it as 10dp.
Is there a better way to put a Drawable next to the EditText, rather than using my silly approach ?
Or how can my approach be redefined ?
Excerpt from the layout:
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#drawable/email_bordered"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="34dp"
android:textSize="30sp"
android:layout_marginBottom="20dp"
/><EditText
The email_bordered.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This controls the white background-->
<item
android:width="32dp"
android:height="90dp"
android:left="1.9dp"
android:bottom="1.9dp"
>
<shape android:shape="rectangle" >
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
/>
<solid android:color="#fff" />
</shape>
</item>
<!-- This adds the vector image -->
<item
android:bottom="50dp"
android:top="10dp"
android:width="24dp"
android:height="24dp"
android:left="5dp"
android:drawable="#drawable/ic_if_mail_461377"
android:gravity="left">
</item>
<!-- This draws the border around everything -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#ffffff"/>
<corners
android:radius="10dp"
/>
</shape>
</item>
</layer-list>
And this is the preview:
You can use the drawableLeft tag within the EditText to get the desired design.
<EditText
...
android:drawableLeft="#drawable/ic_if_mail_461377"
android:drawablePadding="10dp"/>
EDIT
With a few changes in the layer-list, the final outcome is something like this.
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="45dp"
android:drawableLeft="#drawable/pic"
android:drawablePadding="10dp"
android:background="#drawable/email_bordered"
android:ems="10"
android:inputType="textEmailAddress"
android:paddingLeft="4dp"
android:textSize="30sp"
/>
email_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This controls the white background-->
<item
android:width="62dp"
android:height="45dp"
android:left="1.9dp"
android:bottom="1.9dp"
>
<shape android:shape="rectangle" >
<corners
android:topLeftRadius="10dp"
android:bottomLeftRadius="10dp"
android:topRightRadius="0dp"
android:bottomRightRadius="0dp"
/>
<solid android:color="#fff" />
</shape>
</item>
<!-- This adds the vector image -->
<!-- This draws the border around everything -->
<item>
<shape android:shape="rectangle">
<stroke
android:width="1.9dp"
android:color="#ffffff"/>
<corners
android:radius="10dp"
/>
</shape>
</item>
</layer-list>
Related
I'm trying to set a transparent circle with a stroke as the background of a TextView in android:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/decibel_circle"
android:text="30 dB"
android:fontFamily="sans-serif"
android:textColor="#android:color/black"
android:textSize="70dp" />
</RelativeLayout>
And this is the shape I want to use, located in decibel-circle.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="5dp"
android:color="#F44336">
</stroke>
<solid android:color="#android:color/transparent"/>
<size
android:width="36dp"
android:height="36dp" />
<corners android:radius="12dp" />
</shape>
</selector>
The problem is that the shape is never shown, neither in the preview nor when I run my app.
What am I doing wrong?
in your decibel-circle.xml remove selector just use shape alone
Use this only
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="5dp"
android:color="#F44336">
</stroke>
<solid android:color="#android:color/transparent"/>
<size
android:width="36dp"
android:height="36dp" />
<corners android:radius="12dp" />
</shape>
I want to add shadow to my text view . I found solutions for adding gradients and one other solution which suggested creating a duplicate text view with a lighter shade at the back of the original textView to give a shadow effect. But i want to have a real shadow effect.
This is what i tried .I created an xml file rounded_corner.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#33000000"
android:dashWidth="3dp"
/>
<solid android:color="#000000" />
<padding
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<corners android:radius="10dp"
/>
</shape>
I added elevation and translationz but its not showing shadow in the output
<TextView
android:background="#drawable/rounded_corners"
android:layout_width="260dp"
android:layout_height="70dp"
android:textSize="50sp"
android:text="Welcome User"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp"
android:textColor="#000000"
android:elevation="50dp"
android:translationZ="10dp"
android:id="#+id/welcome"/>
I assume that you want to put the shadow on the TextView and not on the text itself. The you can make a layer-list inside your rounded corner drawable file like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/black" />
<corners android:radius="3dp" />
</shape>
</item>
<item android:bottom="2dp" android:right="2dp">
<shape android:shape="rectangle">
<solid android:color="#282828" />
<stroke android:width="1dp" android:color="#302f2f" />
</shape>
</item>
</layer-list>
If you put the drawable as background of your TextView
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_corner"
the output will link like this:
Just pu this on your TextView XML for shadow on the text:
android:shadowColor="#android:color/holo_purple"
android:shadowDx="10"
android:shadowDy="10"
android:shadowRadius="5"
you can edit with your preferences.
And create a Drawable file for shadow in all input, more or less like this one:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#d8d8d8" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="4px" android:left="0px" android:right="4px" android:top="0px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
You can use the shadow XML attributes.
android:shadowColor="#000"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="50"
For more info check this link, https://developer.android.com/reference/android/widget/TextView.html#attr_android:shadowColor
I have a simple background in my drawable folder, just a border that I use on my EditText :
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners
android:radius="2dp"
/>
<solid android:color="#ffffff"
/>
<stroke
android:width="1dip"
android:color="#000" />
</shape>
But I want to achieve this.
Top rounded and the bottom rectangle in the edges! I'm having trouble and do this
Create one Drawable shape xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dip"
android:color="#color/colorAccent"/>
<corners android:radius="10dp"
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"/>
</shape>
Set that drawable in you EditText background
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/testing"/>
create
round_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/white"/>
<corners android:bottomRightRadius="0dp"
android:bottomLeftRadius="0dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp"/>
</shape>
Set that drawable in you Edit Text background
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_shape"/>
Please try this one work for me
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#000" />
<corners
android:bottomLeftRadius="0dp"
android:bottomRightRadius="0dp"
android:topLeftRadius="25dp"
android:topRightRadius="25dp" />
please check and change radius.
here create editbox_custom_top_radius.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--Here Corner use for give curve to your edit text-->
<corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
<!--stroke is use to apply border on edit text-->
<stroke android:width="2dp"/>
<!--solid for incase you want background color to edit box-->
<solid android:color="#color/app_grey"/>
</shape>
create editbox_custom_bottom_radius.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--Here Corner use for give curve to your edit text-->
<corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp"/>
<!--stroke is use to apply border on edit text-->
<stroke android:width="2dp"/>
<!--solid for incase you want background color to edit box-->
<solid android:color="#color/app_grey"/>
</shape>
and apply it on your edit box
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/editbox_custom_top_radius"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text"
android:padding="#dimen/dimen10"
android:background="#drawable/editbox_custom_bottom_radius"/>
</LinearLayout>
try it.
I'm trying to set a custom drawable to a EditText as expected below:
So I wrote this custom drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingBottom="12dp"
android:paddingLeft="36dp"
android:paddingRight="12dp"
android:paddingTop="12dp">
<item>
<shape>
<corners android:radius="6dp"/>
<solid android:color="#android:color/white"/>
<stroke
android:width="1dp"
android:color="#BBBBBB"/>
</shape>
</item>
<item
android:width="32dp"
android:gravity="left">
<shape android:shape="rectangle">
<size android:width="32dp"/>
<corners
android:bottomLeftRadius="6dp"
android:topLeftRadius="6dp"/>
<solid android:color="#AAAAAA"/>
</shape>
</item>
<item android:left="8dp">
<bitmap
android:gravity="left"
android:src="#drawable/ic_email"
android:tileMode="disabled"/>
</item>
</layer-list>
The result in preview is quite good (except different corner size, not handled by Android Studio)
But in device, it totally not works...
The second item is stretched and does not respect the width attribute.
I know, I can do it with a 9-patch, but I want to know if is it possible with drawables?
Managed to do it with a single background drawable and a compound drawable on the EditText.
bg_edittext_icon.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/white" />
</shape>
</item>
<item>
<shape>
<solid android:color="#AAAAAA" />
<corners android:radius="6dp" />
</shape>
</item>
<item
android:left="34dp">
<!-- left is icon size + 2x side padding around icon-->
<!-- 18 + 8 + 8 -->
<shape>
<solid android:color="#android:color/white" />
<corners android:radius="6dp"
android:topRightRadius="6dp"
android:topLeftRadius="0dp"
android:bottomRightRadius="6dp"
android:bottomLeftRadius="0dp"/>
</shape>
</item>
<item>
<shape>
<corners android:radius="6dp" />
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#BBBBBB" />
</shape>
</item>
</layer-list>
In your layout, use it like that:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_edittext_icon"
android:inputType="textEmailAddress"
android:drawableLeft="#drawable/ic_email_white_18dp"
android:drawablePadding="20dp"
android:paddingLeft="8dp"
android:paddingRight="12dp"
android:paddingTop="12dp"
android:paddingBottom="12dp"
android:hint="Email"
/>
With paddingLeft the icon padding inside the grey box and drawablePadding the padding inside the grey box + padding of the edit zone.
Here is the result from Genymotion (aka, real device):
Hope you like it !
I need to make my EditText look like its got depth like in the image below.
I have this shape I don't know what to add to it
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark" />
</shape>
you can accomplish that with a drawable like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!--background to compensate the transparency under shadow and glow (for bigger depth values)-->
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark" />
</shape>
</item>
<!--top, dark, inner shadow-->
<item
android:bottom="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#1A000000" />
</shape>
</item>
<!--bottom, light, outer glow-->
<item
android:top="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#4DFFFFFF" />
</shape>
</item>
<!--your color-->
<item
android:top="#dimen/depth"
android:bottom="#dimen/depth">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#color/primary_dark />
</shape>
</item>
</layer-list>
and define a dimen in dimens.xml
<dimen name="depth">2dp</dimen>
by changing of the dimen you can change the depth of the drawable
This might also make your field look close to the effect but with a little more depth (see below). It might be worth adding the background to a outer view to wrap the edit text too so you can indent the text if you haven't already done this.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- outer shine -->
<item
android:top="4dp"
android:left="4dp">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#1597FB" />
</shape>
</item>
<!-- inner shadow -->
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#004173" />
</shape>
</item>
<!-- inner background -->
<item
android:top="2dp"
android:left="2dp">
<shape android:shape="rectangle">
<corners android:radius="50dp" />
<solid android:color="#006EC3" />
</shape>
</item>
</layer-list>
And then you EditText as below;
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#drawable/rounded_rect_layered_combined">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:textColor="#ffffff"/>
</FrameLayout>