Dynamically setting Styles for View : Android - android-layout

I want to change the look (Style) of various views dynamically in an
Activity. But on going through various blogs, I came to know that
android does not support dynamically setting styles for Views
programmatic / dynamic., though I am not sure.
1) Is there any other ways of implementing apart from making use THEMES or making use layout xml files having styles pre set ?
2) Is it possible to set the different styles for various views (say green colour text for TextView1 and red color text for TextView2) by making use of setTheme() for entire Activity?
Thanks

You can change the text color in your java file like this.
textview1.setTextColor(Color.GREEN);
textview2.setTextColor(Color.RED);

Use this xml file in as resource and add it in drawable folder.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:centerColor="#f77b2d"
android:endColor="#f99f66"
android:startColor="#f66a11" />
<stroke
android:width="0.5dp"
android:color="#313437" />
<corners
android:bottomLeftRadius="4dp"
android:bottomRightRadius="4dp"
android:topLeftRadius="4dp"
android:topRightRadius="4dp" />
</shape>

Related

How to implement themed icons by Material You in my Android App?

I can't find anything about that online, no documentation, nothing. Maybe one of you know how to do it or have got some advice for me.
Thank you in advance.
Update on 02/18/2022: Android 13 has official support for themed icons. Just follow their steps here: https://developer.android.com/about/versions/13/features#themed-app-icons
To follow their example:
Create a monochrome version of your app icon
Add this to your ic_launcher.xml file, under the <adaptive-icon /> tagset:
<adaptive-icon>
<background android:drawable="..." />
<foreground android:drawable="..." />
<monochrome android:drawable="#drawable/myicon" />
</adaptive-icon>
Add your icon to your manifest:
<application
…
android:icon="#mipmap/ic_launcher"
…>
</application>
Note: If android:roundIcon and android:icon are both in your manifest, you must either remove the reference to android:roundIcon or supply the monochrome icon in the drawable defined by the android:roundIcon attribute.
All of that was pulled directly from the Google developer's example.
There seems to be competing answers on this, but this issue on the Material Components GitHub repository seems to offer the most insight: https://github.com/material-components/material-components-android/issues/2357
Particularly, using an adaptive icon, like this:
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<background android:drawable="#color/launcher_icon_background"/>
<foreground>
<aapt:attr name="android:drawable">
<vector
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#color/launcher_icon"
android:pathData="m54,74.5l23.3,-29c-0.9,-0.7 -9.9,-8 -23.3,-8c-13.4,0 -22.4,7.3 -23.3,8l23.3,29l0,0c0,0 0,0 0,0zm7.7,-27.3c1.4,1.7 2.3,3.9 2.3,6.3l0,2l-20,0l0,-2c0,-2.4 0.9,-4.6 2.3,-6.3l-2.3,-2.3l1.4,-1.4l2.3,2.3c1.7,-1.4 3.9,-2.3 6.3,-2.3c2.4,0 4.6,0.9 6.3,2.3l2.3,-2.3l1.4,1.4l-2.3,2.3zm-9.7,4.3c0,1.1 -0.9,2 -2,2c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2c1.1,0 2,0.9 2,2zm8,0c0,1.1 -0.9,2 -2,2c-1.1,0 -2,-0.9 -2,-2c0,-1.1 0.9,-2 2,-2c1.1,0 2,0.9 2,2z" />
</vector>
</aapt:attr>
</foreground>
</adaptive-icon>
... with colors defined in colors.xml that refer to system-defined colors that derive from the Material 3 color pallet:
<resources>
<color name="launcher_icon_background">#android:color/system_accent1_100</color>
<color name="launcher_icon">#android:color/system_neutral1_800</color>
</resources>
Note that these colors derive from the system-defined pallet, as described in Material Design 3: https://m3.material.io/libraries/mdc-android/color-theming
You will likely have to create separate resource files that target API 31+ specifically. I have not tested out these recommendations.

Android Studio 4.1 bordered Button loses border setting background color in kotlin code

There are numerous threads for creating a bordered button in this forum.
It works for example perfect with this xml-file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFEB3B" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
where i fail is the following:
i need 2 bordered buttons, one is yellow, the other one blue. Due to program logic I want to switch the colors, the yellow one gets blue, the blue one to yellow.
in the xml-file
<solid android:color="#FFEB3B" />`<!--Background Color-->`
is used.
In Kotlin I only find
button.setBackgroundColor(Color.YELLOW)
But I find nor button.setColor oder button.setdologColor or anything else.
Using setBackgroundColor, the color changes, but the border disappears. Which statement would keep the border?
I solved it with
punktelinks.setBackgroundResource(R.drawable.shapecolory)
in code and with resource
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#FFFF00" />`<!--Background Color-->`
<stroke android:width="2dip" android:color="#000000"/> `<!--Border-->`
</shape>
This works (for me)

Gradient usage in android studio

I am the newly in android studio.
Just reading and studying in regards to the gradient usage.
Is it possible to use gradient colour for the floating button for it to fill the whole shape?
As well, is it possible to create the text view style using gradient colour?
of course create a background.xml drawable folder and write the code below
<?xml version="1.0" encoding="utf-8"?>
<shape>
<gradient android:startColor="#color/colorAccent"
android:centerColor="#color/colorAccent"
android:endColor="#color/black"
android:angle="90"/>
<corners android:radius="10dp"></corners>
</shape>

Reusing a border resource with multiple background colors

I currently have the following xml file in my /res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/transparent_white" />
<stroke
android:width="1dip"
android:color="#color/light_gray" />
</shape>
Sometimes I would like the background to be white, gray, blue etc. depending on the item I am trying to border. Is there a way to do this without creating n number of xml files where the only difference is the color of the solid attribute?
you could do this by dynamically declaring the shape and changing the color at runtime.
ShapeDrawable shapeDrawable= new ShapeDrawable();
shapeDrawable.setShape(new RectShape());
shapeDrawable.getPaint().setColor(<your color>);
((TextView) row.findViewById(<your viewid>)).setBackgroundDrawable(shapeDrawable);

Layout like Cards in android

I want to make an android layout like Google Cards,i know there is an Open Source Libraries, however i want only the Layout and the gray hex background style. I could not find information about this, how i can make this possible? im Attaching new Google maps v7 Layout so you can get the idea.
bg_card.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="#CCC" />
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#FFF" />
</shape>
</item>
</layer-list>
Or just set the background of the card to #FFF and add at the bottom:
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#CCC" />
NOTE:
For everyone who doesn't care about using a library, you should use
a CardView from the Android support library.
Sample code.
<item name="android:windowBackground">#ffe3e3e3</item>
In your style.xml should make you happy ;-)
And for the layout, copying the card background should be enough isn't it?
We can use the LayerList as above to achieve this (OR) by using the nine patch image get same effect, we need not create nine patch image separately for this puprpose android drawable have one.
if you are using eclipse: android:background="#drawable/abc_menu_dropdown_panel_holo_light"
copy paste the above line as your View backround.
or if you are using android studio copy paste the following line:
android:background="#android:drawable/dialog_holo_light_frame"

Resources