Created
April 29, 2020 18:07
-
-
Save vfontjr/929f3867dae069816a658471a4ff043f to your computer and use it in GitHub Desktop.
Source code for https://victorfont.com/simple-advanced-lesson-android-programming-changing-tabwidget-tab-colors/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Copyright (C) 2008 The Android Open Source Project | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a> | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | |
implied. See the License for the specific language governing | |
permissions and limitations under the License. | |
--> | |
<selector xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"> | |
<!-- Non focused states --> | |
<item android:state_focused="false" android:state_selected="false" | |
android:state_pressed="false" | |
android:drawable="@drawable/tab_unselected_v4" /> | |
<item android:state_focused="false" android:state_selected="true" | |
android:state_pressed="false" | |
android:drawable="@drawable/tab_selected_v4" /> | |
<!-- Focused states --> | |
<item android:state_focused="true" android:state_selected="false" | |
android:state_pressed="false" | |
android:drawable="@drawable/tab_focus" /> | |
<item android:state_focused="true" android:state_selected="true" | |
android:state_pressed="false" | |
android:drawable="@drawable/tab_focus" /> | |
<!-- Pressed --> | |
<item android:state_pressed="true" | |
android:drawable="@drawable/tab_press" /> | |
</selector> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void setTabColors() { | |
/** | |
* Before we do anything, determine saved value of IsLightColors | |
*/ | |
MyApp app = (MyApp) this.getApplication(); | |
IsLightColors = app.RetrieveBoolean(getString(R.string.LightColorsKey)); | |
// set the color scheme and resource based on IsLightColors user prefs | |
int tab_indicator = IsLightColors ? R.drawable.tab_indicator_v4_light : R.drawable.tab_indicator_v4; | |
int layoutColor = IsLightColors ? Color.WHITE : Color.BLACK; | |
View myLayout = findViewById(R.id.main_layout); | |
myLayout.setBackgroundColor(layoutColor); | |
// set the background resource for each tab | |
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { | |
tabHost.getTabWidget().getChildAt(i).setBackgroundResource(tab_indicator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment