Android - How to change the background color or the entire TabLayout dynamically? - background-color

I need to change the background color of my entire tab layout dynamically through code in my app. I just need to know the correct line to type. I don't need to change the selected tab or anything. I want to leave the style and text colors and the indicator line as is. I just want to change the background of the entire tab layout dynamically in code. Thank you for your help.
I have already tried the below line of code. Didn't work. Also included my TabLayout XML.
private TabLayout mTabLayout;
mTabLayout = (TabLayout) findViewById(R.id.tabs);
mTabLayout.setBackgroundColor
(this.getResources().getColor(R.color.dark_grey));
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
style="#style/CategoryTab"
android:layout_width="match_parent"
android:layout_height="#dimen/activity_generic_margin_28"
app:tabBackground="#color/app_bar"/>
Thank you for your help.

I solved it by checking the boolean that I have set for night theme prior to setting the content view in onCreate.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences nightModeSwitchState = getSharedPreferences("NightModeSwitchState", 0);
mNightModeSwitchState = nightModeSwitchState.getBoolean("NightModeSwitchState", false);
if (mNightModeSwitchState) {
setContentView(R.layout.activity_book_night);
} else {
setContentView(R.layout.activity_book);
}
I have two layouts made both include tabLayouts. One of them references a style file for tabLayout set to night mode colors with the dark background and the layout references a style file set to regular colors for the tablayout.
<style name="CategoryTab" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/CategoryTabTextAppearance</item>
<item name="tabBackground">#drawable/tab_regular_theme</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
<style name="CategoryTabNight" parent="Widget.Design.TabLayout">
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabSelectedTextColor">#android:color/white</item>
<item name="tabTextAppearance">#style/CategoryTabTextAppearance</item>
<item name="tabBackground">#drawable/tab_layout_dark_mode</item>
<item name="android:textColorSecondary">#android:color/white</item>
</style>
The style files reference different drawables that control the background colors as you can see above.. and here are the drawables...
Here is night mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_grey" android:state_selected="true"/>
<item android:drawable="#color/dark_grey"/>
</selector>
Here is regular mode drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/app_bar" android:state_selected="true"/>
<item android:drawable="#color/app_bar"/>
</selector>
Everything else is the layouts are identical as not to mess anything up just the style files for the tablayouts are changed. I hope this makes sense. I tested it and it works for me. I have tried so many other things and nothing worked. It is important to call Shared Preference to get the value before checking the boolean. I hope this helps someone.

Related

How to change the color of active indicator bottombavigationview material You? material3

I want to change the color of the selected area in the bottom navigation view material 3, but the color does not change in any way. I've tried selector, background, background Tint, and styles. All unsuccessfully.
Currently like this.
It should be like this.
You can change indicator color by overridng it's default style.
Default itemIndicatorStyle :
<style name="Widget.Material3.BottomNavigationView.ActiveIndicator" parent="">
<item name="android:width">#dimen/m3_bottom_nav_item_active_indicator_width</item>
<item name="android:height">#dimen/m3_bottom_nav_item_active_indicator_height</item>
<item name="marginHorizontal">#dimen/m3_bottom_nav_item_active_indicator_margin_horizontal</item>
<item name="shapeAppearance">#style/ShapeAppearance.Material3.NavigationBarView.ActiveIndicator</item>
<item name="android:color">?attr/colorSecondaryContainer</item>
just override the style according to your preference :
<style name="App.Custom.Indicator" parent="Widget.Material3.BottomNavigationView.ActiveIndicator">
<item name="android:color">#color/blue</item>
</style>
And then add this line to your bottomNavigationView xml code :
app:itemActiveIndicatorStyle="#style/App.Custom.Indicator"
you can use this function for change indicator color:
findViewById<BottomNavigationView>(R.id.bottomNavigationView) .itemActiveIndicatorColor = getColorStateList(R.color.white)
and set this annotatin for your onCreate function #RequiresApi(Build.VERSION_CODES.M)
look in the screenshot

Android Studio, change EditText selected text pointer colors

I've been making a chat for a project of mine but how to change the color of these pointers?
Setting the android:textCursorDrawable attribute to #null should result in the use of android:textColor as the cursor color.
or you can use Style look like this
<style name="AppTheme.EditText" parent="#style/Widget.AppCompat.EditText">
<item name="android:textColor">#color/white</item>
<item name="android:textColorHint">#8AFFFFFF</item>
<item name="android:background">#drawable/edit_text_background</item> // background (bottom line at this case)
<item name="android:textCursorDrawable">#color/white</item> // Cursor
<item name="android:textSelectHandle">#drawable/my_white_icon</item> // For pointer normal state and copy text state
<item name="android:textSelectHandleLeft">#drawable/my_white_icon</item>
<item name="android:textSelectHandleRight">#drawable/my_white_icon</item>
</style>
hope it help

ImageView or ImageButton highlight?

How make pressed effect like on attached sreenshots??
Normal state
Pressed
The easiest way to achieve this is to use a selector for drawable. A selector defines a set of multiple images for different states of a view.
Create an xml with following contents in your drawable folder and set it as the background or src of your view(Button/ImageButton).
eg:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blue_button_pressed" android:state_selected="true"/>
<item android:drawable="#drawable/blue_button_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/blue_button_default"/>
</selector>
Use two different images for your pressed and normal states.

AppCompat ActionBar Background Color

I have this style.xml inside my Android app:
<resources xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android">
<style name="OrangeTheme" parent="#style/Theme.AppCompat">
<item name="android:actionBarStyle" tools:ignore="NewApi">#style/OrangeTheme.OrangeActionBar</item>
<item name="android:windowActionBar">true</item>
</style>
<style name="OrangeTheme.OrangeActionBar" parent="#style/Widget.AppCompat.ActionBar">
<item name="android:background">#color/orange</item>
<item name="background">#color/orange</item>
<item name="android:backgroundStacked">#color/orange</item>
<item name="android:backgroundSplit">#color/orange</item>
<item name="android:titleTextStyle">#style/Theme.MyAppTheme.ActionBar.TitleTextStyle</item>
</style>
<style name="Theme.MyAppTheme.ActionBar.TitleTextStyle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
<item name="android:textColor">#color/black</item>
</style>
I have searched StackOverflow and Google, but nothing helped. I wasn't able to change the ActionBar background color.
Inside my MainActivity, I use this code:
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
I tried not to use AppCompat, but then my name has a lot of errors. It extends ActionBarActivity. If I change it to FragmentActivity I can't use getSupportActionBar() and without it my getActionBar(), is always null.
minSdkVersion 19
targetSdkVersion 21
So right now I am not able to change it. I tried renaming the parent of my style theme. I tried using andrioud:background and background. The only thing that is changing is when I use another parent like
Theme.App.Compat.Light or Theme.AppCompat.Light.DarkActionBar.
Does someone have an idea, how I could be able to change the colors and other stuff, on runtime?
With new AppCompat-v7 (v21) you can use new color theming attributes. See here.
Or alternatively you have to use <item name="actionBarStyle"> property (without prefix android).
To use background color with AppCompat, you need to also define the item name without the android: attribute.
for example:
<style name="MyActionBar" parent="Theme.AppCompat.Light">
<item name="android:background">#color/action_bar_background</item>
<item name="background">#color/action_bar_background</item> //<--background item without the android attribute
</style>
and then in your custom theme do the same thing for the custom actionbar style:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="android:actionBarStyle">#style/MyActionBar</item>
<item name="actionBarStyle">#style/MyActionBar</item>
</style>

Android Border Less Button Pressed State Background Color

How can we change the background colour of a Border less button when the button is pressed.
I used this:
android:background="?android:attr/selectableItemBackground"
android:text="#string/btn_skip"
Where my button Displays a label "SKIP" as defined in my "strings.xml".
Now When I test this it is Good Except It shows default "Blue" Colour when in Pressed State.
I want it to match it with other elements of my My UI theme that are "Orange" "#FF8800"
Any Ideas?
Thankx in advance...!
instead of using ?android:attr/selectableItemBackground you can create a xml in drawable in folder with following content.
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="#color/orange"/>
<item
android:state_enabled="false"
android:drawable="#color/default"/>
<item
android:drawable="#color/default"/>
</selector>
Updated:
so you save this files as : btn_drawable.xml in drawable folder.
Simply replace ?android:attr/selectableItemBackground
with *#drawable/btn_drawable.xml*
Thanks #Nimish-Choudhary ...! I followed your advice. and I have explained it further for clarity to all having same question as me as a beginner.
Create an xml with name say "btntransparent.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:dither="true">
<item
android:state_pressed="true"
android:drawable="#color/pressed"/>
<item
android:state_enabled="false"
android:drawable="#color/defaultColor"/>
<item
android:drawable="#color/defaultColor"/>
</selector>
Next add the colors "pressed" and "defaultColor" to another xml say "colors.xml"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="pressed">#CCFF8800</color>
<color name="defaultColor">#00000000</color>
</resources>
Here "#CCFF8800" is my pressed state Translucent Orange and "#00000000" is the 100% Transparent Black (;-) thats invisible). You can find more about color codes at Color| Android Developers
Now put the "colors.xml" in values Folder and "btntransparent.xml" in drawable Folder.
Now the last step is to voila use it in the button as
<Button
android:id="#+id/id_btn_skip"
android:background="#drawable/transparentbtn"
android:onClick="skipToMain"
android:text="#string/btn_skip" />
And thats should do the job...!
Take care.
regards,
raisedadead
If I understand you correctly, what you could do is add new resource in #drawable for example my_ripple.xml
<?xml version="0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#FF8800">
</ripple>
Then simply set this as your background.
In your theme add the following:
<item name="android:selectableItemBackground">#FF8800</item>
to override the default blue.

Resources