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/"
/>
Related
I am trying to put 6 buttons in the middle of the screen and it worked until I added text to one of them, as you can see in the picture below :
https://imgur.com/IxIVgHj
This is the code for the 2 buttons that causes problems :
<LinearLayout
android:id="#+id/tableRow3"
android:padding="5dp"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/utilitiesButton"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_marginLeft="10dp"
android:background="#drawable/menu_sms"
android:gravity="bottom|right"
android:padding="10dp"
android:text="0"
android:textColor="#color/red"
android:textSize="40dp" />
<Button
android:id="#+id/statisticsButton"
android:layout_width="125dp"
android:layout_height="125dp"
android:layout_marginLeft="10dp"
android:background="#drawable/menu_statistics"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
I need the text to be bottom|right. The bigger the text size from the left button is, right button goes down more. Any ideas on this ?
Thank you,
Marius
If you set android:baselineAligned="false" on the LinearLayout, then the buttons inside the LinearLayout would align properly.
Reference: https://possiblemobile.com/2013/10/shifty-baseline-alignment/
Comment
Instead of using a LinearLayout try and use relative layout instead, with an android:gavity of fill_horizontal, and add a bit of space between your textviews, also try and decrease the padding on the first button.
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" />
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.
I have a button with a symbol of an arrow. Besides, I want to have some margin between the button and the adjacent elements:
<Button
android:id="#+id/TSPrev"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="#android:color/holo_orange_light"
android:text="#string/left"
android:textSize="60sp" />
However, when I do this, the arrow appears like this:
How can I keep the text centered vertically when I have margins? Without them, the button text appears correctly.
Thanks
May be This will Help You :
<Button
android:id="#+id/notification"
style="#style/Wrap"
android:layout_margin="8dp"
android:background="#drawable/red_shape_corner"
android:drawableRight="#drawable/notification_bell"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/str_notification"
android:textColor="#android:color/white" />
You Need to use
// Nothing but Image like Arrow etc.
android:drawableRight="#drawable/notification_bell"
// Here you can put you text
android:text="#string/str_notification"
Try using android:gravity="center".
android:gravity attribute documentation
I have a Button with its width as match_parent. And I want a image/icon with a text in its center. I have triedandroid:drawableStart attribute but its of no help. I don't know what I am missing. Is there any one who is also facing the same problem.
Note:This link is not solving my problem. I am attaching my button portion of xml
<Button
android:background="#drawable/blue_button"
android:id="#+id/btn_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:drawableStart="#drawable/ic_list"
android:text="#string/btn_schedule"
android:textColor="#android:color/white" />
After adding these attribute my text is coming on center but image/icon is still on left side of button. Can any body help!!!
You might want to check my answer to this question:
Android Button with text and image
Its an ugly solution but it you want the image and text to be centered as a group in the button rather than text center and drawable padded from the side then its the only way I have found.
100% working
<FrameLayout
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:clickable="false"
android:drawableLeft="#android:drawable/ic_delete"
android:focusable="false"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="Button Challenge" />
from here
This works for me. Basically play with the paddingLeft and PaddingRight of the button to get the text and image close together to the center.
Note: Might be an issue with rather long texts.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search button"
android:drawableStart="#drawable/ic_action_search"
android:paddingLeft="60dp"
android:paddingRight="60dp"/>
</LinearLayout>
I had the same problem and I hadn't found anywhere clear solution. BUT I got some information which also help me to provide following solutions (I have chosen the second one):
Create image which will contain your image and text
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
It is not so clear and you have to add more complicated changes to your layout but it is easier to modify the text later
<FrameLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_height="50dp"
android:layout_width="match_parent" />
<TextView
android:text="sample"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:drawableLeft="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
</FrameLayout>
I tried android:drawableTop
The image shown in the center of the button