I am adding an actionbar to a test app I'm writing, and I see questions throughout stackoverflow about this, but nothing that has helped me at all. Based off of this guide:
http://android-developers.blogspot.com/2011/04/customizing-action-bar.html
I'm trying to change the select color for tabs that i'm using on my action bar. The default is that faint blue color. Just as a test I did this:
<style name="CustomTab" parent="android:style/Widget.Holo.Light.ActionBar.TabView">
<item name="android:background">#000000</item>
</style>
That gives me a solid black tab completely, not the selector part. Can someone help better direct me here? I can't seem to find a good example anywhere.
First you have to define a selector xml file then write this code there and replace your code with this
item name="android:background">#drawable/yourfilename</item>
and the selector xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="#drawable/picture_selected" />
<item
android:state_focused="true"
android:drawable="#drawable/picture_selected" />
<item
android:drawable="#drawable/picture_unselected" />
</selector>
Related
this is a snip/screenshot of my app design in Adobe Illustrator: https://imgur.com/a/7tXii
Depending on the menu you are currently in, the respective section shall be highlighted as shown below (light blue in this case).
I know that you can change the ITEM/ICON color via a custom ThemeOverlay that you create under /styles.xml and by adding app:theme="#style/afore_mentioned_theme_overlay" like this:
<style name="ThemeOverlay.AppCompat.navTheme">
<!-- Color of text and icon when SELECTED -->
<item name="colorPrimary">#color/color_of_your_choice</item>
<!-- Background color when SELECTED -->
<item name="colorControlHighlight">#color/color_of_your_choice</item>
</style>
However, all this does is change the icon color upon selection, instead of highlighting the section below the icon.
The main problem is, probably, that the navigation background is a horizontal bar along the entire screen, but I just want to change the color of 33% of it, depending on the selected item. This will probably require a dirty workaround (?).
Not that much of a dirty workaround required. Just add this after you initialized your BottomNavigationView instance:
navigation.setItemBackgroundResource(R.drawable.menubackground);
and put this inside /drawable/menubackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorAccent" android:state_pressed="true" />
<item android:drawable="#color/colorAccent" android:state_checked="true" />
<item android:drawable="#color/colorPrimary" />
</selector>
Looks like this:
I use SlidingTabLayout which I grab it from here. There I found a almost a perfect solution for my tab problem. However, I still have some issues with this exapmle. The problem with that example is that it uses the same color for selected tabs and unselected ones. But I want to change the background of the tab when it is selected. I tried to put a selector background to generated tabs on createDefaultTabView(Context context) method in SlidingTabLayout.java
When I do that, indicators are just gone away. I tried to put that selector on onDraw() function in SlidingTabStrip.java but the result was the same.
So, can you tell me a way out?
By the way, I am using a ToolBar. This is why I use SligingTabStrip (as I read, TabHost cannot work with ToolBar). Maybe I am wrong with that as well.
To change the color of selected tab change the selector file like
SlidingTabLayout.java
tabTitleView.setTextColor(getResources().getColorStateList(
R.color.selector));
selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/white"/>
<item android:state_focused="true" android:color="#android:color/blue"/>
<item android:state_pressed="true" android:color="#android:color/green"/>
<item android:color="#ccc"/>
</selector>
I'm trying so set the text color on sliding tabs within a custom view according to the tab state. It's working but not on initial display.
The textcolor in the custom view should be white when the tab is selected and darker grey otherwise. I got it partly working already: When I select a tab manually the text color changes correctly.
My problem is that on first display the text of the first tab, whose content is displayed initially and before any user interaction with the tabs, is not colored in the active state color (white). Its grey, like the inactive tabs. If I start interacting with the tabs, everything works, but the initial tab color of the first displayed tab is wrong.
[FIRST TAB | SECOND TAB]
^
Grey (inactive color) on first load
EDIT:
The cause seems to be in the styles.xml: If I declare a custom style that inherits from Widget.Design.TabLayout the initial coloring does not work anymore. If I do not declare custom styles for the tablayout, everything works fine!
EDIT2:
No, it doesn't work. It works without setting the custom view but it still does not do the initial coloring correctly when first loading the tabs with setting the custom view.
Here is my code:
tab_gallerylayout.xml (custom tab view)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mbprojects.retro.view.RetroTextView
android:id="#+id/tab_title"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#drawable/gallerytab_title_color"
app:font="#string/font_name_syncopate"
/>
</LinearLayout>
gallerytab_title_color.xml (selector)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#android:color/white" />
<item android:state_selected="false" android:color="#android:color/darker_gray" />
<item android:state_focused="true" android:color="#android:color/white" />
<item android:state_pressed="true" android:color="#android:color/white" />
<item android:color="#android:color/darker_grey" />
</selector>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="#style/Theme.AppCompat.NoActionBar">
</style>
<style name="AppTheme.Base.Widget.Design.TabLayout" parent="Widget.Design.TabLayout">
<item name="tabMaxWidth">#dimen/tab_max_width</item>
<item name="tabIndicatorColor">#android:color/white</item>
<item name="tabIndicatorHeight">0dp</item>
<item name="tabPaddingStart">12dp</item>
<item name="tabPaddingEnd">12dp</item>
<item name="tabBackground">#android:color/black</item>
<item name="tabTextAppearance">#style/TextAppearance.Design.Tab</item>
</style>
Any suggestions?
The reason for this unusual behaviour is that your viewPager sets first item as current internally in viewPager.setAdapter(). This in turn makes tabLayout to select first tab in tabLayout.setupWithViewPager().
After this when you set custom tabs, state of custom view is not changed to "selected" (because select initial tab code already completed before this).
So, you need to explicilty change the state of custom view of selected tab.
Add below line after you set custom tabs and you're good to go.
mTabLayout.getTabAt(mInitialTab).getCustomView().setSelected(true);
You should use android:selector in your xml instead of the android:textColor. The former is for dynamically changing an attribute while the latter is assigning one value and keeping it
I made a layout that is just simply a textview that says "What do you want?", followed by a series of buttons underneath it.
All of the buttons can be clicked/touched, but when I scroll with the trackball, none of them become highlighted. I noticed, however, then when I disable the background colors on the buttons, I can see the orange box that shows that button's focus.
Is there any way I can visibly see the focus while still being able to have a background color on the buttons?
EDIT: Found the solution! This helped A LOT. Standard Android Button with a different color
Create a "selector" resource in your res/drawable. It can look something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#color/white" />
<item
android:state_pressed="true"
android:drawable="#color/orange" />
<item
android:state_selected="true"
android:state_pressed="false"
android:drawable="#color/blue" />
</selector>
Then set the background of your button to be:
android:background="#drawable/your_selector"
Rather than applying a simple background color to buttons, try applying a ColorStateList instead.
To do so, define a new XML file at
/res/color/buttonstate.xml
and use code such as the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="# FOCUSED COLOR HERE"
android:state_focused="true" />
<item android:drawable="# DEFAULT COLOR HERE" />
</selector>
Notes:
You can definitely add more colors for more states, such as pressed, enabled, and certain other factors.
In the layout or code just reference R.color.buttonstate or #color/buttonstate (the XML's filename).
Make sure the default color is last. This is because it goes down the list and finds the first item that has all of the states the same as it. If you don't provide android:state_focused="false" for the default item and put it first, it will always display.
You can do a similar thing with drawables and
nine-patch drawables to make your own custom button styles.
Rather than just change the background color, consider using a 9-patch style. This is more work to begin, but you'll have much more control over your app's appearance.
Modify your Button layout to look something like this (the style line is the kicker):
<Button
style="#style/PushButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Your styles.xml resource file then should contain a style similar to this:
<style name="PushButton">
<item name="android:background">#drawable/btn</item>
</style>
Then the btn.xml (put in in res/drawable) contents should look something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_focused"
android:state_pressed="false"
android:state_focused="true" />
<item android:drawable="#drawable/btn_default"
android:state_focused="false"
android:state_pressed="false" />
You would then use some image editor to create files named btn_pressed.9.png, btn_focused.9.png, and btn_default.9.png. Drop these files in your res/drawable.
A good starting point is the Google IO app (I lifted the code examples from it). Just grab the png files and modify them to match your desired style.
Keep in mind you can put all sorts of stuff in the style now, like text size, height and width.
I have many custom buttons (ToggleButton) in my app and want to apply different styles for each button. I created a selector for all the buttons and I currently change only the drawable for the button, like this:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected" />
<item
android:drawable="#drawable/button_gradient" />
</selector>
When I try to change the style the same way:
<item
android:state_checked="true"
android:drawable="#drawable/button_gradient_selected"
style="#style/button_checked />
It does not work, I have tried to change the drawable in the style instead (and just stated the style in the selector), I've also tried to create a separate selector but nothing seems to work.
Any ideas?
Right now it's not possible to do this.