Android: Heart Shaped small button to add item to favorite - android

I have found an answer to a question how to create heart shaped button using vector xml.
Answer is here:
https://stackoverflow.com/a/45618554/8523262
But I am not able to get it correctly implemented in my case.
I have two buttons side by side third would be this heart-Button. However, it does not fit correctly with other two buttons. I need it smaller than what original answer has it.
Here is my xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="7sp"
android:textSize="12sp"
>
<Button
android:id="#+id/buttonIA"
android:layout_width="0sp"
android:layout_weight=".3"
android:layout_height="25sp"
android:background="#drawable/buttondesign"
android:textColor="#color/white"
android:text="Hide Arabic Version"
/>
<Button
android:id="#+id/buttonIR"
android:layout_width="0sp"
android:layout_weight=".3"
android:layout_height="25sp"
android:background="#drawable/buttondesign"
android:textColor="#color/white"
android:layout_marginStart="7sp"
android:text="Hide Reference"
/>
<Button
android:id="#+id/buttonF"
android:layout_width="0sp"
android:layout_weight="0.3"
android:layout_height="25sp"
android:background="#drawable/buttondesign"
android:textColor="#color/white"
android:layout_marginStart="7sp"
android:text="Favorite it!"
/>
</LinearLayout>
Can anyone please tell me to resize it to look like a heart when placed inline with the two buttons I have already.
ButtonF is what I tried to set this vector as background of. Changed height/width too but didn't get it how i need it.
Thank you.

I use ImageButton Button instead of Button now. You could use ImageView too.
favoritebutton in below code is the vector xml that I shared link of in my questions, in case it helps anyone.
<ImageButton
android:id="#+id/buttonF"
android:layout_weight=".3"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/favoritebutton"
android:textColor="#color/white"
android:layout_marginStart="7dp"
android:background="#null"
/>

Related

Padding in android, untidy my layout

I have this layout inside a linerar layout (It is a custom Dialog), and when i add padding in the buttons, my layout become untidy.
<RadioGroup
android:id="#+id/toggleGroupEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ToggleButton
android:id="#+id/toggleButtonEntregaOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/bgOff"
android:checked="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/entrega_ok"
android:textOff="#string/entrega_ok"
android:textOn="#string/entrega_ok" />
<ToggleButton
android:id="#+id/toggleButtonEntregaRecusa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/bgOff"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/entrega_recusa"
android:textOff="#string/entrega_recusa"
android:textOn="#string/entrega_recusa" />
<ToggleButton
android:layout_margin="5dp"
android:id="#+id/toggleButtonEntregaAvaria"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/bgOff"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="#string/entrega_avaria"
android:textOff="#string/entrega_avaria"
android:textOn="#string/entrega_avaria" />
</RadioGroup>
that are the result:
if i remove android:paddingLeft="5dp" and android:paddingRight="5dp", works well, but i really wanna a padding inside the elements(I do not want the text along the edges).
Why it happens? (have alot of free space if you see compelte image)
i wanna that way, but with a lite bit more padding (in this case, have only 2dp):
Issue here is not padding, the issue here is that when you adding it last ToggleButton doesn't fit first line, and when it goest to the next one this happens. So as Der Golem said :
Try using proper casing. Or a smaller font size
The other thing, you can add
android:singleLine="true"
android:ellipsize="end"
to your last item, that way you can undestand what causes the issue in your case
EDIT : with following layout you can't reach your goal(unfortunetly)

Alternate ListItem Styles

I'm trying to make a list_item_view similar to
can anybody help me create it...
What i have tried
made separate view for the list items i.e list_item_even and 'list_item_odd'
for brevity i'll only show one of them, below is the code for list_item_even
<TextView
android:id="#+id/displaysms_lastmsgtime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/displaysms_contactname"
android:layout_below="#+id/displaysms_contactname"
android:gravity="right"
android:text="88:88"
android:textColor="#10bcc9"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/displaysms_msgtext"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageButton1"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageButton1"
android:background="#fff"
android:shadowColor="#color/textShadow"
android:shadowDx="1"
android:shadowDy="1"
android:text="hi"
android:textColor="#color/textColor"
android:textSize="16sp" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/displaysms_lastmsgtime"
android:src="#android:drawable/btn_star" />
<TextView
android:id="#+id/displaysms_contactname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp"
android:layout_toRightOf="#+id/displaysms_msgtext"
android:text="john"
android:textColor="#fff"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
and here is output
is it possible to create the yellow icon without using image and place an image inside it?
plaese help, Regards
is it possible to create the yellow icon without using image and place
an image inside it?
Maybe, but why put an unnecessary constraint on yourself? You can use a 9patch drawable for the yellow background thing if you're worried about stretching the pointed part. You can learn all about 9patch here.
i think you better off defining the layout programatically and check the row number if odd use one layout if even use a different one
if (row%2==0) use one layout
else use a different one

How do I make this button in XML?

I wanted to make an ImageButton to put in my app, but I need a very specific one.
I wanted to make an ImageButton similar to that of the one in QuickPic. Notice on the left screenshot, the Camera. See how there's an image, with a translucent gray bar at the bottom with text in it? I wanted to make something very similar to that, but with one large textview in the gray bar instead of two small ones. I am completely lost on how to do this in XML so if someone could help me, It'd be greatly appreciated. Thanks!
You mean something like this? :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageButton1"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignRight="#+id/imageButton1"
android:background="#33000000"
android:text="Image Text" />
</RelativeLayout>
Additionally, you can specify the TextView's gravity and textSize to further match your needs:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageButton1"
android:layout_alignLeft="#+id/imageButton1"
android:layout_alignRight="#+id/imageButton1"
android:background="#33000000"
android:gravity="center_horizontal"
android:text="Image Text"
android:textSize="8sp" />

Image + Text to be centre alligned in button

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

How to Create Borderless Buttons in Android [duplicate]

This question already has answers here:
How to create standard Borderless buttons (like in the design guideline mentioned)?
(19 answers)
Closed 9 years ago.
The Android Design Guidelines say to use borderless buttons (see picture below), but don't really explain how. Someone asked this same question a few weeks ago here: How to create standard Borderless buttons (like in the design guidline mentioned)? and there was an answer marked as "the" answer, but I am still lost and I don't see a way to add comments to a question that has been "closed"
The answer-er said
"Look into the theme attributes buttonBarStyle,
buttonBarButtonStyle, and borderlessButtonStyle"
but I still can't figure out how to actually use those things. I Googled around a bit and couldn't find anything so I figured I'd just ask the question again, and hopefully someone can provide a little more detail on how this works.
I thought I had this solved when I looked here a few weeks ago and noticed the answer about using a transparent background but it isn't quite good enough because it prevents the button from being highlighted when pressed.
Also, setting the style to Widget.Holo.Button.Borderless isn't appropriate because it makes the button boundaries to big.
To figure this out once and for all, I check the android source code for the standard Calendar app and found that it uses the following:
android:background="?android:attr/selectableItemBackground"
Doing it this way ensures the button is borderless and the correct size.
Look at this: http://developer.android.com/guide/topics/ui/controls/button.html#Borderless
The attribute on your Button or ImageButton tag:
style="?android:attr/borderlessButtonStyle"
If you use ActionbarSherlock...
<Button
android:id="#+id/my_button"
style="#style/Widget.Sherlock.ActionButton" />
Some days ago a stumbeled over this again.
Here my solution:
This is done in 2 steps: Setting the button background attribute to android:attr/selectableItemBackground creates you a button with feedback but no background.
android:background="?android:attr/selectableItemBackground"
The line to divide the borderless button from the rest of you layout is done by a view with the background android:attr/dividerVertical
android:background="?android:attr/dividerVertical"
For a better understanding here is a layout for a OK / Cancel borderless button combination at the bottom of your screen (like in the right picture above).
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_alignParentBottom="true">
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginLeft="4dip"
android:layout_marginRight="4dip"
android:background="?android:attr/dividerVertical"
android:layout_alignParentTop="true"/>
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dip"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dip"
android:layout_marginTop="4dip"
android:background="?android:attr/dividerVertical"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/BtnColorPickerCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:text="#android:string/cancel"
android:layout_alignParentBottom="true"/>
<Button
android:id="#+id/BtnColorPickerOk"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="?android:attr/selectableItemBackground"
android:text="#android:string/ok"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"/>
</RelativeLayout>
This code works for me:
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="?android:attr/dividerVertical" />
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:measureWithLargestChild="true"
android:orientation="horizontal"
android:paddingLeft="2dip"
android:paddingRight="2dip"
android:paddingTop="0dip" >
<Button
android:id="#+id/cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickCancel"
android:text="#string/cancel" />
<Button
android:id="#+id/info"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickInfo"
android:visibility="gone"
android:text="#string/info" />
<Button
android:id="#+id/ok"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClickSave"
android:text="#string/save" />
</LinearLayout>
I show 3 buttons at the bottom
android:background="#android:color/transparent"
<Button android:id="#+id/my_button" style="#android:style/Widget.Holo.Button.Borderless" />
You should also set margins and padding of the picture to 0.
Also look at the second, not marked answer at How to create standard Borderless buttons (like in the design guidline mentioned)?

Categories

Resources