How to make buttons rounded with Material Design Theming? - android

I want my button to have rounded corners like:
// Image taken from google
To achieve this with material theming in android is to set the: shapeAppearanceSmallComponent to have a rounded corner.
But setting shapeAppearanceSmallComponent also affects all other components such as EditText so now they are rounded as well.
So instead of setting it to shapeAppearanceSmallComponent, I created a shapeMaterialOverlay. Set this overlay to a buttonStyle and set this button style in the theme as the default button style.
It works but only for default buttons. If I needed a TextButton as such:
<Button
...
style="#style/Widget.MaterialComponents.Button.TextButton"/>
The TextButton won't be rouned. So as a workaround, I created MyTextButton style which extends from TextButton and set the shapeOverlay there as well.
so Now if I need a TextButton, I'll do:
<Button
...
style="#style/Widget.MaterialComponents.Button.MyTextButton"/>
instead.
I will have to do this for all other button types. I was wondering whether this approach is correct and if not, can anyone guide me on how to properly do this?
Thank you very much.

Just use the app:shapeAppearanceOverlay attribute in the layout.
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="#style/buttomShape"
.../>
with:
<style name="buttomShape">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
The only way to apply it to all buttons is to define custom styles for all the button styles as you are just doing. Something like:
<style name="...." parent="Widget.MaterialComponents.Button">
<item name="shapeAppearance">#style/buttomShape</item>
</style>
<style name="..." parent="Widget.MaterialComponents.Button.TextButton">
<item name="shapeAppearance">#style/buttomShape</item>
</style>

You can set a background drawable on your button. The drawable could look like this:
<shape android:shape="rectangle">
<solid android:color="#android:color/blue" />
<corners android:radius="10dp" />
</shape>
This will give you rounded corners for your button background.

You know there is a site for creating custom layouts for buttons
here is the Link
it is a auto generated Code you don't have to code. Maybe it will be helpful for everyone

Create a drawable xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
android:color="#color/colorPrimary"
android:width="1dp" />
<corners
android:radius="30dp"
android:bottomLeftRadius="30dp"
/>
<solid
android:color="#4c95ec"
/>
</shape>

Related

Tinting the background of an outlined Material Components button

I'm trying to create an outlined Material Components button, however I need it to have a semi-transparent background in addition to the stroke.
This is my XML code so far:
<android.support.design.button.MaterialButton
android:id="#+id/foo"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:backgroundTint="#CFFF"
app:strokeColor="#color/colorAccent"
app:strokeWidth="2dp" />
And this is what this looks like:
The issue is that some of the background is visible outside the stroke around the button (the larger the stroke width, the more white pixels getting out).
For example, here's a 5dp stroke:
Is there a way to fix this, a better way to set the background color, or anything?
Just use the Widget.MaterialComponents.Button.OutlinedButton style:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeWidth="2dp"
app:strokeColor="#color/colorAccent"
app:backgroundTint="#3ad64f"
.../>
If you create a style of that you have to remove the app: In my example this style is called SecondButton
<style name="SecondButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:theme">#style/SecondButtonTheme</item>
<item name="backgroundTint">#color/second_button_back</item>
<item name="strokeColor">#color/second_button_text</item>
<item name="strokeWidth">1dp</item>
<item name="android:textColor">#color/second_button_text</item>
</style>
Use it like that in you
<com.google.android.material.button.MaterialButton
style="#style/SecondButton"
.../>
You could try to create your button in drawables directly in xlm based on this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="backgroundcolor"/>
<stroke android:color="strokecolor" android:width="2dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
founded here : https://android--code.blogspot.fr/2015/01/android-rounded-corners-button.html
and use it like this in your layout :
android:background="#drawable/nameofbutton.xml"

How to change my android application's theme?

How can i change theme of my app.Buttons and other things are in an ordinary look.i just want to change those ordinary look.
Use appcompat in your app, it gives some of the good things from lollipop to all pre-lollipop supported apps. http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html
I have mimicked complete material design using appcompat.
Widgets supported by appcompat are:
EditText
Spinner
CheckBox
RadioButton
Switch (use the new android.support.v7.widget.SwitchCompat)
CheckedTextView
For styling button use the below style:
<style name="Button">
<item name="android:textColor">#color/white</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#drawable/primary_round</item>
</style>
Also you can use this library to prep up your app with material design, https://github.com/navasmdc/MaterialDesignLibrary
To change button style, you can change its background. Here is an example for you. Follow these steps:
1. Define button background
For example:
<Button
android:id="#+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#drawable/button_background" // this is your button background
android:text="#string/myButton"
android:textColor="#color/white"/>
2. Create a new folder in res folder
Right click on res folder in your project > New > Folder. Name it with drawable.
3. Create a new xml file for your button background
Here, we can name it with button_background.xml. You may rename it later. Place it in drawable folder. So, fill it with:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- view background color-->
<solid android:color="#color/your_background_color" >
</solid>
<!-- If you want to add some padding -->
<padding
android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
<!-- Here is the corner radius -->
<corners
android:radius="6dp" >
</corners>
</shape>
And, completed... For information about changing app's theme, this is a reference for you: Styles and Themes

Android Material Design Button Styles

I'm confused on button styles for material design. I'd like to get colorful raised buttons like in the attached link., like the "force stop" and "uninstall" buttons seen under the usage section. Are there available styles or do I need to define them?
http://www.google.com/design/spec/components/buttons.html#buttons-usage
I couldn't find the default button styles.
Example:
<Button style="#style/PrimaryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="#+id/button3"
android:layout_below="#+id/editText5"
android:layout_alignEnd="#+id/editText5"
android:enabled="true" />
If I try to change the background color of the button by adding
android:background="#color/primary"
all of the styles go away, such as the touch animation, shadow, rounded corner, etc.
I will add my answer since I don't use any of the other answers provided.
With the Support Library v7, all the styles are actually already defined and ready to use, for the standard buttons, all of these styles are available:
style="#style/Widget.AppCompat.Button"
style="#style/Widget.AppCompat.Button.Colored"
style="#style/Widget.AppCompat.Button.Borderless"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
Widget.AppCompat.Button:
Widget.AppCompat.Button.Colored:
Widget.AppCompat.Button.Borderless
Widget.AppCompat.Button.Borderless.Colored:
To answer the question, the style to use is therefore
<Button style="#style/Widget.AppCompat.Button.Colored"
.......
.......
.......
android:text="Button"/>
How to change the color
For the whole app:
The color of all the UI controls (not only buttons, but also floating action buttons, checkboxes etc.) is managed by the attribute colorAccent as explained here.
You can modify this style and apply your own color in your theme definition:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorAccent">#color/Orange</item>
</style>
For a specific button:
If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
Then you just need to apply this new style on the button with:
android:theme="#style/AppTheme.Button"
To set a default button design in a layout, add this line to the styles.xml theme:
<item name="buttonStyle">#style/btn</item>
where #style/btn is your button theme. This sets the button style for all the buttons in a layout with a specific theme
Simplest Solution
Step 1: Use the latest support library
compile 'com.android.support:appcompat-v7:25.2.0'
Step 2: Use AppCompatActivity as your parent Activity class
public class MainActivity extends AppCompatActivity
Step 3: Use app namespace in your layout XML file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
Step 4: Use AppCompatButton instead of Button
<android.support.v7.widget.AppCompatButton
android:id="#+id/buttonAwesome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome Button"
android:textColor="#color/whatever_text_color_you_want"
app:backgroundTint="#color/whatever_background_color_you_want"/>
If I understand you correctly, you want to do something like this:
In such case, it should be just enough to use:
<item name="android:colorButtonNormal">#2196f3</item>
Or for API less than 21:
<item name="colorButtonNormal">#2196f3</item>
In addition to Using Material Theme Tutorial.
Animated variant is here.
You can use the
Material Component library.
Add the dependency to your build.gradle:
dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
Then add the MaterialButton to your layout:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
app:strokeColor="#color/colorAccent"
app:strokeWidth="6dp"
app:layout_constraintStart_toStartOf="parent"
app:shapeAppearance="#style/MyShapeAppearance"
/>
You can check the full documentation here and API here.
To change the background color you have 2 options.
Using the backgroundTint attribute.
Something like:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">#color/button_selector</item>
//..
</style>
It will be the best option in my opinion. If you want to override some theme attributes from a default style then you can use new materialThemeOverlay attribute.
Something like:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name=“materialThemeOverlay”>#style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component -->
<item name="colorPrimary">#color/green</item>
</style>
The option#2 requires at least the version 1.1.0.
You can use one of these styles:
Filled Button (default): style="#style/Widget.MaterialComponents.Button
Text Button: style="#style/Widget.MaterialComponents.Button.TextButton"
OutlinedButton: style="#style/Widget.MaterialComponents.Button.OutlinedButton"
OLD Support Library:
With the new Support Library 28.0.0, the Design Library now contains the MaterialButton.
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="YOUR TEXT"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
By default this class will use the accent colour of your theme for the buttons filled background colour along with white for the buttons text colour.
You can customize the button with these attributes:
app:rippleColor: The colour to be used for the button ripple effect
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
app:cornerRadius: Used to define the radius used for the corners of the button
Here is how I got what I wanted.
First, made a button (in styles.xml):
<style name="Button">
<item name="android:textColor">#color/white</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#drawable/primary_round</item>
</style>
The ripple and background for the button, as a drawable primary_round.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/primary_600">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#color/primary" />
</shape>
</item>
</ripple>
This added the ripple effect I was looking for.
Beside android.support.design.button.MaterialButton (which mentioned by Gabriele Mariotti),
There is also another Button widget called com.google.android.material.button.MaterialButton which has different styles and extends from AppCompatButton:
style="#style/Widget.MaterialComponents.Button"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
style="#style/Widget.MaterialComponents.Button.Icon"
style="#style/Widget.MaterialComponents.Button.TextButton.Icon"
Filled, elevated Button (default):
style="#style/Widget.MaterialComponents.Button"
Filled, unelevated Button:
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
Text Button:
style="#style/Widget.MaterialComponents.Button.TextButton"
Icon Button:
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/icon_24px" // Icons can be added from this
A text Button with an icon::
Read: https://material.io/develop/android/components/material-button/
A convenience class for creating a new Material button.
This class supplies updated Material styles for the button in the
constructor. The widget will display the correct default Material
styles without the use of the style flag.
Here is a sample that will help in applying button style consistently across your app.
Here is a sample Theme I used with the specific styles..
<style name="MyTheme" parent="#style/Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
</style>
<style name="ButtonAppTheme" parent="android:Widget.Material.Button">
<item name="android:background">#drawable/material_button</item>
</style>
This is how I defined the button shape & effects inside res/drawable-v21 folder...
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp" />
<solid android:color="#color/primary" />
</shape>
</item>
</ripple>
2dp corners are to keep it consistent with Material theme.
I tried a lot of answer & third party libs, but none was keeping the border and raised effect on pre-lollipop while having the ripple effect on lollipop without drawback. Here is my final solution combining several answers (border/raised are not well rendered on gifs due to grayscale color depth) :
Lollipop
Pre-lollipop
build.gradle
compile 'com.android.support:cardview-v7:23.1.1'
layout.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
card_view:cardElevation="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardMaxElevation="8dp"
android:layout_margin="6dp"
>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="#drawable/btn_bg"
android:text="My button"/>
</android.support.v7.widget.CardView>
drawable-v21/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="?attr/colorPrimary"/>
</ripple>
drawable/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="#color/colorPrimaryDark" android:state_focused="true"/>
<item android:drawable="#color/colorPrimary"/>
</selector>
Activity's onCreate
final CardView cardView = (CardView) findViewById(R.id.card);
final Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
ObjectAnimator o1 = ObjectAnimator.ofFloat(cardView, "cardElevation", 2, 8)
.setDuration
(80);
ObjectAnimator o2 = ObjectAnimator.ofFloat(cardView, "cardElevation", 8, 2)
.setDuration
(80);
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
o1.start();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
o2.start();
break;
}
return false;
}
});
1) You can create rounded corner button by defining xml drawable and you can increase or decrease radius to increase or decrease roundness of button corner.
Set this xml drawable as background of button.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="6dp"
android:insetRight="4dp"
android:insetBottom="6dp">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="#0091ea">
<corners android:radius="10dp" />
<solid android:color="#1a237e" />
<padding android:bottom="6dp" />
</shape>
</item>
</ripple>
</inset>
2) To change default shadow and shadow transition animation between button states, you need to define selector and apply it to button using android:stateListAnimator property. For complete button customization reference : http://www.zoftino.com/android-button
I've just created an android library, that allows you to easily modify the button color and the ripple color
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn"
android:text="Android button modified in layout"
android:textColor="#android:color/white"
app:buttonColor="#android:color/black"
app:rippleColor="#android:color/white"/>
You don't need to create an style for every button you want wit a different color, allowing you to customize the colors randomly
// here is the custom button style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="45"
android:centerColor="#color/colorPrimary"
android:startColor="#color/colorPrimaryDark"
android:endColor="#color/colorAccent"
>
</gradient>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
>
</corners>
<stroke
android:width="2dp"
android:color="#color/colorWhite"
>
</stroke>
</shape>
</item>
</selector>
you can give aviation to the view by adding z axis to it and can have default shadow to it. this feature was provided in L preview and will be available after it release. For now you can simply add a image the gives this look for button background

How to change line color in EditText

I am creating an EditText in my layout xml file
But I want to change color line in EditText from Holo to (for example) red.
How that can be done?
This is the best tool that you can use for all views and its FREE many thanks to #Jérôme Van Der Linden.
The Android Holo Colors Generator allows you to easily create Android components such as EditText or spinner with your own colours for your Android application. It will generate all necessary nine patch assets plus associated XML drawable and styles which you can copy straight into your project.
http://android-holo-colors.com/
UPDATE 1
This domain seems expired but the project is an open source you can find here
https://github.com/jeromevdl/android-holo-colors
try it
this image put in the background of EditText
android:background="#drawable/textfield_activated"
UPDATE 2
For API 21 or higher, you can use android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="#android:color/holo_red_light" />
Update 3
Now We have with back support AppCompatEditText
Note: We need to use app:backgroundTint instead of android:backgroundTint
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
app:backgroundTint="#color/blue_gray_light" />
Update 4
AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
I don't like previous answers. The best solution is to use:
<android.support.v7.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
android:backgroundTint for EditText works only on API21+ . Because of it, we have to use the support library and AppCompatEditText.
Note: we have to use app:backgroundTint instead of android:backgroundTint
AndroidX version
<androidx.appcompat.widget.AppCompatEditText
app:backgroundTint="#color/blue_gray_light" />
You can also quickly change the EditText's underline color by tinting the background of the EditText like so:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Something or Other"
android:backgroundTint="#android:color/holo_green_light" />
for API below 21, you can use theme attribute in EditText
put below code into style file
<style name="MyEditTextTheme">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
use this style in EditText as
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="#dimen/user_input_field_height"
android:layout_marginTop="40dp"
android:hint="#string/password_hint"
android:theme="#style/MyEditTextTheme"
android:singleLine="true" />
Programmatically, you can try:
editText.getBackground().mutate().setColorFilter(getResources().getColor(android.R.color.holo_red_light), PorterDuff.Mode.SRC_ATOP);
Its pretty simple (Required: Minimum API 21)...
Go to your xml and select the EditText field
On the right side, you can see the 'Attributes' window. Select 'View All Attributes'
Just search for 'tint'
And add/change the 'backgroundTint' to a desired color hex (say #FF0000)
Keep Coding........ :)
You can change the color of EditText programmatically just using this line of code:edittext.setBackgroundTintList(ColorStateList.valueOf(yourcolor));
i think the best way is by theme:
<style name="MyEditTextTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#color/black</item>
<item name="colorControlActivated">#color/action_blue</item>
<item name="colorControlHighlight">#color/action_blue</item>
</style>
<style name="AddressBookStyle" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13sp</item>
<item name="android:theme">#style/MyEditTextTheme</item>
</style>
<android.support.v7.widget.AppCompatEditText
style="#style/AddressBookStyle"/>
To change Edittext’s underline color:
If you want the entire app to share this style, then you can do the following way.
(1) go to styles.xml file. Your AppTheme that inherits the parent of Theme.AppCompat.Light.DarkActionBar (in my case) will be the base parent of all they style files in your app. Change the name of it to “AppBaseTheme’. Make another style right under it that has the name of AppTheme and inherits from AppBaseTheme that you just edited. It will look like following:
<!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<!--see http://www.google.com/design/spec/style/color.html#color-color-palette-->
<item name="colorPrimary">#color/material_brown_500</item>
<item name="colorPrimaryDark">#color/material_brown_700</item>
<item name="colorAccent">#color/flamingo</item>
<style name="AppTheme" parent="AppBaseTheme">
<!-- Customize your theme here. -->
</style>
Then change the “colorAccent” to whatever the color you want your EditText line color to be.
(2) If you have other values folders with style.xml, this step is very important. Because that file will inherit from your previous parent xml file. For example, I have values-19/styles.xml. This is specifically for Kitkat and above. Change its parent to AppBaseTheme and make sure to get rid of “colorAccent” so that it doesn’t override the parent’s color. Also you need to keep the items that are specific to version 19. Then it will look like this.
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
The line's color is defined by EditText's background property. To change it you should change the android:background in the layout file.
I should note that this style is achieved by using a 9-patch drawable. If you look in the SDK, you can see that the background of the EditText is this image:
To change it you could open it in an image manipulation program and color it in desired color. Save it as bg_edit_text.9.png and then put it in you drawable folder. Now you can apply it as a background for your EditText like so:
android:background="#drawable/bg_edit_text"
drawable/bg_edittext.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
<item
android:left="-2dp"
android:right="-2dp"
android:top="-2dp">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="1dp"
android:color="#color/colorDivider" />
</shape>
</item>
</layer-list>
Set to EditText
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_edittext"/>
The background of widgets are API level dependent.
ALTERNATIVE 1
You can provide a custom image to your EditText background by
android:background="#drawable/custom_editText"
Your image should look something like this. It will give you the desired effect.
ALTERNATIVE 2
Set this xml to your EditText background attribute.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#4C000000"/>
<corners android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
</shape>
This will have the same look and feel of your EditText on every API.
You can change the color with tinting the background
<EditText
android:backgroundTint="#color/red"/>
in xml layout use:
android:backgroundTint="#color/colorPrimary"
or in java code copy this method:
public void changeLineColorInEditText(EditText editText, int color) {
editText.setBackgroundTintList(ColorStateList.valueOf(color));
}
and use it like this:
changeLineColorInEditText(editText, getResources().getColor(R.color.colorPrimary));
Use this method.. and modify it according to ur view names. This code works great.
private boolean validateMobilenumber() {
if (mobilenumber.getText().toString().trim().isEmpty() || mobilenumber.getText().toString().length() < 10) {
input_layout_mobilenumber.setErrorEnabled(true);
input_layout_mobilenumber.setError(getString(R.string.err_msg_mobilenumber));
// requestFocus(mobilenumber);
return false;
} else {
input_layout_mobilenumber.setError(null);
input_layout_mobilenumber.setErrorEnabled(false);
mobilenumber.setBackground(mobilenumber.getBackground().getConstantState().newDrawable());
}
If you want a flat line, you can do this easily with xml. Here is the xml example:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:top="-1dp"
android:left="-1dp"
android:right="-1dp"
android:bottom="1dp"
>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#6A9A3A"/>
</shape>
</item>
</layer-list>
Replace the shape with a selector if you want to provide different width and color for focused edittext.
The best approach is to use an AppCompatEditText with backgroundTint attribute of app namespace. i.e.
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
app:backgroundTint="YOUR COLOR"
android:layout_height="wrap_content" />
when we use android:backgroundTint it will only work in API21 or more but app:backgroundTint works on all API levels your app does.
You can do it dynamically if you have custom class for edittext.
First of all you have declare state and color of edittext given below.
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
Then Create ColorStateList variable with that
ColorStateList myList = new ColorStateList(states, colors);
Then last step is to assign it to edittext.
editText.setBackgroundTintList(myList);
After this you have to written on focused change event.
this.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
setUnderlineColor(selectionColor,deselectionColor);
}
});
And you can make above code inside setUnderlineClor() Method,
private void setUnderlineColor(int primaryColor, int secondaryColor) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{-android.R.attr.state_focused}, // enabled
new int[]{android.R.attr.state_focused}, // disabled
};
int[] colors = new int[]{
secondaryColor,
primaryColor,
};
ColorStateList myList = new ColorStateList(states, colors);
setBackgroundTintList(myList);
}
}
Use android:background property for that edittext. Pass your drawable folder image to it.
For example,
android:background="#drawable/abc.png"
<EditText
android:id="#+id/et_password_tlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:textColorHint="#9e9e9e"
android:backgroundTint="#000"
android:singleLine="true"
android:drawableTint="#FF4081"
android:paddingTop="25dp"
android:textColor="#000"
android:paddingBottom="5dp"
android:inputType="textPassword"/>
<View
android:id="#+id/UnderLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/et_password_tlay"
android:layout_centerHorizontal="true"
android:background="#03f94e" />
**it one of doing with view **
Try the following way, it will convert the bottom line color of EditText, when used as a background property.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:left="#dimen/spacing_neg"
android:right="#dimen/spacing_neg"
android:top="#dimen/spacing_neg">
<shape>
<solid android:color="#android:color/transparent" />
<stroke
android:width="#dimen/spacing_1"
android:color="#android:color/black" />
</shape>
</item>
</layer-list>
This can simply be done by including this android:theme="#style/AppTheme.AppBarOverlay as attribute in for your editText
and add this <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> to your styles

customize the height of tab indicator in view pager?

I am looking to style the tab indicator like pininterest where the indicator does not look like the normal "android" style . Is there some attribute where i can set the indicator height ?Please look into the following image .
It might be not a proper solution, and question is really old, but as I haven't found any proper solutions, this is how I did it:
I created a layerList file called indicator:
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF0000" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="rectangle">
<solid android:color="#efefef" />
</shape>
</item>
</layer-list>
And just set it as my tab indicator background:
<style name="CustomTabPageIndicator" parent="#android:style/Widget.Holo.ActionBar.TabView">
<item name="android:background">#drawable/indicator</item>
</style>
I've also applied this theme to my actionBar:
<style name="MyActionBarTheme" parent="android:style/Widget.Holo.ActionBar">
<item name="android:actionBarTabStyle">#style/CustomTabPageIndicator</item>
</item> </style>
It works because in indicator.xml first shape is lower shape, which is actual indicator, and to I set its height by providing bottom padding for another shape which color is identical to actionbar color.
just set layout_height attribute in your layout file:
com.viewpagerindicator.TitlePageIndicator
android:layout_height="#dimen/indicator_height"
Using the material design TabLayout, You can set the height with app:tabIndicatorHeight
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorHeight="4dp" />

Categories

Resources