This is a sample TextView in XML, which will properly apply the style TextContactContactName to the TextView.
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/photo"
android:layout_alignTop="#id/photo"
android:paddingLeft="10dp"
android:text="First Last"
style="#style/TextContactContactName"
/>
Now, I have some other things to set on this element, which are not text related. As such I tried to move the TextContactContactName and apply it via "android:textAppearance"
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/photo"
android:layout_alignTop="#id/photo"
android:paddingLeft="10dp"
android:text="First Last"
android:textAppearance=#style/TextContactContactName"
/>
Unluckily, the text formatting is not being applied.
This is how the style (stored in res/values/styles-text.xml) looks like:
<style name="TextContactContactName">
<item name="android:textSize">24sp</item>
<item name="android:textColor">#333333</item>
<item name="android:shadowColor">#ffffff</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">1</item>
<item name="android:shadowRadius">1</item>
</style>
Am I using the textAppearance tag wrong?
I tested on Android 2.2 - HTC Desire.
Ok, looks like my fault. I had made a quick test:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text styled using 'style'."
style="#style/UiTestTextView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text styled using 'textAppearance'."
android:textAppearance="#style/UiTestTextView"
/>
and the styles-text.xml:
<style name="UiTestTextView">
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">16sp</item>
<item name="android:shadowColor">#ffffff</item>
<item name="android:shadowDx">1</item>
<item name="android:shadowDy">2</item>
<item name="android:shadowRadius">3</item>
</style>
The key thing is to remember that shadow* elements are not text properties. And as such will not be applied via textAppearance.
I'm going to ask on the Android Developers Mailing list if this is desired behavior.
I filed a bug report on this issue. I think I got the standard reply "the shadow is not considered part of the text appearance".
http://code.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=35887
Related
I need to remove underline of TextInputLayout. Its almost removed but showing little parts on start and on the end (black). It shows only when activated Screen below
style:
<style name="EditTextLoginRegister" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="boxCornerRadiusBottomEnd">8dp</item>
<item name="boxCornerRadiusBottomStart">8dp</item>
<item name="boxCornerRadiusTopEnd">8dp</item>
<item name="boxCornerRadiusTopStart">8dp</item>
<item name="boxStrokeWidth">0dp</item>
<item name="boxStrokeColor">#android:color/transparent</item>
<item name="boxBackgroundColor">#color/grey</item>
<item name="android:textColorHint">#color/greyLight2</item>
<item name="startIconTint">#color/greyLight2</item>
<item name="endIconTint">#color/greyLight2</item>
<item name="hintTextColor">#color/violet</item>
</style>
and the TextInputLayout:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/tl_email_login"
style="#style/EditTextLoginRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="24dp"
android:hint="#string/e_mail"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_header_login"
app:startIconDrawable="#drawable/ic_user">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_email_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:textColor="#color/greyLight" />
</com.google.android.material.textfield.TextInputLayout>
Try adding boxStrokeWidthFocused to your style
<style name="EditTextLoginRegister" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<!-- Other attributes -->
<item name="boxStrokeWidthFocused">0dp</item>
</style>
Admittedly, this is the most difficult part -> styling Material design views.
In Android Studio, it is possible to get AutoComplete options, and I usually go through the complete list and try out various items.
how can i remove that gap after the icon? how can i Wrap button around icon normally?
<Button
android:layout_marginStart="4dp"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/white"
app:icon="#drawable/googleg_standard_color_18"
app:iconTint="#null"/>
I dont see any direct way of doit it but did some tweak to help you out
Create Button without wrap content give some width as per your design look and feel.
<Button
android:id="#+id/button"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/ic_google_hangouts"
app:backgroundTint="#android:color/white" />
In code do the following changes and play with adjusting padding per your requirement.
val button = findViewById<Button>(R.id.button)
button.setPadding(24,0,0,0)
the image dimension which I used for testing is 24dpX24dp
my final result:
You can use a ImageButton as suggested in the comments
You can use the MaterialButton with this style:
<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">48dp</item>
<item name="android:minHeight">48dp</item>
<item name="iconGravity">textStart</item>
</style>
Something like:
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Widget.MaterialComponents.Button.IconOnly"
app:icon="#drawable/...."
app:iconTint="#null"/>
Here is the button :
<Button
android:id="#+id/btn_choose_photo"
style="#style/Widget.MaterialComponents.Button.TextButton.Dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_camera"
android:drawablePadding="8dp"
android:text="#string/image_picker_dialog_choose_image"
android:textAlignment="textStart" />
I am using material themes, so this will be inflated into a material button
drawableStart has no effect at all, however drawableEnd, bottom and top work just fine
When I make the button tag a text view, drawableStart works
It seems like a bug or maybe I am missing something ?
Edit: My app theme is as follows :
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!---colors-->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryVariant">#color/colorPrimaryVariant</item>
<item name="colorPrimaryDark">#color/colorPrimaryVariant</item>
<item name="colorSecondary">#color/colorSecondary</item>
<item name="colorSecondaryVariant">#color/colorSecondaryVariant</item>
<item name="colorAccent">#color/colorSecondary</item>
<!--
<item name="android:colorBackground">#color/colorBackground</item>
-->
<!--components-->
<item name="textInputStyle">#style/text_input_layout_style</item>
<item name="bottomSheetDialogTheme">#style/bottom_sheet_dialog_theme</item>
<item name="spinnerStyle">#style/spinner_style</item>
<item name="android:spinnerStyle">#style/spinner_style</item>
<item name="android:toolbarStyle">#style/toolbar_style</item>
<item name="toolbarStyle">#style/toolbar_style</item>
<item name="actionOverflowButtonStyle">#style/overflow_button_style</item>
<item name="android:actionOverflowButtonStyle">#style/overflow_button_style</item>
</style>
You should use app:icon like this:
In the layout:
<Button
...
app:icon="#drawable/ic_camera"
style="#style/Widget.MaterialComponents.Button.TextButton"
/>
It is displayed at the start, before the text label. You can change icon gravity, tint or size.
For more information
Using
android:drawableLeft
will solve the problem, but will not give you RTL(Right to left) support. If your end user uses a different language which follow RTL, then your Button will not support it.
Are you managing the Button's property in the Activity or Fragment, as technically speaking,
android:drawableStart
should work.
You can change to "App compat button"
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/continuar_anonimo_button"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
app:backgroundTint="#color/greenPrimary"
android:background="#drawable/corner_boton_outline"
android:text="Continuar anonimamente"
android:paddingLeft="15dp"
android:drawableStart="#drawable/ic_google_color"
style="?android:textAppearanceSmall"/>
Use android:drawableLeft
android:drawableLeft="#drawable/ic_phone"
android:backgroundTint="#android:color/white"
I have multiple buttons that all have the same textSize, layout_width, etc. and I do not want to copy and paste these attributes over and over as it makes for hard to edit code. In other words, I am looking to take many of these:
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/round_button_normal"
android:text="#string/button1"
android:textSize="50sp" />
And instead be able to use a preset button (where the layout_width, layout_height, layout_weight, background, and textSize are all set to the above values, by default):
<PresetButton
android:id="#+id/button1"
android:text="#string/button1" />
Just use styles.
Styles and Themes
Example:
In: res/values/styles.xml
<style name="Numbers">
<item name="android:inputType">number</item>
</style>
Use like this:
<EditText
style="#style/Numbers" />
You can use styles.xml to create a style, then you can apply that styling to each button.
http://developer.android.com/guide/topics/ui/themes.html
in your values -> style.xml file put the xml attribute
<style name="buttonConfirm" parent="#android:style/Widget.Button">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">15dip</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
and in your layout call it by this xml
<Button
android:id="#+id/button1"
style="#style/buttonConfirm"
android:text="Button"
/>
I made a RadioButton with images.
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/radioType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioAlc"
style="#style/navbar_button"
android:checked="true"
android:drawableTop="#drawable/policeradio"
android:text="Barrage" />
<RadioButton
android:id="#+id/radioCrash"
style="#style/navbar_button"
android:drawableTop="#drawable/carcrashradio"
android:layout_marginLeft="-10dp"
android:text="Accidente" />
<RadioButton
android:id="#+id/radioMarch"
style="#style/navbar_button"
android:drawableTop="#drawable/marchradio"
android:layout_marginLeft="-10dp"
android:text="Manifestation" />
<RadioButton
android:id="#+id/radioRadar"
style="#style/navbar_button"
android:drawableTop="#drawable/radarradio"
android:text="Radar" />
</RadioGroup>
the problem is that with
<item name="android:button">#null</item>
I can remove the radio button, to let the user click the image, but it seems that it keeps the space to show it. In my case, I would like to remove that extra space that is bothering me !
Is it possible???
I apply that style
<style name="navbar_button">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">#null</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">12dp</item>
</style>
I don't know if you use other styles we can't see here, but if I copy your code in a layout file and add this to my styles.xml:
<style name="navbar_button">
<item name="android:button">#null</item>
</style>
it works fine and there is no extra space.
EDIT:
You have to add the item <item name="android:padding">0dp</item> to your navbar_button style, then it works on Android 2.3, too.