I have added the following button in my XML file:
<Button
style="#style/RoundedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/rounded_button"
android:text="Say Hi" />
The following is what the rounded_button background looks like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/transparent" />
<corners android:radius="50dp" />
<stroke
android:width="1dp"
android:color="#FFF" />
</shape>
The following is the RoundedButton style:
<style name="RoundedButton" parent="Base.Widget.AppCompat.Button">
<item name="android:textColor">#FFF</item>
<item name="android:background">#color/md_blue_700</item>
</style>
The following is the output:
As you can see, the background of the entire layout is blue. But for some reason, the button has a different shade of blue at either end. Why is this? How can I get it to be truly transparent and just show the background?
Related
I am trying to have two text views inside a Linear Layout with a horizontal orientation. Now I want to have the following things for each text view:
An icon at left - implemented using android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
A round border around the text view - implemented using android:background="#drawable/round_border"
Also, need that ripple effect on touching a touch item
The 3rd point is implemented in other text views using the att.
android:background="?selectableItemBackground"
android:clickable="true"
But there is already a background att. for the round border. Now my question is how to achieve that ripple effect? Is using following att. only way:
android:foreground="?selectableItemBackground"
Android shows this att. is not supported in lower api versions so worried.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/vision_mission"
android:gravity="center"
android:layout_margin="8dp"
android:padding="8dp"
android:textSize="16sp"
android:onClick="openVisionMission"
android:drawableLeft="#drawable/ic_lightbulb_outline_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/team"
android:textSize="16sp"
android:drawableLeft="#drawable/ic_people_blue_24dp"
android:background="#drawable/round_border"
android:clickable="true"
android:focusable="true"
android:padding="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:gravity="center"/>
</LinearLayout>
You can achieve ripple effect and text view rounded background at the same time using the following code.
Custom ripple drawable, it require API level 21+
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/grey">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<stroke
android:width="1dp"
android:color="#android:color/holo_blue_dark" />
</shape>
</item>
</ripple>
After that set background of your text view
android:background="#drawable/custom_ripple"
For pre lollypop ie. below API level 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorDarkGray"/>
</shape>
</item>
<item android:state_pressed="false">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreen"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorButtonGreenFocused"/>
</shape>
</item>
</selector>
It is not exactly what you are looking for.
You can achieve easily something similar with a MaterialButton with a OutlinedButton style.
Something like:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
android:text="...."
.../>
You can customize the padding between the icon and the text, the textColor, the strokeWidth, the strokeColor and also the color selector used for the app:rippleColor
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton.Icon"
app:icon="#drawable/ic_add_24px"
app:strokeColor="#color/primaryDarkColor"
app:strokeWidth="2dp"
app:iconPadding="16dp"
.../>
you can change your background drawable with selector tag
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
I am trying to make a button like this
Button Image but I cannot put the icon in front of the text. The button I made now looks like this button image now. So, the main problem is how could I get the icon to be near in front of the text?
This is my button shape code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="50dp"
/>
<solid
android:color="#00A651"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="300dp"
android:height="20dp"
/>
</shape>
This is the code I use to implement button into activity:
<Button
android:id="#+id/problem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:drawableStart="#drawable/ic_rate_review_black_24dp"
android:textColor="#FFFFFF"
android:textSize="15sp"
android:text="#string/button_problem" />
Everything worked as Muhib Pirani suggested. Thanks to him.
Button's code now looks like this:
<Button
android:id="#+id/problem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/buttonshape"
android:drawableStart="#drawable/ic_rate_review_black_24dp"
android:drawablePadding="5dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="#string/button_problem"
android:textColor="#FFFFFF"
android:textSize="15sp" />
The ButtonShape.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="50dp"/>
<solid
android:color="#00A651"
/> </shape>
remove size and padding from your buttonXml reduce your radius
and add paddingTop and paddinBottom to your
or you can also use TextView as button have some padding already assigned with it.
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="10dp"
/>
<solid
android:color="#00A651"
/>
</shape>
You can use the MaterialButton:
<MaterialButton
style="#style/Widget.MaterialComponents.Button.IconOnly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
.../>
with:
<style name="Widget.MaterialComponents.Button.IconOnly">
<item name="iconPadding">0dp</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingRight">12dp</item>
<item name="android:minWidth">12dp</item>
<item name="android:minHeight">12dp</item>
<item name="iconGravity">textStart</item>
</style>
button with rounded corner + icon +text
style="#style/Base.Widget.AppCompat.Button.Borderless"
this work for me no need of much customisation
I am trying to achieve the effect which is shown in this following image:
In this image there is an edittext with round corner and internal shadow at top. I tried a lot but not success in getting shadow inside edittext.
I searched on this topic but all examples show shadow outside edittext border. I have no idea how can I achieve this.
The button and background image is already done, The only thing which is left is edittext shadow. If someone has already done this or know how to do this please share with me. Any help whould be appreciated.
Got like this
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:centerY="0.2"
android:startColor="#FFBDBDBD"
android:centerColor="#65FFFFFF"
android:endColor="#00FFFFFF"
android:angle="270"
/>
<stroke
android:width="1dp"
android:color="#C3C3C3" />
<corners
android:radius="25dp" />
</shape>
Just create an xml file in your drawable folder name as round_corner.xml.And add this following code.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:radius="3dp"
/>
<solid
android:color="#android:color/white"/>
</shape>
At Last you add this xml in background property of your Edittext as follows:-
<EditText
android:id="#+id/ed1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/round_corner"
/>
Done..Definitely it works..
1.) Create rounded_edittext.xml file in drawable folder
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="15dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
<stroke android:width="1dip" android:color="#FF0000" />
</shape>
2.) Put below code in styles.xml file placed in values folder
<style name="largeEdittextText">
<item name="android:textAppearance">#android:style/TextAppearance.Large.Inverse</item>
<item name="android:textSize">15dp</item>
<item name="android:singleLine">true</item>
<item name="android:paddingTop">8dp</item>
<item name="android:paddingBottom">8dp</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:background">#FFB90F</item>
<item name="android:textColor">#android:color/black</item>
</style>
3.) Apply both on edittext in layout file
<EditText
android:id="#+id/userName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:hint="#string/login_userHint"
android:text="admin"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:singleLine="true"
android:textAppearance="#style/largeEdittextText"
android:background="#drawable/rounded_edittext">
</EditText>
if you want to make the border of EditText Round and curve, then just paste this code in Drawable/mystyle.xml (create this xml file) .
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:thickness="0dp"
android:shape="rectangle">
<stroke android:width="1dp"
android:color="#c8c8c8"/>
<corners android:radius="11dp" />
</shape>
Now in your EditText link this file as
android:background="#+drawable/mystyle"
I am using the following. I want to add stylish properties like color and style.
Please help
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="OK" />
paste the following code into new xml file in drawable folder and name the file draw and
and in button give background of this xml file name
<?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="#1F2120" />
<corners
android:bottomLeftRadius="6dp"
android:bottomRightRadius="6dp"
android:textColor="#android:color/white"
android:topLeftRadius="6dp"
android:topRightRadius="6dp" />
</shape>
this is how you can use this
<Button
android:id="#+id/force"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="15dp"
android:layout_weight="1"
android:background="#drawable/draw"
android:text="some txt"
android:textColor="#android:color/white"
android:textSize="20dp" />
<!-- Custom Style for bottom displaying confirm and cancel -->
<style name="Widget.GsMenuButton" parent="#style/Widget.Button">
<item name="android:layout_height">36px</item>
<item name="android:layout_width">82px</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">end</item>
<item name="background">#drawable/gs_menu_btn</item>
<item name="android:textColor">#fff</item>
</style>
This is what i have done to make my button. It change the button background color with background attribute and change the text color with android:textColor. I think you can just add these to your button attribute.
you can add images to this button by using :
android:background="#drawable/image_name"
if you want to simply add some color to this button's background, you can do it like this:
android:background="#ff0000" // color code for red color
besides, you can specify text color like this :
android:textColor="#0000ff" // color code for blue color
if you want to change your button's background on different events like while the button is foccused, pressed etc. you need to create an xml file say mybutton_style.xml indrawable folder.
// code for mybutton_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/btn_image_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/btn_image_focussed" /> <!--focused -->
<item android:drawable="#drawable/btn_image_default" /> <!-- default -->
</selector>
after that you just need to specify it as your button's background:
android:background="#drawable/mybutton_style"
copy following bg.xml file in to your drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape android:shape="rectangle">
<corners android:radius="10dp" />
<gradient
android:endColor="#fffffa"
android:startColor="#ffffff"
android:angle="270" />
<solid android:color="#00aa00" />
<padding android:bottom="10.0dip" android:left="10.0dip" android:right="10.0dip" android:top="10.0dip" />
</shape></item>
<item android:state_pressed="false"><shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#ffffff" />
<gradient
android:endColor="#fffffa"
android:startColor="#ffffff"
android:angle="270" />
<padding android:bottom="10.0dip" android:left="10.0dip" android:right="10.0dip" android:top="10.0dip" />
</shape></item>
</selector>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:background="#drawable/bg"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:text="OK" />
if you want to make your button stylish but in simple way then you can try the following:
android:background="#null"
android:typeface="serif"
if you don't want simple stuff then you can customize it as you want.
I have two questions:
1) how do I change the color of the seek bar (path) from yellow (the default color) to white. What I mean to say is, while I slide the thumb , it turns the line traversed from grey to yellow. I want track/line to either remain grey or white..Basically I want just the thumb to move with no color change in the seek bar.
2)
How to change the thumb of seekbar from rectangle to circle/sphere/round shape.
any pointers will be appreciated.
I want to complete the answer from above for the people who are new to the system,
the missing xmls ( background_fill , progress_fill and progress could look like that for a gradient red
progress.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#android:id/background"
android:drawable="#drawable/background_fill" />
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/progress_fill" />
</item>
</layer-list>
background_fill.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF555555"
android:centerColor="#FF555555"
android:endColor="#FF555555"
android:angle="90" />
<corners android:radius="5px" />
<stroke
android:width="2dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
progress_fill.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#FF470000"
android:centerColor="#FFB80000"
android:endColor="#FFFF4400"
android:angle="180" />
<corners android:radius="5px" />
<stroke
android:width="2dp"
android:color="#50999999" />
<stroke
android:width="1dp"
android:color="#70555555" />
</shape>
i did not complete the implementing for android:thumb, so the thumb will be still the original one
Therefore we just have to delete this line again from our layout xml where we define the seekbar
android:thumb="#drawable/thumb"
You should set SeekBar XML properties:
<SeekBar
....
android:progressDrawable="#drawable/progress"
android:thumb="#drawable/thumb"
....
/>
Where progress is something like this:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"
android:drawable="#drawable/background_fill" />
<item android:id="#android:id/progress">
<clip android:drawable="#drawable/progress_fill" />
</item>
</layer-list>
and thumb is
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/thumb_fill" />
</selector>
Since SeekBar uses colorAccent by default, you can create a new style with your custom color as colorAccent then use theme attribute to apply it to the SeekBar.
<SeekBar>
.
.
android:theme="#style/MySeekBarTheme"
.
</SeekBar>
in the #style:
<style name="MySeekBarTheme" parent="Widget.AppCompat.SeekBar" >
<item name="colorAccent">#ffff00</item>
</style>
seekBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
seekBar.getThumb().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
You can just add tint from API level 21.
Add these properties to seekbar element:
android:progressTint="#android:color/white"
android:thumbTint="#android:color/white"
Final result:
<SeekBar
android:id="#+id/is"
android:layout_width="match_parent"
android:max="100"
android:progressTint="#android:color/white"
android:thumbTint="#android:color/white"/>
1. Create a drawable XML:
Name it : progress_drawable
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#fff"/>
</shape>
2. Assign it to Seekbar
<SeekBar
android:id="#+id/slider_1"
android:progressDrawable="#drawable/progress_drawable"
android:layout_width="180dp"
android:layout_height="32dp"
android:layout_gravity="center"
android:rotation="270" />
Just change the accent color of your style
<style name="TickMarkSeekBar" parent="Theme.KefTheme">
<item name="tickMark">#drawable/tickmark</item>
<item name="thumbTint">#android:color/white</item>
<item name="colorAccent">#android:color/white</item>
</style>