Hey I'm trying to add a selected state to my Nav Drawer. Everything is pretty boilerplate from the implementation here: http://developer.android.com/training/implementing-navigation/nav-drawer.html
There's some custom style in there and I'm trying to style a custom selected state for the List View (the list view that is actually the nav drawer)
I created a selector for the list items:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#color/darkerGrey" android:state_pressed="true"/>
<item android:drawable="#color/mediumGrey" android:state_selected="true"/>
</selector>
and I'm seeing the style work for the "pressed" state, but I'm not seeing the "selected" state work. In code to make it selected I'm using the ListView method setItemChecked:
mDrawerList.setItemChecked(0, true);
I've also set on my ListView the property
android:choiceMode="singleChoice"
I would greatly appreciate any advice! Thanks!
p.s. I've tried using "state_checked" as well, to no avail.
try setting this selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#color/darkerGrey" android:state_pressed="true"/>
<item android:drawable="#color/mediumGrey" android:state_selected="true"/>
</selector>
to the listView as:
android:listSelector="#drawable/your_selector"
or
to the list_row_layout that you use for inflating into the list as:
android:background="#drawable/your_selector"
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"
I wanna make a bottom navigationview like instagram. I have an image and to show it in this view I have to write this,
bottomNavigation.setItemIconTintList(null);
but after that I cant see which menu item is selected because color always same in every item. So any ideas ?
You should add the item_selector.xml such as :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:color="#color/colorPrimaryDark"/>
<item android:color="#android:color/darker_gray"/>
</selector>
and set it to your BottomNavigationView component that inside the xml file
app:itemTextColor="#drawable/nav_item_selector"
I am using Leanback BrowseFragment and need to be able to set background colour of header item when selected. Any direct method/XML attributes available to do that? I have been looking into BrowseFragment and leanback themes.xml.
Reference:
https://developer.android.com/reference/android/support/v17/leanback/app/BrowseFragment.html
https://android.googlesource.com/platform/frameworks/support/+/master/v17/leanback/res/values/themes.xml
In my case I used custom header items with icons. You can see details in this tutorial. For TextView from my layout I defined xml file in the color directory, something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/accent_color" android:state_selected="true"/>
<item android:color="#color/white"/>
</selector>
and the same xml files (but with android:drawable attributes ) in the direcroty drawable.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/icon_focused"
android:state_selected="true"/>
<item android:drawable="#drawable/icon"/>
</selector>
Then simply set android:textColor="#color/your_new_xml_file" for TextViewand use your drawable files for your header ImageView.
If you need to change View background, not only header icon and text color, set such xml file with defined colors as a View background.
OnSelectLevelChanged() method in my presenter class is empty (without this, like in tutorial above) :
holder.view.setAlpha(mUnselectedAlpha + holder.getSelectLevel() *
(1.0f - mUnselectedAlpha));
I am trying to develop application with Master/ Detail (Fragment) layout but I am facing a weird issue that is when i added my own listview adapter then the activation of the listview items on click/select stopped working.
The code provided in default with dummy data was
setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(
getActivity(), android.R.layout.simple_list_item_activated_1,
android.R.id.text1, DummyContent.ITEMS));
the code that I replaced is
CMyListAdapter adapter = new CMyListAdapter(getActivity(),
CMyContent.getSampleMyContentList());
setListAdapter(adapter);
Now, when i click the the listview item, it highlights and doesn't remain persistent.
If you are using a custom layout make sure you are settings the right background drawable:
android:background="?android:attr/activatedBackgroundIndicator"
In case you want to customize indicator's style you have to define your own StateListDrawable xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/red" android:state_pressed="true"/>
<item android:drawable="#color/blue" android:state_selected="true"/>
<item android:drawable="#color/green" android:state_activated="true"/>
</selector>
and assign it as your row layout background.
I have a listview activity in which I set the selector color using the following code. But when I select an item, the whole list gets highlighted with the selector color, which I don't want. Where Am I doing wrong? Any help is appreciated.
ListView lv = getListView();
lv.setFocusableInTouchMode(true);
lv.setBackgroundColor(Color.WHITE);
lv.setSelector(R.color.blue);
Use this way to use Selector
Create a xml in res/drawable and set the color for different event state
Then this xml as Selector
For example, let res/drawable/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:drawable="#color/gray" />
</selector>
Then declare gray in your res\values\colors.xml
<color name="gray">#808080</color>
Then set selector as
lv.setSelector( R.drawable.selector);
There's no need to make res/drawable/selector.xml.
Just add these lines in your onCreate method:
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(R.color.pressed_state_color));
selector.addState(new int[]{-android.R.attr.state_pressed}, new ColorDrawable(R.color.normal_state_color));
lv.setSelector(selector);
Sorry, I haven't enough reputation to add comments to previous answers.
Thanks for your example, it worked better for me this way...Try this instead.
Create a xml in res/drawable and set the color for different event state
Then this xml as Selector
For example, let res/drawable/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="#color/gray" />
</selector>
Then declare gray in your res\values\strings.xml
<color name="gray">#808080</color>
Then set selector as
lv.setSelector( R.drawable.selector);
lv.setDrawSelectorOnTop(true); // if you have background Drawable on your listView
You must set
android:state_selected = "true"
to
android:state_selected = "false"
Answers here doesn't work for custom views in ListView because of invalid background. Solution is to set the background of each item to android:background="?android:attr/activatedBackgroundIndicator". Here is example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="?android:attr/activatedBackgroundIndicator">
<!-- your item content-->
</LinearLayout>
After that, StateListDrawable works as expected.
Source: http://www.michenux.net/android-listview-highlight-selected-item-387.html,