I am trying to get a rounded EditText.
My EditText in layout is like
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" >
<EditText
android:id="#+id/EditTextSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_round"
android:padding="5dip"
android:singleLine="true" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/searchBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#android:drawable/ic_menu_search" />
</FrameLayout>
and the drawable I am using as EditText background is like
<?xml version="1.0" encoding="utf-8"?>
<!-- res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners
android:bottomLeftRadius="12dp"
android:bottomRightRadius="12dp"
android:topLeftRadius="12dp"
android:topRightRadius="12dp" />
</shape>
In my XML graphical layout, my edittext looks fine and rounded, but in emulator and also in device it does not look like that?
Does anyone have any idea why could that be?
Thanks
Remove the android:padding="10dp" attribute to make the drawable look like this. I just tested it and removing the padding attribute shows the rounded corners just fine.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
<corners
android:bottomLeftRadius="12dp"
android:bottomRightRadius="12dp"
android:topLeftRadius="12dp"
android:topRightRadius="12dp" />
</shape>
To add padding to the Shape Drawable, notice the padding attribute added in the code above.
The proof lies in the pudding: ;-)
Device Screenshot:
Emulator Screenshot:
You didn't provide the stroke element, so you probably just don't see the background (since it's only a white fill!). When you change the background or add a stroke, it should be visible.
Add:
<stroke android:width="2dp" android:color="#000000" />
And poof! The rectangle is visible :) BTW The padding isn't the issue - it's simply ignored.
Related
As far as my knowledge aids me, there isn't any property to drop shadow around an EditText field, that can be set in the layout file. I searched for the problem and found solution that I've to provide a custom background xml drawable.
My drawable/background_with_shadow looks like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-lis xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape
android:shape="rectangle">
<solid android:color="#android:color/darker_gray" />
<corners android:radius="5dp"/>
</shape>
</item>
<item android:right="1dp" android:left="1dp" android:bottom="2dp">
<shape
android:shape="rectangle">
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</layer-list>
and I'm setting this property for my LinearLayout which contains an EditText
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_with_shadow">
<EditText>
.
.
</EditText>
<LinearLayout>
However it just drops the shadow around the bottom and bottom corners.
How would I do it for all the four sides and corners?
I'm trying to achieve this:
There are some better ways to get shadow around your edit text without using layer-list :
#1
Wrap your EditText in a CardView like this :
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="7dp"
android:layout_margin="10dp"
app:cardCornerRadius="0dp">
<EditText
android:id="#+id/msg_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:hint="Write a message"
android:paddingLeft="5dp"
android:paddingStart="5dp"
android:textColorHint="#c7c7c7"
android:textSize="15dp" />
</android.support.v7.widget.CardView>
OUTPUT :
2
Use a 9 patch as the background of your EditText :
<EditText
android:id="#+id/msg_box"
android:padding="20dp"
android:background="#drawable/shadow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:lines="5"
android:gravity="top"
android:hint="Enter your complaint..."
android:textColorHint="#a7a7a7"
android:textSize="15dp" />
OUTPUT
Read more about 9patch here.
Here's a great online tool to generate 9 patch shadow.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item>
<shape>
<!-- set the shadow color here -->
<stroke
android:width="2.5dp"
android:color="#color/darker_gray" />
<!-- setting the thickness of shadow (positive value will give shadow on that side) -->
<!--Set shadow width as per your requirement -->
<padding
android:bottom="2dp"
android:left="0.5dp"
android:right="2dp"
android:top="0.5dp" />
<corners android:radius="2dp" />
</shape>
</item>
<!--White Background -->
<item>
<shape>
<solid android:color="#fff" />
<corners android:radius="2dp" />
</shape>
</item>
</layer-list>
Set this layout as your LinearLayout background.
I am trying to get a TextView with white background and rounded corners and text in the middle.
Something that looks like this:
So far I have this but it is not giving me the above effect.
<TextView
android:textColor="#000000"
android:background="#FFFFFF"
android:text="0" />
First of all, I would create a custom drawable resource for easy implementation of rounded corners.
(place this in res/drawable)
my_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="ffffff" />
<corners
android:radius="15dp"/>
<stroke
android:width="1dp"
android:color="#android:color/black" />
</shape>
More info on xml drawable resources can be found right here if you want to get into more advanced drawables (gradients, layer-list, animation, etc...)
Then change your TextView in xml layout file to match this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textColor="#000000"
<!--refer to your custom drawable from earlier-->
android:background="#drawable/my_bg"
<!--center text within TextView-->
android:gravity="center"
android:text="0" />
I hope this helps, Happy Coding!
Set the gravity
android:gravity="center"
http://developer.android.com/reference/android/widget/TextView.html#attr_android:gravity
You are missing the width and height attribute for TextView.
For the rounded corners use a Shape Drawable
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Under res/drawable
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#FFFFFF"/>
<corners android:radius="7dp" />
<stroke android:width="5px" />
</shape>
Then
<TextView
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#drawable/background"
android:gravity="center"
android:text="0"
android:textColor="#000000" />
Snap
I'm having a problem with the rounded corners background shape in the emulator and I really can't figure it out.
The shape code is as follows:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dip" />
<stroke
android:width="1dip"
android:color="#ccffffff" />
<solid
android:color="#cc111111" />
<padding
android:left="3dip" android:top="3dip"
android:right="3dip" android:bottom="3dip" />
</shape>
used in the following relative layout:
<RelativeLayout
android:id="#+id/loginboxlayout"
android:layout_width="190dp"
android:layout_height="240dp"
android:layout_centerInParent="true"
android:background="#drawable/rounded"
android:padding="0dp" >
(...)
</RelativeLayout>
On the graphical layout, in eclipse, it displays correctly, but on the emulator it doesn't: screenshots.
I'm using android 4.0.
Thanks in advance.
put shape file in res>layout folder and then
<RelativeLayout
android:id="#+id/loginboxlayout"
android:layout_width="190dp"
android:layout_height="240dp"
android:layout_centerInParent="true"
android:background="#layout/rounded"
android:padding="0dp" >
(...)
</RelativeLayout>
Rect.xml---(Put this XML in drawable folder)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp" android:paddingTop="50dp"
android:layout_marginLeft="50dp">
<solid android:color="#eeeee0" android:paddingLeft="25dp" />
<stroke android:width="0.8dp" android:color="#000000" />
<corners android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp" android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
And add it where...watever control you want to round--
android:background="#drawable/rect"
It will work definitely .
Dont use dip as size formate. Use dp instead.
And Replace
<corners android:radius="50px" />
instead of
<corners android:radius="50dip" />
It will solve your issue.
Emjoy. :)
<LinearLayout
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:background="#drawable/settings_border" android:padding="1dp"
android:layout_marginLeft="15dip" android:layout_marginRight="15dip" android:layout_marginTop="5dip">
<RelativeLayout android:id="#+id/map_refresh"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:background="#drawable/settings_selector_up"
android:padding="15dp">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Map refresh period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
<RelativeLayout android:id="#+id/own_location"
android:layout_height="fill_parent" android:layout_width="wrap_content"
android:padding="15dp"
android:background="#drawable/settings_selector_mid">
<TextView android:id="#+id/Text1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Own location update period">
</TextView>
<TextView android:id="#+id/TextView09"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="1 min"
android:layout_alignParentRight="true"
android:paddingRight="5dp">
</TextView>
</RelativeLayout>
I want set only bottom border in relativelayout. I want to dispaly listview style but not using listview. Lets say each listitem is relativlayout. I want set only bottom border so its look like a listview's divider.
I hope I understood what you said.
in the res folder create a new folder (if you don't already have it) named drawable
there create an xml named "borders.xml"
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#2f481c" />
<stroke android:width="2dp" android:color="#999999" />
<padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />
<corners android:radius="10px"/>
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:angle="90" android:centerColor="#6da23f" android:endColor="#8bc45d" android:startColor="#4c8a39" />
<stroke android:width="1dp" android:color="#FFFFFF" />
<padding android:bottom="4dp" android:left="3dp" android:right="3dp" android:top="6dp" />
<corners android:radius="10px"/>
</shape>
</item>
</selector>
You can further edit it as you like.
Then select the layout from the Outline and click Background properties, and select the borders xml that you created.
This will create borders for all 4. Alternatively, you can add a simple
<View android:layout_width="1dip"
android:layout_height="fill_parent"
android:background="#FFFFFF" />
line and add it to the bottom of your layout and change the color/size to your liking.
For some reason the other solutions didn't work for me - all borders were shown no matter how I changed it. This worked:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/gray" />
<solid android:color="#color/gray" />
</shape>
</item>
<item android:bottom="1dp">
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#color/white" />
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
The color of the border is gray and the background is white for the container (LinearLayout in my example). You can simply change the second item to make the border thicker or have border on the top/left/right.
This is how the layout xml looks like:
<LinearLayout
android:id="#+id/searchWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/bottom_gray_border"
android:layout_alignParentTop="true">
<EditText
android:id="#+id/searchEditText"
style="#style/EditTextSearch"
android:hint="#string/find"
android:layout_marginLeft="5dp"/>
</LinearLayout>
I got the idea from here: Is there an easy way to add a border to the top and bottom of an Android View?
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dip"
android:color="#FF8000" />
<solid
android:color="#00FFFFFF"
android:paddingLeft="10dip"
android:paddingTop="10dip"/>
<corners android:radius="10px"/>
<padding
android:left="10dip"
android:top="10dip"
android:right="10dip"
android:bottom="10dip" />
</shape>
You can save this as borderframe.xml in the res/drawable folder (create it if it doesnt exist yet), and reference it like so: android:background="#drawable/borderframe".
A simple way to display a border is to create a horizontal LinearLayout, and to set its background color and height according to the border you want.
I'm trying to define a nice rounded corner square shape in Android, but I can only get a nice rounded corner rectangle.
My xml in /res/drawable looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/uk.co.alexlydiate.sequencer">
<item android:state_pressed="true"
app:state_playing = "true" >
<shape>
<stroke
android:width="2dp"
android:color="#color/red" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
<solid
android:color="#color/blue" />
<size
android:width="3dp"
android:height="3dp" />
</shape>
</item>
</selector>
And all works fine except
<size android:width="3dp" android:height="3dp" />
Which I'd have hoped made the thing square, but no such luck.
My layout looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid8x8"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<uk.co.alexlydiate.sequencer.Key android:background="#drawable/key"
android:id="#+id/keyA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_margin="2px"
/>
<uk.co.alexlydiate.sequencer.Key android:background="#drawable/key"
android:id="#+id/keyA2"
android:layout_below="#+id/keyA1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" android:layout_margin="2px"
/>
</RelativeLayout>
Anybody got any bright ideas? Thanks!
AFAIK: shapes stretch themselves according to the View's layout.
Instead you can define the Square View and set shape background.
OR
Use an Image as background