What controls the default padding between views? - android

When I put a view on a layout, it automatically has some padding/margin on it.
See this picture:
The red rectangle show the space that the button takes. Note the empty space between the button and the red lines. That's the space in question.
What controls that space and how to I change it ?
It's messing up my lisview's button alignments!
The code for that layout:
<Button
android:text="#+id/Button01"
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="#+id/Button02"
android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

The button has a default background set by Android. If you look at Android source code, you can find that the button's background has a transparent area around it. This is done in order to make it into a nine-patch drawable. So you cannot remove that padding unless you specify your own background drawable for the button.

Related

Android button margins when background color added

I have problem with button margins (Android Studio) when I change background colors of buttons. All buttons are the same size but those added background color has no space between them. How can I fix this?
This code is for buttons for uncolored buttons
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btnAdd"
android:id="#+id/btnAdd"
android:layout_above="#+id/btnSub"
android:layout_centerHorizontal="true"/>
And I added this line for get background color on colored buttons.
android:background="#color/red"
If I delete all background attributes from middle buttons then all buttons looks same same and in sync.
These are the same buttons without this line of code:
android:background="#color/red"
What should I do to get exactly same button sizes with same margins with background color? No java code involved. Just xml codes above.

Show four button in same line without any space between them?

I want to have four button under action bar like this. But the problem is I can not remove the space between the buttons. I used negative margin but it does not look like this. So my question is how can I achieve this design?
I did so far
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sports"
android:id="#+id/sports"
android:layout_marginRight="-8dp"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Political"
android:id="#+id/political"
android:layout_toRightOf="#+id/sports"/>
and the output is
You need to add to buttons borderless style.
style="?android:attr/borderlessButtonStyle"
It is a default system style, that using in themes, to create button bars.
Without it, always will be a small margin. You can read about it here, and here, from official documentation. It allows you to create button bars, like in your screenshot.
Try this :
Put all your buttons within Horizontal linear layout and set the width and height as follows,
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.25"
You can use linear layout for view button in same line. Using layout_weight

button text not getting centered vertically in button

I am creating an Android app, and I have a button with a custom background image behind it. The background image is just a rectangle, but now the button text does not seem to be centered vertically. Here is my xml for the button:
<Button
android:id="#+id/save_to_list"
android:layout_below="#+id/info_pic"
android:text="SAVE TO LIST"
android:textColor="#android:color/white"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="260dp"
android:layout_height="35dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"></Button>
and I set the background here:
ImageView button_bg = (ImageView)findViewById(R.id.button_background);
savetolist_button.setBackgroundDrawable(button_bg.getDrawable());
Is there a where to change the position of the text within a button?
android:layout_gravity defines layout alignment inside it's parent. To align text within the button you need android:gravity, for example
<Button
...
android:gravity="center"
...>
</Button>
Your button is too short to fit the text properly. Increase the layout_height and the text should be centered.
Your text likely has some padding built into the font as part of each letter, and that is why your text is being pushed in one direction. Set the layout_height to "wrap_content" and your text should now be centered correctly.
I found it best to use a 9 patch image for a rectangular background rather than an xml drawable.

Split button text into two sections

I am trying to port my WP7 app to android.
Does anyone know how I can layout the text on a single button so that some text appears aligned left and other text appears aligned right? (See below). I need access to be able to dynamically change the percentage number on the right side using code but the text on the right is just static.
Anyone know the answer to this?
The image is here:
http://i.imgur.com/zW7YV.png
Yes you could make it two buttons.
Remove all padding and margin from between them.
Set the same background drawable.
And just ensure when the left is clicked it invokes the right's onPress method (so it looks as if they depress together).
Or wrap the buttons/imageviews/textviews in a layout and perform the onClick on that.
I would use a RelativeLayout for this.
<RelativeLayout
android:width="fill_parent"
android:height="wrap_content"
android:clickable="true"
android:background="#18a2e7"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align_parentRight="true"
android:text="0%" />
</RelativeLayout>

How can I make button text extend outside 9 patch content area?

I have a row of buttons with custom 9 patch images, and variable length text. I would like the buttons to be the same height. When the text is long enough to wrap, it expands the button size, making the button with wrapped text bigger than the others. I'm laying these buttons out in code in linear layouts. I can fix the size of the button, but then it just cuts off the bottom. How can I make the text take up more of the padding space of the button, so that the text butts up against the top line of the button?
9patch content area is just used to set a padding. If you change the button's padding you will override the one set by the 9patch.
edit
try something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button android:text="ButtonButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="0dp"
android:paddingBottom="10dp"/>
<Button android:layout_width="wrap_content" android:text="Button"
android:layout_height="match_parent"/>
<Button android:layout_width="wrap_content" android:text="Button"
android:layout_height="match_parent"/>
</LinearLayout>
this way the button will be as high as the others. Sadly, I don't know if it depends on the original button's 9patch that might have asymmetrical paddings, but I can't make the text align with that from other buttons (I think gravity is by default set to center). Maybe with your 9patch it works though. (EDIT: oh, but if you'll have two lines of text who cares about alignment)
Ultimately I was unable to find a nice way of doing this without creating a custom view class.

Categories

Resources