Padding in android, untidy my layout - android

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)

Related

How to remove internal Image padding (Top, Left and Bottom) of an Android Checkbox

I have a Checkbox in Android with a TextView above it:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="false"
android:layout_margin="0dp"
android:gravity="center_vertical|center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="true" />
</LinearLayout>
The problem with Checkbox however is that it's internal "Image" got an invisible border around it. How to remove this? I've tried android:padding="-5dp" but this only worked for the right side of the CheckBox for some reason, not the top, left and bottom (my main concern is the Top).
Here is a screenshot of the Checkbox in Eclipse Graphical Layout window:
LineairLayout selected:
Checkbox selected:
In the second picture you can see the Checkbox has some padding around it when you select it. I want to have this removed (especially on the top), so that the space between the Text and the Checkbox is reduced.
Here is a picture when I have padding="-5dp":
Checkbox selected:
PS: If someone knows a solution with just a Checkbox and it's Text-field, so I can removed the LineairLayout and TextView, while still having the Text above the Checkbox I would also appreciate it. A single Checkbox with a custom style and Text is better for performance than a Checkbox inside a LineairLayout with an additional TextView. (I know this is more something for a different question, but I just add it here since it doesn't have a lot of priority. Everything is working already, except for the padding of the Checkbox.)
Ok, I changed the LineairLayout to a RelativeLayout, and than I can change the margin to a minus dp. This is my end result:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/default_margin">
<TextView
android:id="#+id/tv_edit_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:singleLine="false"
android:gravity="center_horizontal"
android:text="#string/edit_checkbox" />
<CheckBox
android:id="#+id/cb_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tv_edit_checkbox"
android:layout_centerHorizontal="true"
android:layout_marginTop="-10dp"
android:checked="true" />
</RelativeLayout>
This gives the following result:
RelativeLayout selected:
This does change the space between the Text and the Checkbox. For me this is what I want, however it still doesn't explain why the Android Checkbox' internal Image got a padding at its Top, Left and Bottom sides.. Now idea who of Android had this idea, but it was a pretty bad one.. It was better to just use no padding on the internal image and then have some default internal Margin/Padding that can be changed by users..
OR
Edit :
You can also get the same result with LinearLayout :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="0dp"
android:text="For all products"
android:textSize="10sp" />
<CheckBox
android:id="#+id/checkBox_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:padding="0dp" />
</LinearLayout>

How do you remove the extra inner padding inside a Spinner button in an Android layout?

I am trying to create a Button that looks like a Spinner.
I have done this by creating a Button like this:
<Button
android:id="#+id/dateButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/dateLabel"
android:text="default text"
android:textSize="15sp"
style="?android:attr/spinnerStyle"
android:gravity="center_vertical|center_horizontal" />
The problem is that the text ("default text") inside the Button seems to have extra padding around it, which makes the Button look bloated and expanded in its height.
I thought that setting the layout_height for the button to "wrap_content" would fix this and make the button thinner, but it does not have any effect.
Does anyone know how to make this button's height wrap to the text inside it?
Here is the example code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="10dp" >
<TextView
android:id="#+id/dateLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Select date:"
android:textSize="15sp" />
<Button
android:id="#+id/dateButton"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/dateLabel"
android:text="default text"
android:textSize="15sp"
style="?android:attr/spinnerStyle"
android:gravity="center_vertical|center_horizontal" />
</RelativeLayout>
android:gravity="center_vertical|center_horizontal" instead of this you can write
android:gravity="center" but as per your requirment just remove this line.do not add any gravity and change your height size android:layout_height="40dp"
i changed your code
<Button
android:id="#+id/dateButton"
android:layout_width="150dp"
android:layout_height="35dp"
android:layout_toRightOf="#+id/dateLabel"
android:text="default text"
style="?android:attr/spinnerStyle"
/>
The padding of this button is decided by the drawable of it. You either need to change the spinner drawable (can be done at runtime) , or provide your own drawable to the given button that would make it look like a spinner, without using the default one

Positioning text within button in Android

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

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

Categories

Resources