in my listview, when user long press on a item, i draw a custom actionbar and provide user option to delete multiple items at a time.
by default if i perform long press action, i will get selection color as blue and it gets disappear.
To overcome i tried adding a selector like this.
listviewselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/BLUE" />
</selector>
If i set this selector, when user performs long press i can see red color, but after wards if user performs selection, no color is getting retained on item. By default it looks white.
I tried setting background color based on condition like below
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(REDCOLOR);
}
else
{
convertView.setBackgroundColor(WHITE);
}
In this case, if user performs long press and then if user select multiple items i could see red color and by default all the items color will be white. But if user touches any item the default color will be nothing i.e no color appears on selection.
How to get default white color, on tap blue color and upon multiple selection red color?
i tried like this
New selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/WHITE" />
</selector>
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.RED));
}
else
{
convertView.setBackgroundColor(R.drawable.listviewselector);
}
In this i get by default all items blue color. why?
i tried like this, it worked.
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.BLUE));
}
else
{
convertView.setBackgroundColor(android.R.drawable.list_selector_background);
}
Related
I'm trying to make this NavigationBottomView:
All I want is to make selected item text instead of icon.
I googled it and tried to make custom navigationBottomView item, but I found nothing like what I want.
is it possible to be a text ? or even can i hide the icon and display item title only ?
Yes it's.
To control toggling BottomNavigationView item text, use app:itemTextColor with a custom selector
In your case you need to show up the text when an item is checked, and hide it otherwise.
To control toggling the item icon, use app:itemIconTint with a custom selector
In your case you need to show up the icon when an item is unchecked, and hide it otherwise.
For both text/icon cases you can use a transparent color as a hack for the hidden state.
Example:
<com.google.android.material.bottomnavigation.BottomNavigationView
...
app:itemTextColor="#drawable/text_selector"
app:itemIconTint="#drawable/icon_selector"
text_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFEB3B" android:state_checked="true" />
<item android:color="#android:color/transparent" android:state_checked="false" />
</selector>
icon_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFEB3B" android:state_checked="false" />
<item android:color="#android:color/transparent" android:state_checked="true" />
</selector>
UPDATE
i tried it before but all i got icon get hidden and text still at the
bottom not shifted to the center like gif u sent
You can have the same behavior by changing the BottomNavView text size:
Create the following style to increase the item text:
<style name="BottomNavigationViewActiveItemText" parent="#style/TextAppearance.AppCompat.Caption">
<item name="android:textSize">20sp</item>
</style>
Apply it to the BottomNavigationView with app:itemTextAppearanceActive="#style/BottomNavigationViewActiveItemText"
If your item text can be in two lines, use also:
<style name="BottomNavigationStyle">
<item name="android:gravity">center</item>
<item name="android:lines">2</item>
</style>
And apply it with android:theme="#style/BottomNavigationStyle"
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'm trying to get my ListView to have three background color states.
normal
pressed (when the user's finger is touching)
context menu active (when the context menu appears for a specific item)
note: I don't have a need for a selected state when the context menu is NOT active.
Basically, the part I'm having a hard time with is #3. When a user long-presses a listview item, I want the context menu to show, but I also want the item they pressed to highlight a different color.
In code, I have this
// note: the names are just to tell you what view type we're dealing with.
// android_Widgit_ListView: the ListView
// android_Views_View: the Cell
if (android_Widgit_ListView != null)
{
android_Views_View.Background = ContextCompat.GetDrawable(context, Resource.Drawable.listview_selector);
android_Widgit_ListView.SetSelector(Resource.Drawable.listview_selector);
android_Widgit_ListView.CacheColorHint = Color.Transparent.ToAndroid();
}
And I have a selector (note the different colors just to see what's up)
<!-- listview_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/PrimaryMediumBlue"/>
<item android:state_activated="true" android:drawable="#color/PrimaryYellow"/>
<item android:state_selected="true" android:drawable="#color/SecondaryWarmGrey"/>
<item android:state_checked="true" android:drawable="#color/SecondaryGreen"/>
<item android:state_pressed="true" android:drawable="#color/SecondaryLightGrey"/>
<item android:drawable="#color/white"/>
</selector>
The problem is that when I long-press the item to bring up the context menu, the list view item returns to white.
How do I get that state to stay a different color?
Its a bit of a workaround but...
You could bind a color changing function to a property in the context menu.
ex: a bool called "active" that you set "OnAppering" and "OnDisapering".
Implement INotifyPropertyChanged and fire an event when the active property is changed.
and then listen for the event in the listview and change the color on that.
-
i am not sure this is the best way...
<style name="MyTheme" parent="MyTheme.Base">
<item name="android:colorLongPressedHighlight">#color/ListViewHighlighted</item>
</style>
<color name="ListViewHighlighted">#A8A8A8</color>
add this on Resourses>Values>Styles.xml
I have an image button (star) which is used to mark something as favorite. I envision that when the user clicks on the star, the star will turn yellow. When they click on an already yellow start, it will go back to normal.
Transition from one color to the other would make a call to the server. I am doing that part already.
To change the color on click I did this.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:drawable="#drawable/ic_action_ic_action_star" />
</selector>
However, this changes the color only for the time being when the buttons is clicked. It doesn't remain changed on the click, in other words, it doesn't toggle.
How can I toggle the color of a button on each click?
Use android:state_selected in the state list along with View.setSelected(boolean selected) in your Java code.
I am using drawables to nicely style my buttons, and that works fine, except for the text color in the button.
I have defined a state_enabled="false" item in a selector and using setEnabled gives me the right button styles, but I have to jump through quite some loops to get the text color different. This code for example doesn't work (it shows no, or black, text when disabled, and darkgray when enabled):
public void setButtonsEnabled(boolean enable) {
btnAccept.setEnabled(enable);
btnDecline.setEnabled(enable);
int color = R.color.White;
if (!enable) {
color = R.color.DarkGray;
}
btnAccept.setTextColor(color);
btnDecline.setTextColor(color);
}
I found the solution.
The key lies in also setting the TextColor to a selector in res/colors:
android:textColor="#color/button_text"
android:background="#drawable/button_selector"
For the background selector I used this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:drawable="#drawable/btn_buddy_enabled"></item>
<item android:state_enabled="false" android:drawable="#drawable/btn_buddy_disabled"></item>
<item android:state_enabled="true" android:state_pressed="true" android:drawable="#drawable/btn_buddy_clicked"></item>
</selector>
And the textColor selector is this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="false" android:color="#color/White"></item>
<item android:state_enabled="false" android:color="#color/Gray"></item>
<item android:state_enabled="true" android:state_pressed="true" android:color="#color/White"></item>
</selector>
Simply calling setEnabled() will make everything work fine.
You are using the wrong value for the color. R.color.White returns the resource ID of the value, not the value itself. Try Color.WHITE, or getResources().getColor(R.color.White)
Have you checked out ColorStateLists? They are pretty awesome. So basically apply all those ideas of Drawable selectors to a set of colors.
Make a folder called [Your Project]/res/colors/ and then put an xml file in there called, button_color.xml (or whatever).
button_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- Any Enabled button, gets White Text -->
<item
android:color="#color/White"
android:state_enabled="true" />
<!-- Buttons with any other state, get DarkGray Text -->
<item
android:color="#color/DarkGray"/>
</selector>
And then for your TextView, you can just do something like, mTextView.setTextColor(R.color.button_color); At that point there is no need for that if/else type of logic, the selector will do it for you. The selector gets rolled up into the color resource but the class it actually generates is called a ColorStateList in case you find it referenced in other documentation.