I want to customize my searchview edittext which expanded when I click on searchview icon to be like this image
And I tried the custom style to do that but I got this result in this image
The problems:
There is a blue vertical bar at the start of the edittext I need to remove it and make it appears only on a search query and after the querytext also I want to change the color of this vertical bar
I need to add a white search icon with specific padding of the hint and text query currently the white icon in the result screen disappears on search query
How I got this result?
I define searchViewStyle
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="searchIcon">#drawable/ic_baseline_dark_search_24</item>
<item name="closeIcon">#null</item>
<item name="goIcon">#drawable/ic_baseline_dark_search_24</item>
<item name="commitIcon">#drawable/ic_baseline_dark_search_24</item>
<item name="android:editTextColor">#color/white</item>
<item name="android:textColorHint">#color/white</item>
<item name="android:textColorHighlight">#color/red</item>
<item name="queryBackground">#drawable/radius_drawable</item>
<item name="searchHintIcon">#drawable/ic_baseline_search_24</item>
<item name="queryHint">"search_hint"</item>
</style>
I define searchview like this in my fragment layout
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
style="#style/SearchViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.appcompat.widget.SearchView>
I define a custom drawable background for searchview
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="18dp"
/>
<solid
android:color="#4F4F4F"
/>
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"
/>
<size
android:width="270dp"
android:height="60dp"
/>
</shape>
While I do not clearly understand what your requirements are exactly, I would like to point out one thing though.
For Applying a style to a SearchView we have to use the theme attribute, your code seems okay, maybe your changes are not being applied for now I believe. So try using the android:theme attribute for your style as follows. The rest of your requirements should easily come by to you with some trial and error I believe. Thanks.
<androidx.appcompat.widget.SearchView
android:id="#+id/searchView"
android:theme="#style/SearchViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
Here is how I would recommend you to update your style to get as close as possible to your design.
<style name="SearchViewStyle" parent="Widget.AppCompat.SearchView">
<item name="searchIcon">#drawable/ic_baseline_dark_search_24</item>
<item name="queryBackground">#android:color/transparent</item>
<item name="queryHint">"search_hint"</item>
<item name="iconifiedByDefault">false</item>
<item name="android:textColorHint">#color/white</item>
<item name="android:background">#drawable/radius_drawable</item>
</style>
And you can modify your drawable and get rid of the padding and size since you are using wrap_content in your layout
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#4F4F4F" />
<corners android:radius="18dp" />
</shape>
You can use custom searchview in AppCompat SearchView . Copy searchview from this link searchview.xml
Then edit the xml or remove the extra margin from xml.
Add custom searchview layout like this:
<androidx.appcompat.widget.SearchView
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:gravity="center"
app:layout="#layout/custom_search_view"
app:iconifiedByDefault="true"
app:queryBackground="#android:color/transparent"
app:queryHint="Search"
app:searchIcon="#drawable/search" />
I am using TextInputLayout around edittext to display hint and error. Hint is working perfectly fine as expected, but on using setError on TextInputLayout, the whole edittext is getting foreground colored. I just want the bottom highlight line to be colored not the whole box. The current behaviour is as below:
Code on how I used it in layout is as below:
<android.support.design.widget.TextInputLayout
android:id="#+id/inputLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/TextLabel">
<EditText
android:id="#+id/etLoginEmail"
style="#style/EditTextStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/text_email_address"
android:imeOptions="actionNext"
android:inputType="textEmailAddress" />
</android.support.design.widget.TextInputLayout>
The Style which is been used is as below:
<style name="TextLabel" parent="Widget.Design.TextInputLayout">
<item name="android:textColorHint">#color/COLOR</item>
<item name="android:textSize">#dimen/font_larger</item>
<item name="colorAccent">#color/COLOR</item>
<item name="colorControlNormal">#color/COLOR</item>
<item name="colorControlActivated">#color/COLOR</item>
</style>
EditTextStyle:
<style name="EditTextStyle">
<item name="android:textSize">#dimen/font_normal</item>
<item name="android:textColor">#color/grey_text</item>
<item name="android:fontFamily">#font/gotham_book</item>
<item name="android:paddingBottom">#dimen/spacing_normal</item>
<item name="android:paddingTop">#dimen/spacing_normal</item>
<item name="android:background">#drawable/edittext_background</item>
</style>
edittext_background.xml
<?xml version="1.0" encoding="utf-8"?>
<item>
<shape>
<solid android:color="#color/grey_edittext_line" />
</shape>
</item>
<item android:bottom="1dp">
<shape android:shape="rectangle">
<solid android:color="#FFFFFF" />
</shape>
</item>
How can I remove this foreground color and get the color only on 1dp of highlighted line as in password?
I have a cardview. The button default color is grey. I want it to look like the right side image(like the blessings). This right one has been created setting backgroundtint to white but in older versions its still shows grey.
<android.support.v7.widget.CardView
android:id="#+id/programm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="6dp"
card_view:cardElevation="6dp"
android:layout_gravity="end|right"
card_view:cardBackgroundColor="#color/hotpink"
android:onClick="openNextActivity">
<Button
android:layout_width="wrap_content"
android:drawable="#color/white"
android:layout_height="wrap_content"
android:text="#string/Programme"
android:textColor="#color/hotpink"
android:onClick="openNextActivity"
/>
</android.support.v7.widget.CardView>
If i change the background of the button to white the entire color changes to white:
<android.support.v7.widget.CardView
android:id="#+id/programm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardCornerRadius="2dp"
android:layout_alignParentBottom="true"
android:layout_margin="6dp"
card_view:cardElevation="6dp"
android:layout_gravity="end|right"
card_view:cardBackgroundColor="#color/hotpink"
android:onClick="openNextActivity">
<Button
android:layout_width="wrap_content"
android:drawable="#color/white"
android:background="#color/white"
android:layout_height="wrap_content"
android:text="#string/Programme"
android:textColor="#color/hotpink"
android:onClick="openNextActivity"
/>
</android.support.v7.widget.CardView>
As per google search, this default grey color is set in some theme.I probably need to overwrite some theme and set some attribute. But I am not sure what do I need to change.
Try this:
<Button
android:layout_width="wrap_content"
android:text="Button Text"
android:theme="#style/myButtonTheme"
android:layout_height="wrap_content"/>
set this myButtonTheme in your styles.xml file:
<style name="myButtonTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:textColorPrimary">#color/white</item>
<item name="colorButtonNormal">#5552f1</item>
</style>
[ set your desired background color in colorButtonNormal ]
Output:
i haven't tested it for this kind of situation, but i think something like this can work
you can create a background.xml in your drawable directory and set it as background
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<item>
<shape>
<!-- set the shadow color here -->
<stroke
android:width="3dp"
android:color="#77000000" />
<!-- setting the thickness of shadow (positive value will give shadow on that side) -->
<padding
android:bottom="3dp"
android:left="1dp"
android:right="3dp"
android:top="1dp" />
<corners android:radius="4dp" />
</shape>
</item>
<!-- Background -->
<item>
<shape>
<solid android:color="#fff" />
<corners android:radius="4dp" />
</shape>
</item>
things you can try with this is that you can set the shadow color the color you want or something else that i had in mind was set the opacity of shadow color to zero so that it would be transparent and you can set its thickness by the paddings of the shadow
hope this helps
In yout styles define:
<style name="BlueButtonTheme" parent="#style/AppTheme">
<item name="android:buttonStyle">#style/Widget.AppCompat.Button.Colored</item>
<item name="colorButtonNormal">#color/disabledButtonColor</item>
<item name="colorAccent">#color/enabledButtonColor</item>
<item name="android:disabledAlpha">1</item>
<!-- button text color-->
<item name="android:textColor">#drawable/colored_button_text_color</item>
</style>
And in your layout xml file:
<Button
android:id="#+id/buttonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:theme="#style/BlueWhiteButtonTheme"
android:text="#string/buttonLabel"/>
Just set background for button in your layout
<Button
android:background="#android:color:white"
</Button>
I want to add a white stroke to black text inside a button. I can figure out to do this with regular textview but not when it comes to a button.
Thi is regarding Xml android studio btw.
activity_main.xml:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:background="#drawable/login_button_style"
android:text="Log Ind"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editTextPass"
android:layout_alignEnd="#+id/editTextPass"
android:clickable="true" /><![CDATA[
/>
login_button_style.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#d7d7d7"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
<stroke android:color="#d10f0f" android:width="3dp" />
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="shadoweffect">
<item name="android:paddingLeft">4px</item>
<item name="android:paddingBottom">4px</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">25sp</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">3</item>
</style>
</resources>
can someone please enlighten me?
Look at this answer to a similar question on how to make a border. He creates a new file that is a shape of just that color then stores it as a drawable to be used as the background image
https://stackoverflow.com/questions/7690416/android-border-for-button
Just change the color to white and there you go
EDIT:
Actually seeing now. You want the letters to be outlined in white.
So my best guess would be to draw the entire image with the text and border up in Photoshop/Sketch. And then save that as a drawable. To set for your buttons background. Make sure to set the buttons text to just quotes since you will already have the text in the Photoshop mockup
android:text = ""
I know how to make Material Design button with color fill:
style="#style/Widget.AppCompat.Button.Colored"
And no-bordered transparent button:
style="#style/Widget.AppCompat.Button.Borderless.Colored"
However, is there a way to make Material design bordered (transparent inside) button? Something like below?
You can also use the Material Components for Android.
Add the dependency to your build.gradle:
dependencies { implementation 'com.google.android.material:material:1.3.0' }
In this case you can use the MaterialButton in your layout file:
<com.google.android.material.button.MaterialButton
....
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="#color/colorPrimary"/>
Apply the style #style/Widget.MaterialComponents.Button.OutlinedButton.
In your case use the app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions.
Use te attribute app:strokeColor and app:strokeWidth to change the color and the width of the border.
You can also customize the corners using ShapeApperance (it requires version 1.1.0)
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">#style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance" parent="">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSize">8dp</item>
</style>
The official doc is here and all the android specs here.
With jetpack compose you can use the OutlinedButton and the border attribute:
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
shape = RoundedCornerShape(8.dp)
) {
Text(text = "Save")
}
OLD (support library)
With the new Support Library 28.0.0, the Design Library now contains the Material Button.
You can add this button to our layout file with:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="XXXX"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
You can customize the button with these attributes:
app:backgroundTint: Used to apply a tint to the background of the button. If you wish to change the background color of the button, use this attribute instead of background.
app:strokeColor: The color to be used for the button stroke
app:strokeWidth: The width to be used for the button stroke
Also
Here's how to do it correctly.
What you need to do is
1 - Create shape drawable with stroke
2 - Create ripple drawable
3 - Create selector drawable for less than v21
4 - Create a new style for button with border
5 - Apply style on button
1 - Create shape with stroke
btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#color/colorAccent">
</stroke>
<solid android:color="#color/colorTransparent"/>
<corners
android:radius="5dp">
</corners>
</shape>
2 - Create ripple drawable
drawable-v21/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorOverlay">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</ripple>
android:id="#android:id/mask" is required to have ripple touch feedback on the button. The layer that is marked as mask is not visible on screen, its just for touch feedback.
3 - Create selector drawable for less than v21
drawable/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_outline" android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline" android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline"/>
</selector>
4 - Create a new style for button with border
All resources that that are needed to create the style are given above, that's how your style should look like
<style name="ButtonBorder" parent="Widget.AppCompat.Button.Colored"/>
<style name="ButtonBorder.Accent">
<item name="android:background">#drawable/bg_btn_outline</item>
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:singleLine">true</item>
</style>
4 - Apply style on button
<Button
style="#style/ButtonBorder.Accent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
That's pretty much it. Here's a sample of how the buttons looks now.
You could do this pretty easily by setting Material Design Button's style attribute to #style/Widget.MaterialComponents.Button.OutlinedButton and setting app:strokeColor attribute value to your preferred color.
example:
<com.google.android.material.button.MaterialButton
android:text="Rounded outlined button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/btnRound"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#color/colorPrimaryDark"/>
References:
https://material.io/develop/android/docs/getting-started/
https://material.io/develop/android/components/material-button/
Credits to #NomanRafique for the detailed answer! However, because of the custom background, we've lost a few important things:
Button's height is bigger than the default Widget.AppCompat.Button
Paddings
Enable/Disable states
In case you're wondering, here is how the default background looks like: https://android.googlesource.com/platform/frameworks/support/+/a7487e7/v7/appcompat/res/drawable-v21/abc_btn_colored_material.xml
By reusing original insets, paddings and color selectors, in a simple case we can come up with something like this (all the values are default and come from android support/material library) :
drawable-v21/bg_btn_outlined.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
<padding
android:bottom="#dimen/abc_button_padding_vertical_material"
android:left="#dimen/abc_button_padding_horizontal_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<solid android:color="#android:color/white"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
</shape>
</item>
</ripple>
</inset>
styles.xml
<style name="Button.Outlined" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:background">#drawable/bg_btn_outlined</item>
</style>
At this point, we should have an outlined button that responds to touches, respects the enabled="false" state and of the same height as the default Widget.AppCompat.Button:
Now, from here you can start customizing colors by providing your own implementation of the #color/abc_btn_colored_borderless_text_material color selector.
In your XML use this,
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Its a button"
android:textColor="#android:color/white"
android:textSize="#dimen/_12ssp"
app:backgroundTint="#android:color/transparent"
app:strokeColor="#android:color/white"
app:strokeWidth="#dimen/_1sdp" />
where
app:backgroundTint is used for background color
app:strokeColor is border color
app:strokeWidth is border width
This is how I do buttons only with a border and the ripple effect on Lollipop and above. Just like the AppCompat buttons, these have a fallback pressed effect on lower APIs (if you need ripples on lower APIs, you need to use an external library). I use FrameLayout because it's cheap. The color of the text and the border is black, but you can change it with a custom one:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/background_button_ghost">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:gravity="center"
android:padding="14dp"
android:textSize="16sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#android:color/black"
android:text="Text"/>
</FrameLayout>
drawable/background_button_ghost.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#android:color/black"/>
<solid android:color="#color/transparent"/>
</shape>
If I have missed something, please leave a comment and I will update the answer.
You can do this with Jetpack Compose too. To do it, just create a composable function with a OutlinedButton and pass the as parameter the border you want:
#Composable
fun OutlineButton() {
OutlinedButton(
onClick = { //TODO - implement click here },
border = BorderStroke(1.dp, Color.Blue), // <-- border property
shape = RoundedCornerShape(corner = CornerSize(20.dp)),
colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Blue)
){
Text(text = "CONFIRM")
}
}
Just use MaterialButton with an outline style.
<com.google.android.material.button.MaterialButton
...
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="Delete Mentor Manager"
app:strokeColor="#color/...."
app:strokeWidth="1dp" />
Simply you can use this code. Its looks soo Good.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#F48025"
android:text="login"
android:textColor="#color/colorWhite" />
</LinearLayout>
Here border color is:
android:background="#ffffff"
And background color is:
android:backgroundTint="#F48025"
<Button
android:id="#+id/btn_add_discussion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:padding="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:backgroundTint="#80ffffff"
android:textColor="#color/colorPrimaryDark"
style="#style/btn_start_new_discussion"
android:text="Start new discussion" />
Put below code in Styles.xml file :
<style name="btn_start_new_discussion">
<item name="android:layout_marginTop">15dp</item>
<item name="strokeWidth">2dp</item>
<item name="strokeColor">#color/colorPrimary</item>
<item name="cornerRadius">10dp</item>
</style>