I want to build a button with 3 different color in the background like the attached image, and I have no idea how to do that with shapes
Thank you for you help.
Use ImageButton and use above image as background of that button:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/YOUR_BTN_IMG"/>
1st make a xml file in drawable folder with bg1 name and put this code
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="90"
android:centerColor="#4CAF50"
android:endColor="#2E7D32"
android:startColor="#b5e7b7"
android:type="linear" />
</shape>
than where ever you want it use like
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bg1"/>
here u change angle,start end and center color as per your requirement
Related
I want to make a gradient button in an Android app that I'm developing in Kotlin. I have made the drawable file for the gradient background but when I do give the button the customized background it doesn't work, nothing changes. This is the code for the gradient background and below that is the XML code for the button.
<item>
<shape android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
</item>
<com.google.android.material.button.MaterialButton
android:layout_width="match_parent"
android:layout_height="55dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo"
android:layout_marginHorizontal="26dp"
tools:layout_editor_absoluteX="16dp"
app:cornerRadius="10dp"
android:textSize="20sp"
android:text="Create an account"
android:background="#drawable/create_acc_btn"
android:fontFamily="#font/montserrat" />
You just have to fix your gradient drawable file.
remove the item tag from it and put android xml namespace declaration like this :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<gradient android:startColor="#color/start_color"
android:endColor="#color/end_color"/>
<corners android:radius="10dp"/>
</shape>
And add this line to your button xml code :
app:backgroundTint="#null"
without this your gradient background won't show up
Note : MaterialButton manages its own background to control elevation, shape, color and states. Consider using backgroundTint, shapeAppearance and other attributes where available. A custom background will ignore these attributes and you should consider handling interaction states such as pressed, focused and disabled
You are also going to lose ripple effect when using custom background.
I am trying to create a capsule/pill shaped button in XML. I have defined a drawable and set it as the background of my button, but in the preview, and when I run the app, it's displaying as a blue rectangle, despite the background drawable being a white oval. Does anyone know why that might be happening?
Here's the button:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/search_box"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_capsule"
android:text="#string/search"
android:textColor="#color/precipLightBlue"/>
And here's the background drawable:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
To have a capsule/pill shaped button you can use the MaterialButton in the official Material Component library using the app:cornerRadius attribute.
<com.google.android.material.button.MaterialButton
android:layout_width="100dp"
app:cornerRadius="32dp"
..../>
With the version 1.1.0 you can also customize the shape of your component using the app:shapeAppearanceOverlay attribute
<com.google.android.material.button.MaterialButton
....
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.ButtonRounded"
.../>
In the style define:
<style name="ShapeAppearanceOverlay.ButtonRounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">32dp</item>
</style>
You can also try to use the ExtendedFloatingActionButton:
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/exfab"
android:layout_width="wrap_content"
.... />
You put a shape that you didn't defined. To define an oval you should put the shape="oval" option in the tag. Although i think you want a rectangle with rounded corners as i see in your code.
Also 1000dp radius is a lot, maybe that's making an issue.
Define size too. Because that shape doesn't have any size and may not appear as you are using wrap_content in the button definition
Try this:
<corners android:radius="30dp" />
<solid android:color="#android:color/white" />
<size
android:width="200dp"
android:height="50dp"/>
If you want an oval remove the corners tag and add android:shape="oval" as property of shape tag
Here is the code regarding button drawable background.
Xml code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center|top"
android:background="#color/colorLightPurple">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:background="#drawable/button_bg"
android:text="Search"
android:textColor="#color/colorBlue"/>
</LinearLayout>
For button background , please add same drawable file
seems like you're doing everything right. Just a small addition, try adding android:shape="rectangle" in the shape tag.
This is what button_capsule.xml should look like.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="1000dp" />
<solid android:color="#android:color/white" />
</shape>
Hope this works. All the best.
In Material design version 1.2.0 they have fixed this issue and added a property for setting background for Material Button
here is the dependency for the latest version
com.google.android.material:material:1.2.0
and also if u want to remove the space between the drawable top and bottom so that the drawable get the whole width and height use these 2 properties
android:insetTop="0dp"
android:insetBottom="0dp"
if want to explore more about the properties u can refer to the release of library and can check all the properties.
https://github.com/material-components/material-components-android/releases/tag/1.2.0
use this.
<androidx.appcompat.widget.AppCompatButton
.....
/>
What I want to do:
What I have:
I want to make a circular icon with initials in it. In my scenario initials can have one or two letters. Like you can see my solution work fine for two letters but not to one. How can I solve this for both cases?
There is my code from the XML file:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="20dp"
android:textAllCaps="true"
android:textColor="#color/white"
android:background="#drawable/shape_rounded_blue_button"
android:layout_gravity="center_horizontal"
android:gravity="center"
local:MvxBind="Text Initials"/>
And there is shape_rounded_blue_button:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/blue"/>
<corners android:radius="30dp" />
</shape>
As I wanted to implement the same thing, I made some research on GitHub and found this library:
https://github.com/amulyakhare/TextDrawable
It does exactly what you want and has some neat features as auto-coloring. You can use it with an ImageView and create a drawable, e.g. like this:
val drawable = TextDrawable.builder()
.beginConfig()
.width(bubbleSize)
.height(bubbleSize)
.endConfig()
.buildRound("FH", MATERIAL.getColor("FH"))
theImageView.setImageDrawable(drawable)
And that looks like this:
Your text will automatically be centered within the bubble, so it's no problem to have one or two characters.
You need to create a drawable resource file under res>drawable and save it as bg_circle.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#424242"/>
<size android:width="120dp" android:height="120dp"/>
</shape>
Now make icon_container in your layout.xml file as:
<RelativeLayout
android:id="#+id/icon_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/icon_width_height"
android:layout_height="#dimen/icon_width_height"
android:src="#drawable/bg_circle" />
<TextView
android:id="#+id/icon_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#android:color/white"
android:textSize="#dimen/icon_text" />
</RelativeLayout>
For complete code implementation visit Implementing Gmail like icon with text and random colors
You need to create a circle shape.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#color/base30"/>
For better effects(shadow and appearance) you can use TextDrawable.
I am gonna to create the a alert dialog box in android with rounded shape.
I follow many threads in this site. Create a shape.xml and place it drawables
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="5dp" android:color="#FF0000" />
<corners android:bottomRightRadius="20dp" android:bottomLeftRadius="20dp"
android:topLeftRadius="20dp" android:topRightRadius="20dp"/> <solid android:color="#FFFF00"/>
And apply the above shape to the linearlayout.
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/transparent" >
<LinearLayout android:layout_width="200dip"
android:layout_height="60dip"
android:orientation="vertical"
android:background="#drawable/shape">
</LinearLayout>
However, there is still have a rectangle border and back color outside the dialog box. That is not what I want.
I have search for a long time I dont know how to fix it.
I aimed to make it transparent outside the corner of the dialog box layout. Thank you so much!
Screen Shot: http://postimg.org/image/3xbnmquyt/
For the shape you want I found the default one on Android Studio, this:
android:background="#drawable/abc_menu_dropdown_panel_holo_light"
Apply it to your LinearLayout, It is exatcly what you're talikng about and you don't need to create other xml files in the drawables. To see how It looks like see my answer here:
Android View shadow
Hope I helped you.
Remove the stroke tag because it is used for creating the border. Also you shouldn't use gradient and solid together because they exclude one another.
http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
I'm playing a bit with Android developing some sample applications and came across issue I can't get over with. I'd like to change width of "bar" of SeekBar view.
What I want could be better seen on screenshot provided
http://i.stack.imgur.com/XczAf.png
Could some please be so kind and help me out?
Thanks a lot in advance
You can use thumb drawable for to change hight and and thumb image
Use below line in seekbar xml file
android:thumb="#drawable/seekbar_thumb_draw"
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:thumb="#drawable/seekbar_thumb_draw" />
//seekbar_thumb_draw.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape>
<size
android:height="40dp"
android:width="40dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item android:drawable="#drawable/seekbar_thumb_image"/>
seekbar_thumb_image is image for thumb that u want
Photshop the exact size that you want. Then save it in your drawables folder. Then reference it in the xml code for your seekbar.
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="wrap_contentt"
android:layout_height="wrap_content"
android:thumb="#drawable/seek_thumb_verywidebar" />