center image inside button - android

i have this code for displaying an image on a button.
How to position the image so that it doesn't touch the frame/border?
this the code:
<Button
android:layout_width="0dp"
android:layout_weight="0.45"
android:onClick="EXIT"
android:layout_height="95dp"
android:text="Exit"
android:textSize="22sp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:background="#drawable/buttonnew"
android:textStyle="bold"
android:drawableTop="#drawable/zexit"
android:textColor="#000000"
android:layout_gravity="bottom"
/>

Since you are referring to the Button rather than the drawable, it would be best to use padding instead of margin, e.g. android:paddingTop

Related

how to align the drawable to left of text in textview?

I want to display a drawable and text in a textview. When I use below code it is displaying both but drawable is in left most side of textview.
<app.com.myapp.views.TextViewMedium
android:id="#+id/approve_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#color/color_5eba6f"
android:gravity="center"
android:drawableLeft="#drawable/approve"
android:text="#string/notification_approve"
android:textColor="#color/color_FFFFFF"
android:textSize="#dimen/dp_14" />
My text is aligned in center. I want to align drawable to left of that text.Gap between drawable and text should be just 6 dp. Is it possible to do? Or do I need to create a layout with imageview and textview to achieve the same?
Use these tags to set the image in your textView.
android:drawableLeft="#drawable/your_image"
android:drawablePadding="6dp"
Hope this helps!
This can be possible way :
<TextView
android:id="#+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_weight="1"
android:background="#drawable/edittext"
android:drawableLeft="#mipmap/icon_username"
android:drawablePadding="6dp"
android:gravity="fill_horizontal|fill|left"
android:padding="5dp"
android:singleLine="true"
android:textSize="15dp"
android:windowSoftInputMode="stateAlwaysHidden" />
Try this
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sample Text"
android:id="#+id/handling"
android:textSize="20dp"
android:drawableLeft="#drawable/emoticons_bear"
android:drawablePadding="6dp"/>
You need to use drawableLeft attribute as below:
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/userFname"
android:drawableLeft="#drawable/user_name"
android:drawablePadding="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="16sp"
android:textColor="#android:color/white"
/>
to do that you must use relative layout like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!--Your text view-->
<TextView
android:id="#+id/myText"/>
<!--Your image view-->
<ImageView
android:layout_toLeftOf="#+id/myText"
android:padding_right="6dp"/>
</RlativeLayout>
with that code you ImageView is forced to be at the left of your TextView and by 6dp spacing
Don't forget you must use relativeLayout as a parent layout so you can use toLeftOf
This is possible way to do it
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:id="#+id/yourTextView"
android:textSize="12sp"
android:drawableLeft="#drawable/yourDrawable"
android:drawablePadding="6dp"/>
If not satisfies your demands you must use separate ImageView and TextView i think.. Hope to help you!

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

how do I make an image fit in my button

each time I add and image to my button (want to add it below the text) it only shows the top left of the image and adds itself at the top of the button even when I use android:drawableBottom .
here is my XML code :
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="#string/weather_station"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/weather"
android:paddingBottom="120dp"
android:paddingLeft="5dp"
android:typeface="monospace"
android:id="#+id/weather_station" />
android:drawableBottom="#drawable/weatherman"
/>
use ImageButton instead of using Button and try to scale the image`
<ImageButton
android:id="#+id/my_image_button"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/my_img" />
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="bUTTON"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/shape1"
android:paddingBottom="120dp"
android:paddingLeft="5dp"
android:drawableBottom="#drawable/cast_ic_notification_2"
android:typeface="monospace"
android:id="#+id/weather_station" />
how cant it possible even your drawable line out of scop ? try above WORKING CODE
Try setting up the below scale type in the xml for fill image in button.
android:scaleType="scaleXY"
So put this attribute in your xml code.
<Button
android:layout_width="162dp"
android:layout_height="200dp"
android:text="#string/weather_station"
android:textStyle="bold"
android:textSize="18dp"
android:background="#drawable/weather"
android:paddingBottom="120dp"
android:scaleType="scaleXY"
android:paddingLeft="5dp"
android:typeface="monospace"
android:id="#+id/weather_station"
android:drawableBottom="#drawable/weatherman"/>

Align text in TextView with drawableTop

I have this TextView:
<TextView
android:id="#+id/silverCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:contentDescription="silver"
android:drawableTop="#drawable/silver"
android:text="3" />
Which looks like this:
How can I center the 3 inside the View to be situated directly below the drawable I set with android:drawableTop?
You need to use gravity instead of layout_gravity:
<TextView
android:id="#+id/silverCount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:contentDescription="silver"
android:drawableTop="#drawable/silver"
android:text="3" />
This will center the 3 inside the TextView to appear directly below the drawable like this:

how do I align text and image in same button

How do I align text and image with same button? I want to do like this image:
but my button looks like this, with text overlapping the image:
This is my code below. How do I make space in between text and image in the same button?
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_weight="0"
android:weightSum="1"
android:orientation="horizontal" >
<Button android:id="#+id/btnUtilitybillpayments"
style="?android:attr/buttonStyleSmall"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:drawableLeft="#drawable/handshake"
android:layout_marginRight="55dip"
android:layout_marginLeft="55dip"
android:drawablePadding="-25dp"
android:paddingLeft="10dip"
android:paddingRight="50dip"
android:layout_marginTop="10dip"
android:background="#drawable/curvedplanebutton"
android:textColor="#drawable/button_text_color"
android:text="Utility Bills Payment"/>
</LinearLayout>
try this
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#android:drawable/btn_star_big_on"
android:text="Drawable left">
Button>
For more info go to this link
http://izvornikod.com/Blog/tabid/82/EntryId/8/Creating-Android-button-with-image-and-text-using-relative-layout.aspx
I think you made your button to Complex,
Just use below code and let me know what happen..
<Button android:id="#+id/btnUtilitybillpayments"
style="?android:attr/buttonStyleSmall"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="55dip"
android:layout_marginLeft="55dip"
android:drawableLeft="#drawable/handshake"
android:layout_marginTop="10dip"
android:gravity="left"
android:paddingLeft="20dip"
android:background="#drawable/curvedplanebutton"
android:textColor="#drawable/button_text_color"
android:text="Utility Bills Payment"/>
As I have also doubt on layout_margin attributes of your Button. If it doesn't work then try to remove
android:layout_marginRight="55dip"
android:layout_marginLeft="55dip"
Some thing like this.
<Button
android:id="#+id/btnUtilitybillpayments"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/curvedplanebutton"
android:drawableLeft="#drawable/handshake"
android:gravity="left|center"
android:text="Utility Bills Payment"
android:textColor="#drawable/button_text_color" />
A more suitable approach is by making the button as a 9patch image. That way, you can define where the text should be by patching the right and bottom side. And there wont be any need for such complex button.
You can go here for more information-
http://developer.android.com/tools/help/draw9patch.html

Categories

Resources