Set drawable position inside button - android

I've got a button and a drawable on the left of the text, but I want the drawable to be closer to the text. So I need to move the drawable.
I've defined android:drawableLeft but the content of the button is not centered.
Here is my code:
<Button
android:id="#+id/addsubject"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_action_add"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="#string/addsubject"
android:textColor="#color/white"
android:background="#drawable/custombutton1" />
Here is how it looks now:
And here is how I'd like it to be:
Thank you!

Use the app:iconGravity="textStart" attribute in the MaterialButton
<com.google.android.material.button.MaterialButton
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
../>
If you want to reduce the padding between the icon and the text just use the app:iconPadding attribute:
<com.google.android.material.button.MaterialButton
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"/>

try this
<Button
android:id="#+id/addsubject"
android:layout_width="160dip"
android:layout_height="60dip"
android:layout_gravity="center"
android:drawableLeft="#drawable/ic_action_add"
android:drawablePadding="2dip"
android:gravity="center"
android:paddingLeft="30dip"
android:paddingRight="26dip"
android:singleLine="true"
android:text="#string/addsubject"
android:textSize="13dip" />

I got this result by simply putting these properties to the button specifically
<Button
...
android:paddingLeft="60dp"
android:drawablePadding="-50dp"
android:gravity="center"
android:drawableLeft="#drawable/your_button_icon"
/>
You can tune in with your own values for your desired results.
Cheers :-)

I done this simply using paddingLeft and paddingRight. Its works!
...
android:paddingLeft="25dp"
android:paddingRight="25dp"/>

You can use LinearLayout as your button by adding OnClickListener and customize however you want.
<LinearLayout
android:layout_width="160dip"
android:layout_height="60dip"
android:orientation="horizontal"
android:background="#drawable/custombutton1">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="#drawable/ic_action_add"
android:layout_gravity="center"
android:layout_weight="1"
android:scaleType="center">
</ImageView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/addsubject"
android:layout_gravity="center"
android:layout_weight="1"/>
</LinearLayout>

The padding between the left border of button and right border of the icon is controlled by android:paddingLeft property. The padding between the icon and the text is defined by android:drawablePadding property.
Add these two properties to you button and set the values you are happy with.
<Button
...
android:paddingLeft="24dp"
android:drawablePadding="8dp"
/>

There's also other possibilities that you want to inflate the drawable/icon programatically, after reading a while and using a lot of combination using XMLs, but still doesn't works,
You might want to fork below library instead, to suits your use-cases, such as, customizing the view, position, etc programatically, see below reference:
Drawable/icon aligned with text, see sample here
Button like facebook sign-in, see sample here
For RadioButton, see full gist code here
P.S: Copyrighted and regards to the author

for Materialbutton, you have to use the icon attribute and you can also change its tint with iconTint like this example:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:icon="#drawable/ic_cancel"
app:iconTint="#color/black"
android:text="text" />

Related

Button with left icon and text perfectly aligned

Good evening,
I am developing an Android app and I am currently doing the Login interface in XML.
I am trying to create buttons with icon and text, like in the picture below :
http://i.imgur.com/J5Cj1w4.png
And here is my actual result :
http://i.imgur.com/VPALdDD.png
With this code :
<Button
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_facebook"
android:text="#string/signup_with_facebook"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_facebook_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
<Button
style="?android:textAppearanceSmall"
android:layout_marginTop="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/sign_up_google"
android:text="#string/signup_with_google"
android:textColor="#color/primaryTextLight"
android:drawableLeft="#drawable/ic_google_plus_box_white_24dp"
android:layout_weight="0"
android:background="#drawable/button_shape_login"/>
I am stuck at this step.
How can I get the final needed result with XML code ?
Thank you!
You can give padding to drawable like this : android:drawablePadding="50dip"
Also this question answered in this post.
Use android:drawablePadding attribute
You only need to specify android:paddingLeft attribute.
Try specifying a value of 36dp for example
The better option is to actually make the button a Relative/Linear layout with the layout set inside, the drawablePadding will not work so well with different lengths of text and buttons.
Essentially, RelativeLayout which is your button with a nested ImageView and TextView and you'll have good control of the layout, with consistent paddings around images and text within the button.
I haven't tested the following, this is essentially what you need
<LinearLayout
android:id="#+id/facebookButton"
android:layout_width="250dp"
android:layout_height="80dp"
android:orientation="horizontal">
<ImageView
android:src="#drawable/your_drawable"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="50dp"/>
<TextView
android:text="Sign up with Facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"/>
</LinearLayout>
android:gravity="center_vertical"
//use in both textView
<Button
android:paddingLeft="50dp"
android:textAlignment="textStart"
android:drawableLeft="#drawable/"
/>

Customize AppCompatButton drawableLeft?

How can I place drawableLeft and button text of AppCompatButton like in the image below?
I have tried to apply paddingLeft, but the text is moving away from the drawable to the right.
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:paddingLeft="20dp"
android:text="#string/filter_by"
android:textAllCaps="false" />
And so far the result is looking like
Layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/profileHeader"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp">
<android.support.v7.widget.AppCompatButton
android:id="#+id/findFriends"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_search"
android:text="#string/find_friends"
android:textAllCaps="false" />
<android.support.v7.widget.AppCompatButton
android:id="#+id/filterBy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableLeft="#drawable/ic_btn_filter"
android:text="#string/filter_by"
android:textAllCaps="false" />
</LinearLayout>
And the interpreted requirement for making things clear.
Using layout_weight property am adjusting the width of both buttons and what i have to do is, the text should be in center of the button
and the drawable should be aligned near left of the text.
Any suggestion please?
use
android:drawablePadding=""
probably you can't, if you want like this then you have to make it using complex layout like this see

Android button with icon and text

I have some buttons like this in my app:
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp" />
I'm trying to create a same button with text and a icon.
android:drawableLeft doesn't work for me (Maybe it would, but i don't know how to set a max height to the icon).
So i created a LinearLayout with a ImageView and a TextView and made it act like a button:
<LinearLayout
android:id="#+id/bSearch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_default"
android:clickable="true"
android:padding="16dp"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:adjustViewBounds="true"
android:maxHeight="30dp"
android:maxWidth="30dp"
android:scaleType="fitCenter"
android:src="#drawable/search_icon" />
<TextView
android:id="#+id/tvSearchCaption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textSize="24sp"
android:paddingRight="30dp"
android:gravity="center"
android:text="Search" />
</LinearLayout>
My new button is exactly what i want (font size, icon and text placement).
But it doesn't look like my default buttons:
So i tried, to change the background and the text color of my new Button:
Button Search = (Button) findViewById(R.id.bSearch);
LinearLayout bSearch2 = (LinearLayout) findViewById(R.id.bSearch2);
bSearch2.setBackground(bSearch.getBackground());
TextView tvSearchCaption = (TextView)findViewById(R.id.tvSearchCaption);
tvSearchCaption.setTextColor(bSearch.getTextColors().getDefaultColor());
This gives a strange result, my old button, gets messed up:
When i change the order of these two buttons in the XML, so the "new button" goes first, it makes another strange result:
Now i noticed, that when i try to press the old button, the new one gets pressed.
Any ideas?
Try this one.
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:drawableLeft="#android:drawable/ic_menu_search"
android:textSize="24sp"/>
To add an image to left, right, top or bottom, you can use attributes like this:
android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom
The sample code is given above. You can also achieve this using relative layout.
You can use the Material Components Library and the MaterialButton component.
Use the app:icon and app:iconGravity="start" attributes.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/..."
app:iconGravity="start"
../>
For anyone looking to do this dynamically then setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) on the buttons object will assist.
Sample
Button search = (Button) findViewById(R.id.yoursearchbutton);
search.setCompoundDrawablesWithIntrinsicBounds('your_drawable',null,null,null);
This is what you really want.
<Button
android:id="#+id/settings"
android:layout_width="190dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/colorAccent"
android:drawableStart="#drawable/ic_settings_black_24dp"
android:paddingStart="40dp"
android:paddingEnd="40dp"
android:text="settings"
android:textColor="#FFF" />
#Liem Vo's answer is correct if you are using android.widget.Button without any overriding. If you are overriding your theme using MaterialComponents, this will not solve the issue.
So if you are
Using com.google.android.material.button.MaterialButton
or
Overriding AppTheme using MaterialComponents
Use app:icon parameter.
<Button
android:id="#+id/bSearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="Search"
android:textSize="24sp"
app:icon="#android:drawable/ic_menu_search" />
What about like this?
<Button
android:id="#+id/yourID"
android:drawableStart="#drawable/ic_flag"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:textStyle="bold"
android:textAlignment="textStart"
android:background="#drawable/btn_background"
android:fontFamily="#font/YourFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Text" />
try It:-
position-> left,right,top,bottom
android:drawableposition="#android:drawable/-yourImage-"
android:drawabletop="-Yourfilesrc-"
android:drawablebottom="-Yourfilesrc-"
android:drawableleft="-Yourfilesrc-"
android:drawableright="-Yourfilesrc-"
I was experiencing the same problem with pure Button view. In my case XML solution was not usefull, because I have some logic to show/hide this icon.
There is one more solution using pure Kotlin:
(bSearch as? MaterialButton)?.let {
icon = AppCompatResources.getDrawable(context, R.drawable.search_icon)
iconGravity = MaterialButton.ICON_GRAVITY_START
}
P.S. We can cast Button to MaterialButton as fair as we can use icon param in Button: icon param is not actually Button param - it is sneaky MaterialButton param somehow working with Buttons.

Android Button Customization

May i ask how to put icon on my button like the image below?
And also where can i have resources or sample resources of buttons like this?
You'll need to set the drawable to the left of your Button. Either via xml or programmatically.
Either create a button and set a compound drawable or create an ImageButton.
<TextView android:id="#+id/login_with_facebook"
android:layout_width="100dp"
android:height="48dp"
android:text="FACEBOOK"
android:gravity="center"
android:drawableLeft="#drawable/facebook_icon_48_x_48"
android:clickable="true"/>
<Button android:drawableLeft="#drawable/image"
android:text="facebook"/>
If u want space between image and text, then give drawablePadding Like:
<Button android:drawableLeft="#drawable/image"
android:text="facebook"
android:drawablePadding="10dp"/>
using RelativeLayout it can be done easily. here I got snippet for you,
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/fb_logo_button"
android:text=""
android:background="fb_logo"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<Button
android:layout_toRightOf="#id/fb_logo_button"
android:id="#+id/fb_text_button"
android:text="Facebook"
android:textAlignment="center"
android:background="#android:color/holo_blue_bright"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</RelativeLayout>
this will ensure that whatever your images are for respective DPIs, it will align this layout to center horizontally. Within this layout logo will be always to left of text as in picture.
This is applicable when your parent is also a RelativeLayout. If you got LinearLayout as parent then in above RelativeLayout tag change android:layout_centerHorizontal="true" to android:layout_gravity="center_horizontal"
I can't test it in eclipse as I'm out of home. Try this and let me know. Thanks.

Button view with drawableLeft

I need to add drawable to my button and added drawableLeft to it. If button's width is set to wrap_content, then everything is fine and looks good. But if I set custom width to my button then drawableLeft image goes away from button's caption to left edge of the button. Is this somekind of a design bug in android? How can I fix this to let button look the same on custom width?
Regards,
evilone
Seems that x-position of the drawableLeft depends on left padding and x-scroll position: source. So that's by-design behavior. I think you'd better use SpannableString and ImageSpan classes to display an image to the left of the button's text:
final SpannableStringBuilder builder = new SpannableStringBuilder("\uFFFC");
builder.setSpan(new ImageSpan(getResources.getDrawable(R.drawable.your_drawable)),
0, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(getString(R.string.your_string));
textView.setText(builder);
Also, you can use Html.fromHtml() method to build Spannable from HTML. But in this case you need to implement Html.ImageGetter interface to load images from resources.
EDIT: There's a way to do a button-like widget using only XML:
<LinearLayout
android:layout_width="200dip"
android:layout_height="wrap_content"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:background="#android:drawable/btn_default">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:src="#drawable/button_icon"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:text="#string/button_text"/>
</LinearLayout>
Just set android:gravity="center_vertical" to your button attributes.
<Button
android:id="#+id/button_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_btn_icon"
android:drawablePadding="5dp"
android:text="Button"
android:gravity="center_vertical" />
I had this problem just right now, but I found out that you just need to set android:padding to the button. That way you push the picture away from left side of the button. Check this code:
`<Button
android:id="#+id/button1"
android:layout_width="170dp"
android:layout_height="50dp"
android:layout_below="#+id/txt1"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:background="#drawable/button"
android:drawableLeft="#drawable/icon"
android:drawablePadding="20dp"
android:minHeight="40dp"
android:onClick="onClick"
android:padding="40dp"
android:text="#string/string1"
android:textSize="16sp" />`

Categories

Resources