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
I am using a LinearLayout as view group which holds two children (TextView and EditText). My XML code looks as follows:
<?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="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"
android:textSize="20sp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Edit Text"
android:textSize="20sp" />
</LinearLayout>
which will produce the following design:
As depicted in the zoomed-in view on the left, TextViewand EditText are not equally aligned vertically (EditText is indented a bit more as shown by the small red arrows).
It seems there is a bit of a padding (a few dp) around the hint and the line underneath which prevents them from "touching" the left edge of their view field. Is there any way to force the hint within EditText to squeeze to the left of its view field?
How can I get rid of this indentation, other than by adding paddings and margins?
Thanks for any ideas and advice!
Without add any paddings and margins you can use a custom drawable for the background of your Edittext to remove the default padding
in drawable/edittext_bg.xml
<?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/transparent" />
</shape>
</item>
<item
android:top="-2dp"
android:right="-2dp"
android:left="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#000000" />
</shape>
</item>
</layer-list>
Now use it in your editext:
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"
android:hint="Edit Text"
android:textSize="20sp" />
Output:
It should not be in this way... You should check your style.xml, maybe you set additional margins there.
Also, try to specify layout_gravity:"left" attribute.
try typing letters on Edit Text then you can see both textview and edit text was aligned equally
Did you try Constraint layout? It's removing the need to have so much hierarchy in xml layouts and is easy to use.
this preview, run the simulator for correct preview.
But, for best result use https://developer.android.com/reference/android/support/design/widget/TextInputLayout.html
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).
I've got a LinearLayout with two EditText descendants (not direct children). I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) on each to put a drawable (the same drawable) on the right side of each of the EditTexts. The two edit texts are identical, except for their order in the ListView. However, the size of the drawable in the first EditText becomes larger than the second after adding the drawables. Specifically, the first EditText resizes to accomodate the intrinsic size of the drawable, whereas the second EditText does not (although it does grow some small amount).
Why do two of the same drawables appear differently in the two near-identical views?
Also, I could solve this problem by figuring how to make the drawable not force the EditText to resize. As you can see, I've followed the directions at https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList regarding making the background scale, but that doesn't seem to do the trick.
the code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
style="#style/fieldLabel"
android:text="phone number"/>
<EditText
android:id="#+id/contactPhoneField"
android:inputType="phone"
android:layout_width="fill_parent"
style="#style/editableField"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
style="#style/fieldLabel"
android:text="e-mail address"/>
<EditText
android:id="#+id/contactEmailField"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
style="#style/editableField"/>
</LinearLayout>
the drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#FFD060"
android:endColor="#FFBB33"
android:angle="270"/>
<padding android:left="5dp" android:top="5dp"
android:right="5dp" android:bottom="5dp" />
<corners android:radius="5dp" />
<stroke android:width="2dp" android:color="#FF8800"/>
</shape>
</item>
<item android:drawable="#drawable/warn"/><!-- a 35x35 px png which is taller than the native height of an EditText-->
</layer-list>
then at some point I call:
contactPhoneField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
contactEmailField.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.error_background, 0);
Additionally, every subsequent time I call EditText.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.xyz,0) (after calling it the first time) causes first EditText's size to toggle between matching the 2nd EditText and matching the drawable size!
Is it possible to set the margin or padding for the image which we added with the android:drawableLeft?
As cephus mentioned android:drawablePadding will only force padding between the text and the drawable if the button is small enough.
When laying out larger buttons you can use android:drawablePadding in conjunction with android:paddingLeft and android:paddingRight to force the text and drawable inward towards the center of the button. By adjusting the left and right padding separately you can make very detailed adjustments to the layout.
Here's an example button that uses padding to push the text and icon closer together than they would be by default:
<Button android:text="#string/button_label"
android:id="#+id/buttonId"
android:layout_width="160dip"
android:layout_height="60dip"
android:layout_gravity="center"
android:textSize="13dip"
android:drawableLeft="#drawable/button_icon"
android:drawablePadding="2dip"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:singleLine="true"
android:gravity="center" />
TextView has an android:drawablePadding property which should do the trick:
android:drawablePadding
The padding between the drawables and the text.
Must be a dimension value, which is a floating point number appended
with a unit such as "14.5sp". Available units are: px (pixels), dp
(density-independent pixels), sp (scaled pixels based on preferred
font size), in (inches), mm (millimeters).
This may also be a reference to a resource (in the form
"#[package:]type:name") or theme attribute (in the form
"?[package:][type:]name") containing a value of this type.
This corresponds to the global attribute resource symbol
drawablePadding.
android:drawablePadding will only create a padding gap between the text and the drawable if the button is small enough to squish the 2 together. If your button is wider than the combined width (for drawableLeft/drawableRight) or height (for drawableTop/drawableBottom) then drawablePadding doesn't do anything.
I'm struggling with this right now as well. My buttons are quite wide, and the icon is hanging on the left edge of the button and the text is centered in the middle. My only way to get around this for now has been to bake in a margin on the drawable by adding blank pixels to the left edge of the canvas with photoshop. Not ideal, and not really recommended either. But thats my stop-gap solution for now, short of rebuilding TextView/Button.
Yes. use drawablePadding as follows,
<TextView
android:id="#+id/tvHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings and Contents"
android:drawableLeft="#drawable/icon_success"
android:drawablePadding="10dp" />
Make your drawable resources.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<inset android:drawable="#drawable/small_m" android:insetLeft="10dp" android:insetTop="10dp" />
</item>
<item>
<inset android:drawable="#drawable/small_p" android:insetLeft="10dp" android:insetTop="10dp" />
</item>
</selector>
android:drawablePadding is the easiest way to give padding to drawable icon but You can not give specific one side padding like paddingRight or paddingLeft of drawable icon.To achieve that you have to dig into it.
And If you apply paddingLeft or paddingRight to Edittext then it will place padding to entire Edittext along with drawable icon.
According to #Bhargav Thanki:
android:drawablePadding is the easiest way to give padding to
drawable icon but You can not give specific one side padding like
paddingRight or paddingLeft of drawable icon.To achieve that you
have to dig into it. And If you apply paddingLeft or paddingRight
to EditText then it will place padding to entire EditText along
with drawable icon.
Example:
<TextView
android:layout_width="match_parent"
android:padding="5dp"
android:id="#+id/date"
android:gravity="center|start"
android:drawableEnd="#drawable/ic_calendar"
android:background="#drawable/edit_background"
android:hint="Not Selected"
android:drawablePadding="10dp"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textColor="#color/black"
android:layout_height="wrap_content"/>
define a shape for your edittext and give it a padding
For Example
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<padding
android:left="5dp"
android:right="5dp"
/>
<solid android:color="#F6F6F6" />
<stroke
android:width="1px"
android:color="#C3C3C3" />
<corners
android:bottomLeftRadius="1dp"
android:bottomRightRadius="1dp"
android:topLeftRadius="1dp"
android:topRightRadius="1dp" />
</shape>
The padding defined in this shape will help in give padding to drawableleft or right
---------------------- Apply this shape on EditView
<EditText
android:id="#+id/example"
android:layout_width="fill_parent"
android:layout_height="36dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#drawable/shape2"
android:drawableLeft="#drawable/icon1"
android:drawablePadding="#dimen/txtDrwblPadding"
android:ems="10"
/>
using that defined shape as background will give your EditText some style plus margin to drawableLeft.
<TextView
android:layout_width="wrap_content"
android:layout_height="32dp"
android:background="#drawable/a"
android:drawableLeft="#drawable/concern_black"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:drawablePadding="10dp"
android:text="text"/>
note: layout_width needs to be wrap_content and use paddingLeft paddingRight drawablePadding to control gap. If you specify layout_width value is will has gap between icon and text, I think once give the layout_width a specify value, the padding will measure.
I'll throw my answer into the ring as well. If you want to do this programmatically you can do the following.
final Drawable drawable = ContextCompat.getDrawable(getContext(), R.drawable.somedrawable);
final boolean isLTR = ViewCompat.LAYOUT_DIRECTION_LTR == ViewCompat.getLayoutDirection(this);
final int iconInsetPadding = getResources().getDimensionPixelSize(R.dimen.icon_padding);
final Drawable insetDrawable = new InsetDrawable(drawable, isLTR ? 0 : iconInsetPadding, 0, isLTR ? iconInsetPadding : 0, 0);
This will add the padding to the end of the drawable where end will mean left/right depending if phone is in LTR or RTL.
Another easy solution can be achieved by inset layerlist
layered_drawable
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<inset
android:insetRight="30dp"
android:drawable="#drawable/ic_air_date">
</inset>
</item>
</layer-list>
Button in XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/layered_drawable"
android:text="something" />
Instead of Button use LinearLayout with ImageView and TextView inside. In child items like ImageView and TextView use android:duplicateParentState="true".
You can use a padding for the button and you can play with drawablePadding
<Button
style="#style/botonesMenu"
android:padding="15dp"
android:drawablePadding="-15dp"
android:text="#string/actualizarBD"
android:textAlignment="gravity"
android:gravity="center"
android:layout_row="1"
android:layout_column="0"
android:drawableTop="#drawable/actualizar"
android:id="#+id/btnActualizar"
android:onClick="actualizarBD" />
you can use a specific padding depends where put your drawable, with android:paddingLeft="10dp" or android:paddingBottom="10dp" or android:paddingRight="10dp" or android:paddingTop="10dp"
You should consider using layer-list
Create a drawable file like this, name it as ic_calendar.xml
<?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/transparent"/>
</shape>
</item>
<item android:right="10dp">
<bitmap android:gravity="center_vertical|left"
android:src="#drawable/ic_calendar_16dp"
android:tint="#color/red"
/>
</item>
</layer-list>
Under layout file,
<TextView
android:id="#+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_calendar"
android:textColor="#color/colorGrey"
android:textSize="14sp"
/>
You can use android:drawableLeft="#drawable/your_icon" to set the drawable to be shown on the left side. In order to set a padding for the drawable you should use the android:paddingLeft or android:paddingRight to set the left/right padding respectively.
android:paddingRight="10dp"
android:paddingLeft="20dp"
android:drawableRight="#drawable/ic_app_manager"
just remake from:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"/>
<solid android:color="#android:color/white"/>
</shape>
to
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:right="#dimen/_2dp"
android:left="#dimen/_2dp"
android:bottom="#dimen/_2dp"
android:top="#dimen/_2dp"
>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="40dp"/>
<solid android:color="#android:color/white"/>
</shape>
</item>
</layer-list>
If the size of drawable resouce is fixed, you can do like this:
<Button
android:background="#drawable/rounded_button_green"
style="?android:attr/selectableItemBackground"
android:layout_height="wrap_content"
app:layout_widthPercent="70%"
android:drawableRight="#drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
tools:text="example" />
The key here is that:
android:drawableRight="#drawable/ic_clear_black_24dp"
android:paddingRight="10dp"
android:paddingLeft="34dp"
That is, the size of drawable resource plus paddingRight is the paddingLeft.
You can see the result in this example
textView.setCompoundDrawablesWithIntrinsicBounds(AppCompatResources.getDrawable(this,drawable),null,null,null);
addressTitleView.setCompoundDrawablePadding();
Tries to use negative padding
Like:
android:paddingLeft="-8dp"