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.
Related
I create android button programmatically, and I put this butons in a TableRow, every thing work fine, but when I change the backgroudColor the colored button not respect the size as it do without the color:
My code is:
val btn = Button(this)
btn.isEnabled = false
btn.setBackgroundColor(Color.RED)
btn.layoutParams = TableRow.LayoutParams(150, 150)
When I don't use the setBackgroundColor the size is correct.
I don't understand why the size is changes, is there any way to change only the default gray color of the button, without the size is changes.
The default background for a button is not a solid color, but is rather a graphic (e.g. a nine-patch bitmap) that has paddings and margins built into it. Thus, when you replace that background image with a color, all of the built-in paddings and margins are thrown away.
Instead of setting a background color with setBackgroundColor() you could try using setBackgroundTintList() which should tint the existing background image with your chosen color(s).
Alternatively, you would need to manually set the margins and paddings after changing the background to the solid red color, something that is much more painful to do via code than it is in a layout XML file.
Example layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Red Button"
android:backgroundTint="#a00"
android:textColor="#fff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default Button" />
</LinearLayout>
Which yields this rendering:
if your app theme is material-theme then there is a default paddings applied to the button. that's why the color fills the whole canvas of the button after you apply it. try to restore the padding (I believe it's 8dp for all edges).
Hi guys im new in android stuff and i have a problem with a button when color change, because when it changes the button grows a bit and i dont know why. My code of the button click is below.
produzirbt.setBackgroundColor(Color.rgb(38, 198, 219));
Toast.makeText(getApplicationContext(), "Avaria iniciada.", Toast.LENGTH_SHORT).show();
Xml
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="50px"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
In order to keep button size use setColorFilter() method.
In your case:
produzirbt.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.your_color), PorterDuff.Mode.SRC_ATOP);
Your button by default has a backgrond definded by some xml file. It contains backround color, corner radiuses and padding. Then in java code you change this style to plain color and padding becomes zero. To prevent size changing you should define base background with a color. Like this:
<Button
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Pausa"
android:id="#+id/Pausa"
android:textSize="32sp"
android:background="#color/my_button_color"
android:layout_above="#+id/Avaria"
android:layout_toRightOf="#+id/terminaliniciado"
android:layout_toEndOf="#+id/terminaliniciado" />
A the same time using color for button is a hacky method. It'll be better to define normal style as the documentation says.
I have a LinearLayout in an Android app I am creating now which contains a default button with gradient gray color. This LinearLayout is now white but when I try to change the background color to yellow the button also becomes yellow which I don't want to happen. I also tried to use a color image to set the background color on the LinearLayout but the same thing happens. How do I solve this problem? Here is the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:background="#color/yellow">
<Button
android:id="#+id/buttonCart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"/>
</LinearLayout>
SOLVED: Wait, it actually works now. while the buttons become colored and transparent in the layout view in Eclipse the buttons is unaffected when I run the app on the phone. I thought that it would display the same result when the app is runned. Strange how it become that way in the layout view...
In that case you may set a gray background color android:background="#A4A4A4" for button also..
Just set the background on your button separately. You can use a state list drawable, i.e. a different background for each state of the button (pressed, focused, etc) to make it compeletely custom.
Why the background color changes with the layout I'm not sure. It sounds like your button is default to transparant. Check your style and theme to make sure that's not affecting what's displayed.
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.
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.