I am trying to input an image in a regular button, however it does not show up. My code looks like this
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/wood"/>
I also hoped and included src instead of background, however it still did not work.
<Button
android:text="#string/buttonexit"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:src="#drawable/wood"/>
Lastly I also tried to make an image button, however once I added an image I cannot adjust the corners anymore.
You can use AppCompatButton instead of Button.
For a Button, the primary color defined in the app theme overrides any custom background that you want to apply to a Button, but it is not the case for the AppCompatButton.
For example:
<androidx.appcompat.widget.AppCompatButton
android:text="test"
android:layout_width="300dp"
android:layout_height="50dp"
android:id="#+id/buttonexit"
android:layout_marginLeft="50dp"
android:background="#drawable/bg"/>
Besides, you can also use ImageButton to achieve this function.
You can add an image by property android:src and adjust the corners with property android:background by creating a shape xml file.
You can refer to the following code:
1.create file button_shape.xml in folder drawable:
<?xml version="1.0" encoding="utf-8" ?>
<shape xmlns:android = "http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--background color-->
<solid android:color="#20a4e5"/>
<!--radius-->
<corners android:radius="10dip"/>
<!--Sets the width and color of the border line-->
<stroke android:width="2dp" android:color="#fffff0" />
</shape>
2.set it for android:background of ImageButton
<ImageButton
android:text="test"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_marginLeft="50dp"
android:background="#drawable/button_shape"
android:src="#drawable/grass"/>
I just updated my android Studio and every time that I try to change the background color of AppCompatButton the layout gets flat and looses the ripple effect, it wasn't like that.
And normal :
In my old project, works fine too!
<androidx.appcompat.widget.AppCompatButton
android:clickable="true"
android:onClick="Login"
android:layout_marginEnd="50dp"
android:layout_marginTop="17dp"
android:textColor="#color/primaryDarkColor"
android:layout_marginStart="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:text="Logar"
android:focusable="true" />
A good way to change your buttons background and keep the ripple effect is to create a separate drawable for them.
So you would create a default_button_background.xml with some pre-defined colors
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/button_ripple_color">
<item>
<shape android:shape="rectangle">
<corners android:radius="#dimen/button_corner_radius" />
<solid android:color="#color/buttonBackground" />
</shape>
</item>
</ripple>
Then you can assign this drawable to your button background like:
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/default_button_background"/>
I am trying to design an EditText Field having Shadows (bottom and right side) like this
tried googling & hunted many SO discussions but all are for TextView not EditText.
This is my code adding shadow to Input Text but not to TextField
<EditText android:id="#+id/txtpin"
android:maxLength="4"
android:layout_marginLeft="10dp"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:inputType="textPassword"
android:longClickable="false"
android:layout_width="160dp"
android:shadowColor="#color/Black"
android:shadowDx="1.2"
android:shadowDy="1.2"
android:shadowRadius="1.5"
android:background="#color/White">
<requestFocus></requestFocus>
</EditText>
I guess it needs some custom xml view in drawable but not getting exact idea.
What will be the logic to achieve this.
Any help would be appreciated.
Well.. #Shalini's answer helped me in this way but still I got another way to achieve 2D shadow with EditText Field and I am going to share with you.
We need to create custom XML view with three layer for EditText,
bottom shadow and right side shadow
Below is my code.
res/drawable/edittext_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- most important is order of layers -->
<!-- Bottom right side 2dp Shadow -->
<item >
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- Bottom 2dp Shadow -->
<item>
<shape android:shape="rectangle">
<solid android:color="#000000" />
</shape>
</item>
<!-- White Top color -->
<item android:bottom="3px" android:right="3px">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>
Now we can set this shadow view to our TextField using "Background" property
like this
res/layout/main.xml
<EditText android:layout_width="wrap_content"
android:id="#+id/txtpin"
android:maxLength="4"
android:layout_height="37dp"
android:gravity="center_horizontal"
android:longClickable="false"
android:padding="2dp"
android:inputType="textPassword|number"
android:password="true"
android:background="#drawable/edittext_shadow"
android:layout_weight="0.98"
android:layout_marginLeft="15dp">
<requestFocus></requestFocus>
</EditText>
and the result screen is like I have posted in question above.
Thanks to SO, sharing knowledge.
This works for me..
<EditText
android:layout_width="fill_parent"
android:shadowRadius="2"
android:shadowColor="#0000ff"
android:shadowDx="2"
android:shadowDy="4"
android:id="#+id/EditText01"
android:layout_height="wrap_content" />
Hope it helps:)
From Shadow Effect for a Text in Android?, perhaps you'd consider using
android:shadowColor,
android:shadowDx,
android:shadowDy,
android:shadowRadius;
Alternatively:
setShadowLayer()
I want to create custom button and I need it to be circle. How can I create a circle button?
I do not think that be possible with draw9patch.
Also I do not know how to make custom button!
Do you have any suggestion?
Use xml drawable like this:
Save the following contents as round_button.xml in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="#c20586"/>
</shape>
</item>
</selector>
Android Material Effect: Although FloatingActionButton is a better option, If you want to do it using xml selector, create a folder drawable-v21 in res and save another round_button.xml there with following xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#c20586">
<item>
<shape android:shape="oval">
<solid android:color="#fa09ad"/>
</shape>
</item>
</ripple>
And set it as background of Button in xml like this:
<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="hello"
android:textColor="#fff" />
Important:
If you want it to show all these states (enabled, disabled, highlighted etc), you will use selector as described here.
You've to keep both files in order to make the drawable backward-compatible. Otherwise, you'll face weird exceptions in previous android version.
Markushi wrote a circle button widget with amazing effects. Click here!
With the official Material Components library you can use the MaterialButton applying a Widget.MaterialComponents.Button.Icon style.
Something like:
<com.google.android.material.button.MaterialButton
android:layout_width="48dp"
android:layout_height="48dp"
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/ic_add"
app:iconSize="24dp"
app:iconPadding="0dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MyApp.Button.Rounded"
/>
Currently the app:iconPadding="0dp",android:insetLeft,android:insetTop,android:insetRight,android:insetBottom attributes are needed to center the icon on the button avoiding extra padding space.
Use the app:shapeAppearanceOverlay attribute to get rounded corners. In this case you will have a circle.
<style name="ShapeAppearanceOverlay.MyApp.Button.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
The final result:
With jetpack compose you can use:
Button(
onClick = { /* Do something! */ },
modifier = Modifier.width(48.dp).height(48.dp),
shape = CircleShape
) {
Icon(Icons.Filled.Add, "")
}
AngryTool for custom android button
You can make any kind of custom android button with this tool site...
i make circle and square button with round corner with this toolsite..
Visit it may be i will help you
For a FAB looking button this style on a MaterialButton:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.ExtendedFloatingActionButton"
app:cornerRadius="28dp"
android:layout_width="56dp"
android:layout_height="56dp"
android:text="1" />
Result:
If you change the size be careful to use half of the button size as app:cornerRadius.
You can use MaterialButton from AndroidX material library
<com.google.android.material.button.MaterialButton
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_margin="10dp"
android:insetLeft="0dp"
android:insetTop="0dp"
android:insetRight="0dp"
android:insetBottom="0dp"
app:cornerRadius="50dp"
app:icon="#drawable/ic_camera"
app:iconGravity="textStart"
app:iconPadding="0dp"
app:iconSize="35dp" />
and it will be like this
if you want use VectorDrawable and ConstraintLayout
<FrameLayout
android:id="#+id/ok_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:foreground="?attr/selectableItemBackgroundBorderless"
android:background="#drawable/circle_button">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/icon_of_button"
android:layout_width="32dp"
android:layout_height="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:srcCompat="#drawable/ic_thumbs_up"/>
<TextView
android:id="#+id/text_of_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/icon_of_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="5dp"
android:textColor="#android:color/white"
android:text="ok"
/>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
circle background: circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#41ba7a" />
<stroke
android:width="2dip"
android:color="#03ae3c" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
Unfortunately using an XML drawable and overriding the background means you have to explicitly set the colour instead of being able to use the app style colours.
Rather than hardcode the button colours for every behaviour I opted to hardcode the corner radius, which feels marginally less hacky and retains all the default button behaviour (changing colour when it's pressed and other visual effects) and uses the app style colours by default:
Set android:layout_height and android:layout_width to the same value
Set app:cornerRadius to half of the height/width
(It actually appears that anything greater than or equal to half of the height/width works, so to avoid having to change the radius every time you update the height/width, you could instead set it to a very high value such as 1000dp, the risk being it could break if this behaviour ever changes.)
Set android:insetBottom and android:insetTop to 0dp to get a perfect circle
For example:
<Button
android:insetBottom="0dp"
android:insetTop="0dp"
android:layout_height="150dp"
android:layout_width="150dp"
app:cornerRadius="75dp"
/>
here is how you can perform simply, make a drawable resource file in drawable.xml. Say round_button.xml and then paste the following code.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="oval">
<solid
android:color="#color/button_start_gradient_color"/>
</shape>
</item>
<item
android:drawable="#drawable/microphone"/>
</layer-list>
Note:- use your own color and drawable resource as i have used #drawable/microphone
Following is the result
[1]: https://i.stack.imgur.com/QyhdJ.png
If you want to do with ImageButton, use the following. It will create round ImageButton with material ripples.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_settings_6"
android:background="?selectableItemBackgroundBorderless"
android:padding="10dp"
/>
Create a new vector asset in the drawable folder.
You can import your PNG image as well, and convert the file to SVG online at https://image.online-convert.com/convert-to-svg. The higher the resolution, the better the conversion will be.
Next, create a new vector asset from that SVG file.
This is a sample vector circle image you can use. Copy the code to an xml file in the drawables folder.
ic_check.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="256"
android:viewportWidth="256">
<path
android:fillColor="#2962FF"
android:pathData="M111,1.7c-7.2,1.1 -22.2,4.8 -27.9,7 -33.2,12.5 -61.3,40.3 -74.1,73.3 -8.7,22.6 -10.5,55.3 -4.4,78 10.9,40 39.7,72.4 77.4,87 22.6,8.7 55.3,10.5 78,4.4 45.3,-12.3 79.1,-46.1 91.4,-91.4 2.9,-10.7 3.9,-21.9 3.3,-37.4 -0.7,-21.2 -4.6,-35.9 -14,-54.1 -18.2,-35 -54,-60.5 -93.4,-66.4 -6.7,-1 -30.7,-1.3 -36.3,-0.4zM145,23.1c21.8,3.3 46.5,16.5 61.1,32.8 20.4,22.6 30.1,51.2 27.7,81.1 -3.5,44.4 -35.9,82.7 -79.6,94 -21.6,5.6 -46.6,3.7 -67.8,-5.1 -10.4,-4.3 -24.7,-14.1 -33.4,-22.9 -41.6,-41.5 -41.6,-108.4 0,-150 24.3,-24.3 57.6,-35.1 92,-29.9z"
android:strokeColor="#00000000" />
<path
android:fillColor="#2962FF"
android:pathData="M148.4,113c-24.6,26 -43.3,44.9 -44,44.6 -0.7,-0.3 -8.5,-6.1 -17.3,-13 -8.9,-6.9 -16.5,-12.6 -17,-12.6 -1.4,-0 -25.6,19 -25.8,20.3 -0.3,1.4 62.7,50.2 64.8,50.2 1.7,-0 108.4,-112.3 108.4,-114.1 0,-1.3 -23.8,-20.4 -25.4,-20.4 -0.6,-0 -20.2,20.3 -43.7,45z"
android:strokeColor="#00000000" />
</vector>
Use this image in your button:
<ImageButton
android:id="#+id/btn_level1"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="#drawable/ic_check"
/>
Your button will be a circle button.
How do I add a border to a button? Is it possible to do this without resorting to use of images?
Step 1 : Create file named : my_button_bg.xml
Step 2 : Place this file in res/drawables.xml
Step 3 : Insert below code
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#000000" />
</shape>
Step 4: Use code "android:background="#drawable/my_button_bg" where needed eg below:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Text"
android:background="#drawable/my_button_bg"
/>
• Android Official Solution
Since Android Design Support v28 was introduced, it's easy to create a bordered button using MaterialButton. This class supplies updated Material styles for the button in the constructor. Using app:strokeColor and app:strokeWidth you can create a custom border as following:
1. When you use androidx:
build.gradle
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
}
• Bordered Button:
<com.google.android.material.button.MaterialButton
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MATERIAL BUTTON"
android:textSize="15sp"
app:strokeColor="#color/green"
app:strokeWidth="2dp" />
• Unfilled Bordered Button:
<com.google.android.material.button.MaterialButton
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UNFILLED MATERIAL BUTTON"
android:textColor="#color/green"
android:textSize="15sp"
app:backgroundTint="#android:color/transparent"
app:cornerRadius="8dp"
app:rippleColor="#33AAAAAA"
app:strokeColor="#color/green"
app:strokeWidth="2dp" />
2. When you use appcompat:
build.gradle
dependencies {
implementation 'com.android.support:design:28.0.0'
}
style.xml
Ensure your application theme inherits from Theme.MaterialComponents instead of Theme.AppCompat.
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
• Bordered Button:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MATERIAL BUTTON"
android:textSize="15sp"
app:strokeColor="#color/green"
app:strokeWidth="2dp" />
• Unfilled Bordered Button:
<android.support.design.button.MaterialButton
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UNFILLED MATERIAL BUTTON"
android:textColor="#color/green"
android:textSize="15sp"
app:backgroundTint="#android:color/transparent"
app:cornerRadius="8dp"
app:rippleColor="#33AAAAAA"
app:strokeColor="#color/green"
app:strokeWidth="2dp" />
Visual Result
Create a button_border.xml file in your drawable folder.
res/drawable/button_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#FFDA8200" />
<stroke
android:width="3dp"
android:color="#FFFF4917" />
</shape>
And add button to your XML activity layout and set background android:background="#drawable/button_border".
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_border"
android:text="Button Border" />
create drawable/button_green.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#003000"
android:centerColor="#006000"
android:endColor="#003000"
android:angle="270" />
<corners android:radius="5dp" />
<stroke android:width="2px" android:color="#007000" />
</shape>
and point it out as #drawable/button_green:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/button_green"
android:text="Button" />
Please look here about creating a shape drawable
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
Once you have done this, in the XML for your button set android:background="#drawable/your_button_border"
If your button does not require a transparent background, then you can create an illusion of a border using a Frame Layout. Just adjust the FrameLayout's "padding" attribute to change the thickness of the border.
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="1sp"
android:background="#000000">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your text goes here"
android:background="#color/white"
android:textColor="#color/black"
android:padding="10sp"
/>
</FrameLayout>
I'm not sure if the shape xml files have dynamically-editable border colors. But I do know that with this solution, you can dynamically change the color of the border by setting the FrameLayout background.
In your XML layout:
<Button
android:id="#+id/cancelskill"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_weight="1"
android:background="#drawable/button_border"
android:padding="10dp"
android:text="Cancel"
android:textAllCaps="false"
android:textColor="#ffffff"
android:textSize="20dp" />
In the drawable folder, create a file for the button's border style:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#f43f10" />
</shape>
And in your Activity:
GradientDrawable gd1 = new GradientDrawable();
gd1.setColor(0xFFF43F10); // Changes this drawbale to use a single color instead of a gradient
gd1.setCornerRadius(5);
gd1.setStroke(1, 0xFFF43F10);
cancelskill.setBackgroundDrawable(gd1);
cancelskill.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
cancelskill.setBackgroundColor(Color.parseColor("#ffffff"));
cancelskill.setTextColor(Color.parseColor("#f43f10"));
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFFFFFFFF); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFFF43F10);
cancelskill.setBackgroundDrawable(gd);
finish();
}
});
I know its about a year late, but you can also create a 9 path image
There's a tool that comes with android SDK which helps in creating such image
See this link: http://developer.android.com/tools/help/draw9patch.html
PS: the image can be infinitely scaled as well
<com.google.android.material.button.MaterialButton
android:id="#+id/addBtn"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="150dp"
android:layout_height="80dp"
android:gravity="center"
android:backgroundTint="#android:color/transparent"
android:textColor="#color/blue"
app:cornerRadius="8dp"
app:strokeColor="#color/blue"
app:strokeWidth="2dp"/>
With the Material components library just use a MaterialButton with the Widget.MaterialComponents.Button.OutlinedButton style.
You can customize color and width with the strokeColor and strokeWidth attributes
<com.google.android.material.button.MaterialButton
....
style="?attr/materialButtonOutlinedStyle"
app:strokeColor="#color/colorPrimary"/>
With Jetpack compose use an OutlinedButton.
Use the border attribute to customize width and color.
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
) {
Text(text = "BORDER")
}
In your drawable folder create a drawable file called gradient_btn
and paste the code below
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#7BF8C6"
android:centerColor="#9DECAD"
android:endColor="#7BF8C6"
android:angle="270" />
<corners
android:topLeftRadius="15dp"
android:topRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
/>
<stroke android:width="3px" android:color="#000000" />
</shape>
Then in your Button code in xml call the file you created:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#drawable/gradient_btn"/>
Output - Will be a button with gradient and border.
Note - You can change the Hexa decimal codes of the buttons as you wish and also you can change the stroke width.