Android - How can choose shader programmatically? - android

I implemented a shader in this way, the xml file is called horde_shader.xml, and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:endColor="#color/bordeaux"
android:angle="270">
</gradient>
</shape>
I have also implemented the following themes, in the related style.xml file that looks like this:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AllyTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/royalBlue</item>
<item name="colorAccent">#color/royalBlue</item>
<item name="android:navigationBarColor">#color/royalBlue</item>
<item name="colorShader">#drawable/ally_shader</item>
<item name="imageFaction">#drawable/ally</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:textColorHint">#android:color/white</item>
<item name="android:fontFamily">serif</item>
</style>
<style name="HordeTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/bordeaux</item>
<item name="colorAccent">#color/bordeaux</item>
<item name="android:navigationBarColor">#color/bordeaux</item>
<item name="colorShader">#drawable/horde_shader</item>
<item name="imageFaction">#drawable/horde1</item>
<item name="android:textColorSecondary">#android:color/white</item>
<item name="android:textColorPrimary">#android:color/white</item>
<item name="android:fontFamily">serif</item>
</style>
<style name="Theme">Theme</style>
<style name="AppTheme">AppTheme</style>
</resources>
I tried in an activity to set the background in this way:
<TextView
android:id="#+id/banner_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Esempio Titolo"
android:textAlignment="center"
android:fontFamily="serif"
android:textColor="#FFF"
android:paddingBottom="5dp"
android:background="?attr/colorShader"
android:layout_alignParentBottom="true"
android:textSize="17dp"/>

Related

EditText style background specified in theme doesn't apply to EditTex?

I am creating an EditText like image. (Actually, I made it.)
Created drawable xml and set EditText as background.
However, to manage this more easily, I created a style in the theme and assigned it to the style property in EditText, but it is not applied.
What's the reason?
(Or if there is a better way to create these EditTexts please let me know)
--
drawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="5dp"/>
<solid android:color="#color/gray_400"/>
</shape>
theme.xml
<!-- memo EditText style -->
<style name="MemoEditTextStyle" parent="Widget.AppCompat.EditText">
<item name="background">#drawable/edittext_memo</item>
<item name="android:textColor">#color/purple_200</item>
<item name="android:textColorHighlight">#color/gray_400</item>
<item name="colorControlActivated">#color/gray_400</item>
</style>
in xml
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
style="#style/MemoEditTextStyle"
android:hint="#string/hint_memo"
android:paddingLeft="10dp"
android:gravity="center_vertical"/>
result
The background is not applied to EditText because in the style you define a property <item name="background"> instead of <item name="android:background">.
The same result can be also achieved using Widget.MaterialComponents without the need of the drawable xml shape like in the below example:
Define a custom TextInputLayout.OutlinedBox Style:
<style name="Custom.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxBackgroundMode">outline</item>
<item name="boxBackgroundColor">#c2c0ba</item>
<item name="boxStrokeColor">#c2c0ba</item>
<item name="boxStrokeErrorColor">#FF0000</item>
<item name="boxCornerRadiusTopStart">5dp</item>
<item name="boxCornerRadiusTopEnd">5dp</item>
<item name="boxCornerRadiusBottomStart">5dp</item>
<item name="boxCornerRadiusBottomEnd">5dp</item>
<item name="boxStrokeWidth">0dp</item>
<item name="boxCollapsedPaddingTop">0dp</item>
<item name="hintEnabled">false</item>
<item name="errorIconDrawable">#null</item>
<item name="errorTextColor">#FF0000</item>
<item name="android:textColorHint">#B3B3B3</item>
<item name="materialThemeOverlay">#style/Custom.ThemeOverlay.TextInputEditText.OutlinedBox</item>
</style>
<style name="Custom.ThemeOverlay.TextInputEditText.OutlinedBox" parent="#style/ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="editTextStyle">#style/Custom.TextInputEditText.OutlinedBox</item>
</style>
<style name="Custom.TextInputEditText.OutlinedBox" parent="#style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
<item name="android:paddingTop">16dp</item>
<item name="android:paddingBottom">16dp</item>
<item name="android:paddingStart">10dp</item>
<item name="android:paddingEnd">10dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#color/black</item>
</style>
Usage:
<com.google.android.material.textfield.TextInputLayout
style="#style/Custom.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="MEMO"/>
</com.google.android.material.textfield.TextInputLayout>
Result:

android:theme="#style/..." doenst take background

I got the following background for an edit text:
edit_text_rounded_white.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#66c6c6c6"/>
<stroke android:width="0.1dp"
android:color="#66c6c6c6"/>
<corners android:radius="40dp"/>
</shape>
</item>
</selector>
in my styles.xml I define the following:
<style name="editText">
<item name="android:background">#drawable/edit_text_rounded_white</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textCursorDrawable">#drawable/edit_text_cursor</item>
<item name="colorControlNormal">#color/colorPrimaryDark</item>
<item name="colorControlActivated">#color/transparentWhite50</item>
</style>
When I set the style via: style="#style/editText" for my editTexts it takes the background but doesnt take the colorControl I set. When I set the style via android:theme="#style/editText" it takes the colorControls but doesnt show the background image. I already tried src and windowBackground instead of background in the style file.
Any ideas how achieve both? Showing background and the colorControls?
Split your style into two:
<style name="editTextStyle">
<item name="android:background">#drawable/edit_text_rounded_white</item>
<item name="android:drawablePadding">5dp</item>
<item name="android:paddingBottom">2dp</item>
<item name="android:paddingTop">2dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:textCursorDrawable">#drawable/edit_text_cursor</item>
</style>
<style name="editTextTheme">
<item name="colorControlNormal">#color/colorPrimaryDark</item>
<item name="colorControlActivated">#color/transparentWhite50</item>
</style>
And then apply both to your view:
<EditText
...
android:theme="#style/editTextTheme"
style="#style/editTextStyle"/>

set background of Navigation Drawer icon

In my Navigation drawer my listview icon look like this check image. So it's means that i have imposed the icon on the background.
I know through the Frame Layout, i can put the icon on a image but i'm confused how can i do it when it's declared in a res/menu folder
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#drawable/ic_home_white_24dp"
android:title="#string/nav_item_home" />
<item
android:id="#+id/appointment_history"
android:icon="#drawable/appointment_icon"
android:title="#string/nav_history" />
<item
android:id="#+id/contact_us"
android:icon="#drawable/contact_us_icon"
android:title="#string/nav_contact_us" />
<item
android:id="#+id/nav_settings"
android:icon="#drawable/setting_icon"
android:title="#string/nav_setting" />
</group>
</menu>
new Image
Style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="RatingBar" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">#00BFF3</item>
<item name="colorControlActivated">#00BFF3</item>
</style>
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/background_splash</item>
</style>
<style name="MyCustomToolBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="android:textColorPrimary">#color/colorAccent</item>
</style>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
</style>
<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
v21
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
</style>
First you need to set the icon tint to null.
navigationView.setItemIconTintList(null);
Then you could create a xml drawable like this, in this example i will name it rounded_corner_home.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item >
<shape android:shape="rectangle">
<solid android:color="#007f12" />
<corners android:radius="4dp" />
</shape>
</item>
<item>
<bitmap android:src="#drawable/ic_home_white_24dp"/>
</item>
</layer-list>
Then you change the icon propriety of the menu item.
<item
android:id="#+id/nav_home"
android:icon="#drawable/rounded_corner_home"
android:title="#string/nav_item_home" />
Here a screenshot of the result: Screen
Using this code you can change the color of drawer icon :
o NavigationView (it is located in activity_main.xml layout file) The default tint is black but you can use an even darker shade of black by using #000000
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#000000"
app:menu="#menu/activity_main_drawer" />

No Ripple Effect With SelectableItemBackground

I'm trying to implement material design to legacy code.
I want a button to have a ripple effect on click but it shows nothing.
I'm using default ?android:attr/selectableItemBackground.
this is button xml:
<Button
android:text="Wandio"
android:padding="10dp"
android:textColor="#ffffff"
android:background="?android:attr/selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/wandio_link" />
style in values folder:
<resources>
<color name="main_green">#45bb61</color>
<style name="AppTheme" parent="AppTheme.Base">
</style>
<style name="Theme.Splash" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#909CD4</item>
<item name="colorPrimaryDark">#D490CA</item>
<item name="colorAccent">#FFFFFF</item>
<item name="android:imageButtonStyle">#style/ImageButtonStyle</item>
<item name="android:windowBackground">#color/main_green</item>
</style>
<style name="ImageButtonStyle">
<item name="android:background">#android:color/transparent</item>
</style>
</resources>
style in values-v21 folder:
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">#android:transition/move</item>
<item name="android:windowSharedElementExitTransition">#android:transition/move</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:fitsSystemWindows">true</item>
<item name="android:imageButtonStyle">#style/StyleApi21</item>
<item name="android:buttonStyle">#style/StyleApi21</item>
</style>
<style name="StyleApi21">
<item name="android:background">?android:attr/selectableItemBackgroundBorderless</item>
</style>
Never had this problem before. Can't find what I'm missing.
Any suggestions?
I tryed the same thing and I have used this in my custom button xml:
<item ><ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="oval">
<solid android:color="#FFBB00" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent" />
</shape>
</item>
</ripple>
Here is my another xml for older devices:
<item>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/fab_selected" android:state_selected="true"/>
<item android:drawable="#drawable/fab_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/fab_normal"/>
</selector>
</item>
These drawable files are just different colors of the button.

I can not add a style using the selector

I have styles
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextStyle" >
<item name="android:textSize">8sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
</style>
<style name="TextStylePressed" >
<item name="android:textSize">18sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#C2C2C2</item>
</style>
</resources>
and I use selector arrow.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#style/TextStylePressed" android:state_selected="true"/>
<item android:drawable="#style/TextStylePressed" android:state_pressed="true"/>
<item android:drawable="#style/TextStylePressed" android:state_focused="true"/>
<item android:drawable="#style/TextStyle" />
</selector>
The problem is that I can not understand - if it works?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#drawable/arrow" // <---- Framed here
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/arrow" // <---- then here
android:orientation="vertical" >
</LinearLayout>
Consequently, after all attempts nothing happens
AFAIK selectors can only be defined to select a drawable and not any other attribute. If you check the doc , it compiles to StatelistDrawable, So i believe it can only be used for drawables

Categories

Resources