I am trying to give my button a gradient background and stronger stroke. I have read/seen many tutorials but nothing works for me.
This is my addon .xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:shape="rectangle">
<gradient android:startColor="#25AAFF"
android:endColor="#004F81">
</gradient>
<stroke
android:width="5dp"
android:color="#000000">
</stroke>
<corners android:radius="5dp"></corners>
NOTE: The corners work fine
and Button file
<Button
android:background="#drawable/login_button"
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/login_password_input"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="#string/login_button_text"
android:textColor="#FFFFFF"
android:textStyle="bold">
</Button>
Using the Material Components Library there is no difference between <Button /> and <com.google.android.material.button.MaterialButton />. Check this post for more info.
MaterialButton ignored android:background until release 1.2.0-alpha06.
With this release you can have a gradient background using something like:
<Button
android:background="#drawable/bg_button_gradient"
app:backgroundTint="#null"
... />
Also you can apply the stroke using the app:cornerRadius, app:strokeWidth and app:strokeColor attributes:
<Button
app:cornerRadius="4dp"
app:strokeWidth="1dp"
app:strokeColor="#color/primaryLightColor"
..>
If you are using an earlier versions of the material components library you can use the AppCompatButton:
<androidx.appcompat.widget.AppCompatButton
android:background="#drawable/bg_button_gradient"
..>
Related
This question already has answers here:
Android button background is taking the primary color
(6 answers)
Closed 10 months ago.
I have set it up to where my button's style is borderless and I'm using a drawable for the button background android:background="#drawable/circle"
This is the code for the #drawable/circle file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape = "rectangle">
<corners android:radius="1000dp"/>
<solid android:color="#263238"/>
</shape>
</item>
</selector>
and that color (#26338) does not appear on the button instead the button looks like this
what it looks like in design mode
In case you're wondering heres the XML code for the button
<Button
android:id="#+id/clearBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:backgroundTint="#color/red"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
When in reality it should be more like this color
this is the color I'm looking for
Some more additional info is that my button is in a TableRow inside of a tableLayout. Also when I try applying a backgroundTint it also does not work. Am using the latest version of android studio and I'm pretty sure everything else is up to date.
How can I fix this? Thanks.
That is Button bug.
change Button
to androidx.appcompat.widget.AppCompatButton
use bellow xml for create button background.
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp"/>
<solid android:color="#263238"/>
</shape>
this code for use drawable with button.#drawable/circle is your background drawable name.
<Button
android:id="#+id/clearBTN"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:fontFamily="#font/oswald_medium"
android:text="#string/clear"
android:textColor="#FFFFFF"
android:textSize="24sp" />
I had the same problem with Buttons.
use AppCompatButton instead using Button.
package : androidx.appcompat.widget.AppCompatButton
This button is displaying a purple color and its XML code is
android:id="#+id/btnLogin"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="141dp"
android:layout_marginEnd="142dp"
android:layout_marginBottom="48dp"
android:background="#drawable/btn_login"
android:fontFamily="#font/poppins"
android:text="Login"
android:textAllCaps="false"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="#+id/tvCreateAccount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
"#drawable/btn_login" is a shape I created -
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#71D775">
</solid>
<corners android:radius="66dp">
</corners>
</shape>
And this is how it looks like,
This is because the default MaterialButton style overrides backgroundTint.
So if we are using a custom android:background, we have to make sure to null out backgroundTint. We can achieve it by any of the following way:
Add any of these line to your button XML.
app:backgroundTint="#null"
OR
app:backgroundTint="#empty"
For some reason, you need to set the app:backgroundTint to null.
Like so:
app:backgroundTint="#null"
Heres the code
<LinearLayout
android:id="#+id/back"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:background="#drawable/border_control"
android:orientation="vertical">
<Spinner
android:id="#+id/frmL"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textAlignment="center"
android:textColor="#color/black" />
and here's the <border_control>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#color/design_default_color_on_primary" android:width="1dp"/>
<corners android:radius="10dp"/>
<solid android:color="#color/design_default_color_on_primary"/>
</shape>
dark mode layout in it is the spinner
in normal mode, you can see the spinner
EDIT - so what i did was made another folder named values-night and copied the color.xml file in it and changed the colors i was using to more darker, now i detects if the system night mode is enabled it picks colors from value-night forlder
if anyone was having same problem you can also do what i did this also gives your app a dark mode.
Try to copy all from the values/themes.xml to the values-night/themes.xml. I'm sorry I can't make comments for now.
layout_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
main_activity.xml
<ImageButton
android:background="#drawable/layout_bg"
android:src="#drawable/myicon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#android:color/black"
android:minWidth="80.0dp"
android:minHeight="80.0dp"
android:padding="10px"
android:scaleType="fitCenter"
android:layout_alignParentBottom="true"
android:id="#+id/btnFoo"
android:layout_marginLeft="91.0dp"
/>
I am setting the background of my ImageButton to a layout so I can have dynamic rounded corners as you can see in this image:
However, I need to change the background color of just that button to another solid color such as red.
If I set the background color with btnFoo.SetBackgroundColor(Color.Red), then the rounded corners are gone.
I assume because it overwrote the background attribute to be a solid color value rather than the layout_bg.xml file.
How can I set the background color of this specific ImageButton and not all the ImageButtons in my main_activity that use layout_bg as the background?
You need to make another drawable with the color Red then use it :
btnFoo.setBackgroundResource(R.drawable.red_drawable);
use material button all functionality is built in already
<com.google.android.material.button.MaterialButton
android:layout_width="size"
android:layout_height="size"
android:backgroundTint="#color/colorPrimary"
android:text="Add Images"
android:textAllCaps="false"
android:textColor="#color/white"
app:cornerRadius="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="20dp"/>
You can use something like:
btnFoo.background.colorFilter =
BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
color, BlendModeCompat.SRC_ATOP)
Note: it requires androidx.core:core:1.2.0
As alternative you can use the MaterialButton :
<com.google.android.material.button.MaterialButton
android:layout_width="80dp"
android:layout_height="80dp"
android:insetTop="0dp"
android:insetBottom="0dp"
app:icon="#drawable/ic_add_24px"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="48dp"
android:backgroundTint="#color/..."
android:textColor="#FFFFFF"
app:cornerRadius="10dp"/>
In this case just use:
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
I am working on an Android app and I am going to implement a EditBox which will very like the Instagram one showing below:
Like the screen capture below, the area marked by the red line is a whole edit box (which means click anywhere, even the image, the editbox will get focus and keyboard will show up.) but only the right side of the blue line is the input text area.
Instagram Editbox http://snag.gy/CTWsK.jpg
The code I currently have is pretty trivial
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="1" >
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#drawable/rounded_edittext"
android:hint="Ho shot man"
android:inputType="textMultiLine"
android:layout_weight="1" />
</LinearLayout>
and drawable is defined as:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
I am not sure whether there is a layout cascade workaround I could do to make the editbox like the Instagram one, or if I need to extend the editbox to change its behavior? Pointers or code sample will be greatly helpful, thank you!
Try like this
<EditText
android:id="#+id/editthis"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/editbox"
android:hint="this this this"
android:padding="5dip"
android:singleLine="true"
android:textColor="#999999"
android:textSize="14dip"
android:drawableLeft="#drawable/ic_launcher" />
Drawable left the icon will be set in left side