android:drawableLeft margin and/or padding - android

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"

Related

Background drawable not applying to button

I am trying to create a capsule/pill shaped button in XML. I have defined a drawable and set it as the background of my button, but in the preview, and when I run the app, it's displaying as a blue rectangle, despite the background drawable being a white oval. Does anyone know why that might be happening?
Here's the button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_box"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_capsule"
android:text="#string/search"
android:textColor="#color/precipLightBlue"/>
And here's the background drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
To have a capsule/pill shaped button you can use the MaterialButton in the official Material Component library using the app:cornerRadius attribute.
<com.google.android.material.button.MaterialButton
android:layout_width="100dp"
app:cornerRadius="32dp"
..../>
With the version 1.1.0 you can also customize the shape of your component using the app:shapeAppearanceOverlay attribute
<com.google.android.material.button.MaterialButton
....
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.ButtonRounded"
.../>
In the style define:
<style name="ShapeAppearanceOverlay.ButtonRounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">32dp</item>
</style>
You can also try to use the ExtendedFloatingActionButton:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/exfab"
android:layout_width="wrap_content"
.... />
You put a shape that you didn't defined. To define an oval you should put the shape="oval" option in the tag. Although i think you want a rectangle with rounded corners as i see in your code.
Also 1000dp radius is a lot, maybe that's making an issue.
Define size too. Because that shape doesn't have any size and may not appear as you are using wrap_content in the button definition
Try this:
<corners android:radius="30dp" />
<solid android:color="#android:color/white" />
<size
android:width="200dp"
android:height="50dp"/>
If you want an oval remove the corners tag and add android:shape="oval" as property of shape tag
Here is the code regarding button drawable background.
Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
android:background="#color/colorLightPurple">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_bg"
android:text="Search"
android:textColor="#color/colorBlue"/>
</LinearLayout>
For button background , please add same drawable file
seems like you're doing everything right. Just a small addition, try adding android:shape="rectangle" in the shape tag.
This is what button_capsule.xml should look like.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
Hope this works. All the best.
In Material design version 1.2.0 they have fixed this issue and added a property for setting background for Material Button
here is the dependency for the latest version
com.google.android.material:material:1.2.0
and also if u want to remove the space between the drawable top and bottom so that the drawable get the whole width and height use these 2 properties
android:insetTop="0dp"
android:insetBottom="0dp"
if want to explore more about the properties u can refer to the release of library and can check all the properties.
https://github.com/material-components/material-components-android/releases/tag/1.2.0
use this.
<androidx.appcompat.widget.AppCompatButton
.....
/>

edittext cursor is not visible when width is wrap content

I have edit text width background drawable and its width is wrap content. When I changed widht wrap to match parent then the cursor will visible.
I have tried android:textCursorDrawable="#null" and editext .setRawInputType(InputType.TYPE_CLASS_TEXT); but it is not working for me.
Note: Background color will change dynamically
Editext code:
<EditText
android:id="#+id/edt_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/rounded_border_text_view"
android:gravity="center"
android:inputType="textMultiLine"
android:maxLength="75"
android:padding="#dimen/dp_08"
android:textColor="#color/white"
android:textSize="#dimen/sp_30"> <requestFocus /> </EditText>
rounded_border_text_view.xml
<?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_02"
android:color="#color/color_00000000" />
<solid android:color="#color/transparent_totally" />
<padding
android:bottom="#dimen/dp_05"
android:left="#dimen/dp_05"
android:right="#dimen/dp_05"
android:top="#dimen/dp_05" />
<corners android:radius="#dimen/dp_05" />
When using wrap_content there is a risk the component is just too small for you to see it.
In that case you could set a min width ? Using styles would be a good practice if you have several components in that case.

Less Opacity in view will make less opacity for all elements in android

I have a layout in which I am using a color and I made its opacity to 0.2. I have one textview in that layout but when I am specifying less opacity to layout then Textview text also faded. Why is that so? I only want to fade background color not textview's text.
// code of layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="0.2"
android:background="#drawable/background_rounded_black"
android:padding="#dimen/dp10">
<TextView
android:id="#+id/txtNews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="1"
android:text="djfhwhfirhfiohrhvfuhvfjvbv vknfjvnfjv fv vefjkhvfhvv hvufevhfuievefv ivfhiefhvfe ifvjioefv"
android:textColor="#android:color/white"
android:textSize="#dimen/dp15" />
<TextView
android:id="#+id/txtTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/txtNews"
android:layout_marginTop="#dimen/dp10"
android:alpha="1"
android:drawableLeft="#drawable/clock_news"
android:text="12 hrs ago"
android:textColor="#android:color/white"
android:textSize="#dimen/dp15" />
and now how it looks http://prntscr.com/gbxi87
You set alpha tag for the parent View which is RelativeLayout. You should set alpha exactly for the View that you want to add transparency for it. Or if you want to add transparency only for background, you should use transparent color as the background of the View.
Also you said you have one TextView in your layout which is two as I see!!!
I suggest you to set alpha color as background of textview it will work fine.
you are set opacity to that widgets so its set opacity to whole textview widgets instead you have to set only opacity to background color of textView.
you can define color with 8 digit hash code.
for example #90FFFFFF here first two digit set opacity to color.
for More info you can see this link to set opacity:
Opacity color to Hash code
You have to change alpha on your background, not to the whole element
So remove
android:alpha="0.2"
And change just opacity of
android:background="#drawable/background_rounded_black"
Simpliest way is to create new background_rounded_black_opacity.xml in drawable folder (just copy drawable/background_rounded_black) and then change background color to what you need.
So if background_rounded_black looks like
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#000000"/>
<stroke android:width="3dip" android:color="#B1BCBE" />
<corners android:radius="10dip"/>
</shape>
You can add your 0.2 opacity like adding 33 to all your colors, so in your case it will be
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33000000"/>
<stroke android:width="3dip" android:color="#33B1BCBE" />
<corners android:radius="10dip"/>
</shape>

How to make the space from the text of the button to its bottom edge independent of the length of the text? (Android)

There are buttons that use the custom style. The distance from the text of the button to its bottom edge differs depending on the length of the text - in case the text is transferred to the second and more lines, the distance to the edge is significantly reduced. Is there a way to somehow make these distances the same? Both buttons are in the LinearLayout. Also i use Calligraphy lib to set custom fonts.
This picture describes the problem.
Buttons in Layout:
<Button
android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_in_text"
android:textAllCaps="false"
android:text="short text"
android:gravity="left|center_vertical"
style="?android:attr/borderlessButtonStyle"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
<Button
android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:background="#drawable/btn_in_text"
android:textAllCaps="false"
android:text="Very long text. The longes text that you saw before"
android:gravity="left|center_vertical"
android:layout_marginTop="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
/>
btn_in_text.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="#000000" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Tried to use
android:includeFontPadding="false"
android:paddingBottom="2dp"
and
android:lineSpacingMultiplier="1.2"
but it did not help.
The very short answer: add this attribute to both of your <Button> tags:
android:minHeight="0dp"
Because you're using <Buttton> instead of <TextView>, you are getting all of the default styles applied to Buttons by the android framework. One of these is a minimum height. That minimum height, combined with your gravity attribute including center_vertical, means that your text will float in the middle of the button until your text is long enough that it wraps enough times to exceed the button's default height.
Another possible solution is to use <TextView> tags instead of Buttons, and remove the style="?android:attr/borderlessButtonStyle" attribute. This will also let you remove android:textAllCaps="false".
Iam not sure about the specifics of your problem but I had a similar problem where my textsize was changing the button's size as well and fixed it using
android:baselineAligned="false"
In the layout which carries the Buttons since by default the bottom edge of all the text in a row is at the same height.
Here is the link of my solved problem which includes a picture similar to yours :-
In XML Button size changes with its Font size
If that doesn't work try making a fixed button height instead of wrap_content.

Align TextView and EditText left in vertically oriented LinearLayout

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

Categories

Resources