Android MaterialButton padding between drawableLeft icon and text - android

As the topic said, with materialButton, I want to set up the distance between drawableLeft icon and text, I do not want to use icon, just drawableLeft
By default, the drawableleft icon is far away from text, I want to make them closer, but I cannot use icon and icon gravity. If I use icon and icon gravity, it will not show because of the style I use, but if I change the style, then there will be another problem: there will be shadow below the button. I do not want the shadow, so I have to use the style.
What I want is a MaterialButton, grey background, left icon, centre is text, icon and text is closed, MaterialButton is round corner, no shadow below the MaterialButton. Here is the code so far:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/btnShortCut"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:orientation="vertical"
android:layout_marginRight="10dp"
app:backgroundTint="#color/background"
app:cornerRadius="#dimen/cornerM"
app:strokeColor="#color/primaryGrey"
app:strokeWidth="0.5dp"
android:padding="5dp"
android:textColor="#color/secondaryText"
android:textSize="#dimen/fontBody" >
</com.google.android.material.button.MaterialButton>
<style name="Button" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textAllCaps">false</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:gravity">center</item>
<item name="android:padding">0dp</item>
</style>

Is it necessary for you to use a material button? Have you considered using a FrameLayout with a textview overlapped on it?
In your example it would look something like this, but with some edits since I'm not sure exactly how you want it to look.
<FrameLayout
android:id="#+id/btnShortCut"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center"
app:cornerRadius="#dimen/cornerM"
app:strokeColor="#color/primaryGrey"
app:strokeWidth="0.5dp"
android:padding="5dp">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableStart="#drawable/<your icon here>"
android:drawablePadding="15dp"
android:textColor="#color/secondaryText"
android:textSize="#dimen/fontBody"/>
</FrameLayout>

Related

How to remove underline from dropdown menu material design in android

I can't seem to get rid of the black underline from Exposed Dropdown Menu done according to material design guide in my android UI.
The dropdown looks like this:
I have tried setting background to null, transparent or custom shape. app:boxBackgroundMode="none" doesn't work either.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/spinner_age"
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_rounded_button"
android:hint="Age">
<AutoCompleteTextView
android:id="#+id/age_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
</com.google.android.material.textfield.TextInputLayout>
Ideally the dropdown would look like a button without the underline. How do I get rid of it?
Please change your this line
android:background="#android:color/transparent"
With
android:background="#00000000"
Make background = null
<AutoCompleteTextView
android:id="#+id/age_dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"/>
just add this line to remove the underline
android:inputType="textNoSuggestions"
Just use the standard style #style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu and
You don't need to use a custom background shape, you can just use the
app:boxCornerRadius* attributes to define the rounded shape.
Something like:
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
app:boxCornerRadiusBottomEnd="32dp"
app:boxCornerRadiusTopEnd="32dp"
app:boxCornerRadiusBottomStart="32dp"
app:boxCornerRadiusTopStart="32dp"
app:boxStrokeColor="#color/myselector"
...>
<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
/>
</com.google.android.material.textfield.TextInputLayout>
To change the color of the line under the component, you have to change the app:boxStrokeColor attribute. Just customize the selector as you prefer:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="0" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0" android:color="?attr/colorOnSurface"/>
</selector>
This is the result:
Pay attention to this note in doc:
Note: When using a filled text field with an EditText child that is not a TextInputEditText, make sure to set the EditText's android:background to #null. This allows TextInputLayout to set a filled background on the EditText.
Alternative:
With the new version 1.1.0 you can also use the shapeAppearanceOverlay attribute to customize the shape of your component.
Just use:
<com.google.android.material.textfield.TextInputLayout
style="#style/MyFilledBox_ExposedDropdownMenu
...>
<AutoCompleteTextView
android:background="#null"
... />
</com.google.android.material.textfield.TextInputLayout>
and this style:
<!-- Rounded -->
<style name="MyFilledBox_ExposedDropdownMenu" parent="#style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
<item name="shapeAppearanceOverlay">#style/ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded</item>
<item name="boxStrokeColor">#color/myselector</item>
</style>
<style name="ShapeAppearanceOverlay.MyApp.TextInputLayout.Rounded" parent="">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">32dp</item>
</style>
Setting boxStrokeWidth = 0dp would solve it

gravity bottom not working with textview inside relative layout

My RelativeLayout has a 25sp image and 25sp text on left side and then 12sp text on right side:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/omgIcon"
android:layout_width="25sp"
android:layout_height="25sp"
android:src="#drawable/omg_face_emoji"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OMG"
android:layout_toRightOf="#+id/omgIcon"
android:textAppearance="#style/HeaderFont" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See All"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:textAppearance="#style/LinkFont" />
</RelativeLayout>
styles.xml:
<style name="HeaderFont" parent="#android:style/TextAppearance.Large">
<item name="android:textColor">#000</item>
<item name="android:textSize">25sp</item>
<item name="android:fontFamily">arial</item>
</style>
<style name="LinkFont" parent="#android:style/TextAppearance.Small">
<item name="android:textColor">#FF00FF</item>
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">arial</item>
</style>
Unfortunately, the right floating text does not align to bottom of relative layout:
The baseline of right text should be same as left text. What am I doing wrong?
yes, it does not align to the bottom of the relative layout. Why? Because the android:gravity="bottom" means that gravity/align inside itself and it means the text See All will be at the bottom of your TextView. You have to use the layout_gravity instead (but it works on LinearLayout only).
If you want to align bottom, just add android:layout_alignParentBottom="true" to your TextView. However, if you want to align bottom of OMG text, you should add android:layout_alignBottom="OMG_ID" instead

Change TextInputLayout focused & unfocused hint color programmatically

I wanted to change the color of hint in TEXT INPUT LAYOUT same when it is in focused or unfocused state.i have two TEXT INPUT LAYOUT fields.Both fields are same color.But it in focused state color appears but in an unfocused state the default grey color appears.i want to keep the color color of hint same in both focused and unfocused state.
<com.example.viveka.snipeid.CustomTextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
app:errorEnabled="true"
>
<EditText
android:id="#+id/editEmail"
android:layout_width="match_parent"
android:layout_height="40dp"
android:hint="email / snipe id"
android:digits="abcdefghijklmnopqrstuvwxyz.#0123456789"
android:textAllCaps="false"
android:textSize="12sp"
android:layout_marginLeft="-4dp"
android:textColor="#color/lightgray"
android:textColorHint="#color/primary"/>
</com.example.viveka.snipeid.CustomTextInputLayout>
Expected image
what i got
i need to change both fields as same color..
you can simply do that by using the material TextInputLayout and add textColorHint , hintTextColor attributes , this works perfectly
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/gray"
app:hintTextColor="#color/gray"
>
You can do like this.
android:textColorHint="#FF0000" was EditText's hint color.
If you want the text color as well as the hint color.
You can change android:textColor="#FF0000" in the EditText.
You can add app:errorTextAppearance="#style/text_in_layout_error_hint_Style" to change the error text color.
<com.example.viveka.snipeid.CustomTextInputLayout
android:id="#+id/input_layout_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:textColorHint="#FF0000"
app:errorTextAppearance="#style/text_in_layout_error_hint_Style"
app:hintTextAppearance="#style/text_in_layout_hint_Style"
app:errorEnabled="true">
<EditText
android:id="#+id/editEmail"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="-4dp"
android:digits="abcdefghijklmnopqrstuvwxyz.#0123456789"
android:hint="email / snipe id"
android:textAllCaps="false"
android:textColor="#FF0000"
android:textColorHint="#color/primary"
android:textSize="12sp"/>
</com.example.viveka.snipeid.CustomTextInputLayout>
The LEFT-TOP hint color depended on app:hintTextAppearance="#style/text_in_layout_hint_Style".
And the style code:
<style name="text_in_layout_hint_Style">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">12sp</item>
</style>
Add error text style
<style name="text_in_layout_error_hint_Style">
<item name="android:textColor">#00ff00</item>
<item name="android:textSize">12sp</item>
</style>
like this:
Hope to help you.
Create a selector_text_input_layout_hint_color.xml (for example)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/colorWhenFocus" android:state_focused="true"/>
<item android:color="#color/colorWhenNotFocus" android:state_focused="false"/>
</selector>
Set in the Text Input Layout with the next attribute:
android:textColorHint="#color/selector_text_input_layout_hint_color"
Thanks to: https://medium.com/#fast3r/this-is-brilliant-martin-thank-you-very-much-6c4a43a65df4
That's all. It works for me ;)
In new material theming, the hint color is a tinted version of the colorOnSurface attribute when unfocused.
When focused it is the colorPrimary

Android SwitchCompat style

I tested my app on my new device with android 5.1.1. In my SettingsActivity i have a switch. I already read some posts and changed it to android.support.v7.widget.SwitchCompat but the problem still is the following: On my old device the switch looks very nice. you can set two texts for textOn and textOff and it fits perfectly. but since api 21 or whatever I get this little bastard here:
It looks like ****. How can I restyle it for all devices like the lollypop switch?
Edit:
The question above is answered.
One tiny thing is missing: How to change the color of the text on the switch.(Not the tag on the left side!!!)
styles.xml
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp">
<android.support.v7.widget.SwitchCompat
android:id="#+id/mySwitch"
android:layout_width="match_parent"
android:switchMinWidth="56dp"
android:layout_height="wrap_content"
android:switchTextAppearance="#style/SwitchTextAppearance"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
app:showText="true"
android:textOn="ON"
android:textOff="OFF"
android:text="Toggle Switch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#android:color/transparent"
android:button="#null"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Using SwitchCompat.setSwitchTextAppearance you can set the style for the text that appears inside the switch itself.
Add a style something like this and set it using setSwitchTextAppearance:
<style name="SwitchTextAppearance" parent="TextAppearance.AppCompat.Widget.Switch">
<item name="android:textSize">12sp</item>
<item name="android:textColor">#3F51B5</item>
</style>
And you should be able to customize the size, color, etc of your "OFF" text.

How to remove extra padding or margin in material design button?

I am trying to create an button which is attached to the TextView above the button as shown in the below image.
The above screenshot is taken from the Note 4 and the OS version is 5.0.1.
Below is the code is used to achieve the UI.
layout/xyz.xml
<Button
android:layout_width="250dp"
android:layout_height="50dp"
android:theme="#style/myButton"
android:text="Cancel"/>
values-v21/style.xml
<style name="myButton" parent="#style/Base.Widget.AppCompat.Button">
<item name="android:colorButtonNormal">#3578A9</item>
<item name="android:inset">0dp</item>
</style>
But if I run the same code in Nexus4 OS verison 5.1.1, the button is taking the margin for all the 4 sides and the screenshot looks like below.
If I remove the "android:theme" and provide the "android:background", the UI looks like the first image. But It won't give the ripple effect. So how the achieve the UI as first image with the ripple effect.
android:insetBottom="0dp"
<com.google.android.material.button.MaterialButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="56dp"
android:text="Edit"
app:cornerRadius="0dp"
android:insetBottom="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Step 1: Put the below code in styles.xml
<style name="myColoredButton">
<item name="android:textColor">#FF3E96</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#FF0000</item>
</style>
Here you can change the textColor( I have used #FF3E96 above) and background color (I have used #FF0000) for your button. You can also override textColor values from your Button related layout xml by using android:colorButtonNormal.
Step 2:Create a new XML file under drawables folder and add the following code: I named my XML file as primary.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorPrimary">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#8B8386" />
</shape>
</item>
</ripple>
Step 3: Use the style and drawable in your Button as follows.
<Button
style="#style/myColoredButton"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Cancel"
android:gravity="center"
android:background="#drawable/primary_round"
android:colorButtonNormal="#3578A9" />
Hope it solves your problem.
I also had issue related to top and bottom spacing for Apply button as you can see above image.
I wanted to show button with uniform background without any spacing as you can see for "Reset" button.
But I have to set both property button background as well as background Tint to same color to achieve this.
If I reuse this buttons at more than one place then I have to programmatically change 2 property to change background color of button.
You can try below solution:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="#+id/btnLeftOk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/apply"
android:textColor="#color/white"
app:backgroundTint="#color/primary_dark_gray"
app:cornerRadius="0dp" />
<com.google.android.material.button.MaterialButton
android:id="#+id/btnRightCancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:filterTouchesWhenObscured="true"
android:gravity="center"
android:text="#string/reset"
android:insetTop="0dp"
android:insetBottom="0dp"
android:textColor="#color/white"
app:cornerRadius="0dp"
app:backgroundTint="#color/btn_background_gray" />
</LinearLayout>
After applying below property to "Reset" button.
android:insetTop="0dp"
android:insetBottom="0dp"
Button spacing will be removed from top and bottom. It will occupy spacing with button color. (like in Reset button)
Button background can be changed using one property app:backgroundTint.

Categories

Resources