android changing layer-list programmatically - android

I have this xml drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+map/marker_frame"
android:drawable="#drawable/map_pointer_green"/>
<item android:id="#+map/marker_profile_picture"
android:drawable="#drawable/questionmark"/>
</layer-list>
how can I change the item attribute from code? How can I access its id?
I want to be able to change each of the drawables from code according to scenarios.

What if use LayerDrawable in your code

Related

How can we make a bottomnavigationview with Image with iconTints?

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"

How to get drawable resource from boolean resource

Is there a way to get a drawable resource from a boolean resource?
For example:
bools.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="use_version_1_drawables">true</bool>
</resources>
my_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/my_drawable_version_1"/>
<item android:drawable="#drawable/my_drawable_version_2"/>
</selector>
Is there a specific state I should be using because from what I understand they are all related to specific events (checking, focusing, etc). Perhaps I shouldn't be using a selector. I simply want to have one resource I can call upon that's actually linking to two others but will select one based on my bool.
You are close. You can think of a selector as an if/else statement. What you're missing now is your actual state. Here is an example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimaryDark" android:state_checked="true" />
<item android:color="#color/gray" android:state_checked="false" />
</selector>
If you're using it with something like a Checkbox, then just set this selector as a drawable resource for the checkbox, and state_checked will be updated automatically.
EDIT: Problem was slightly more complex, and solved in the comments.

Make a API-dependent reference to a theme-style

API 21+:
android:background="?android:attr/selectableItemBackgroundBorderless"
API <21:
android:background="?android:attr/selectableItemBackground"
How is it possible?
I know the solution that uses "?attr/API_dependent_theme_reference" like this. This doesn't work for App Widget layout, unfortunately.
Also, I know the solution uses style="..." for the view and multiple style definitions in values and values-v21 folders. But looking for a way to directly assign the background property (especially as Android, unfortunately, don't support assigning multiple styles like CSS: class="style1 style2 etc"!).
You can have two custom drawables
drawable/backround_name.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="?android:attr/selectableItemBackground"
</layer-list>
and
drawable-v21/backround_name.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="?android:attr/selectableItemBackgroundBorderless"
</layer-list>

How can I use VectorDrawable with the Android Toolbar?

What is the proper method to use the new VectorDrawable in the toolbar?
I tried to use the app:srcCompat element as illustrated below, but nothing showed up.
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:srcCompat="#drawable/ic_clear"
app:showAsAction="ifRoom" />
</menu>
I have my own toolbar layout using android.support.v7.widget.Toolbar and Android Support Library v23.2 on JB (16).
Turns out it's quite easy.
Say, you have vector drawable vd_trash_24dp.
Describing MenuItem one cannot address VectorDrawable directly with android:icon. It seems ignoring app:srcCompat also.
But. As all we know ;)
AppCompat does support loading vector drawables when they are
referenced in another drawable container such as a StateListDrawable,
InsetDrawable, LayerDrawable, LevelListDrawable, and RotateDrawable
Let's try it, should we?
Create StateListDrawable vd_test_vd
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/vd_trash_24dp" />
</selector>
than
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item android:id="#+id/menu_action_filter"
android:title="#string/menu_action_filter"
android:icon="#drawable/vd_test_vd"
android:orderInCategory="100"
app:showAsAction="always"/>
</menu>
street magic indeed.
Yes, one could try and set drawable at runtime with MenuItem.setIcon(). But who need that %)

How to disable click style in android GridView

I have a android.widget.GridView and I'm setting setOnItemClickListener() on it. It works perfectly but adds a style to the UI when a cell is clicked. Any ideas how to remove this style?
Ok thanks #Sam you got me on the right track. Could not get <item android:color="#00000000" />to work, I think for grids you need to use drawables along the lines of Android ListView Selector Color. In the end my code that works is
Java code:
mygrid.setSelector( R.color.empty_selector );
res/color/empty_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/transparent"/>
</selector>
res/color/transparent.xml:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:gravity="center" android:src="#drawable/transparent" />
Not sure if there had been an easier way but this has worked so I'm happy.
I believe you are referring to the color selector or color state list. You can probably create a dummy selector, that has no alternate values, then set it to the GridView with setSelector() in Java or android:listSelector in XML.
A dummy selector, it is transparent:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:color="#00000000" />
</selector>

Categories

Resources