1

Edit: My question is different because the answer referenced is not trying to access and modify the drawable (which is what my question is about), but it is just replacing the drawable.


I'm trying to change the color of a shape within a drawable within a layout within a button.

My question is: How do I access the id for the shape element programmatically?

Here is the drawable that contains the shape within a selector (It is called 'ic_mf_account_icon.xml'):

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:id="@+id/accountShape">
                <shape xmlns:android="http://schemas.android.com/apk/res/android"
                    android:shape="oval">
                    <solid android:color="@color/btn_bg_normal" />
                    <corners android:radius="@dimen/programmatic_btn_corner_radius" />

                </shape>

            </item>
            <item android:right="10dp" android:left="10dp" android:top="10dp" android:bottom="10dp" android:drawable="@drawable/ic_server"/>

        </layer-list>
    </item>
</selector>

Here is the layout that contains the drawable I just listed (The drawable is referenced in the Button at the top):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/FilesListItem" >

<FrameLayout style="@style/FilesListContent" >

    <Button
        android:id="@+id/icon"
        android:layout_width="36dip"
        android:layout_height="36dip"
        android:background="@drawable/ic_mf_account_icon" />

    <include layout="@layout/files_list_item_overlay" />
</FrameLayout>

<LinearLayout
    android:layout_width="0dip"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_weight="1"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:paddingLeft="10dip"
        android:singleLine="true" />
</LinearLayout>

<ImageButton
    android:id="@+id/info"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|right"
    android:background="@null"
    android:padding="@dimen/info_icon_padding"
    android:src="@drawable/ic_info_grey_normal" />

</LinearLayout>

Here is where I'm trying to access the drawable. I can access the initial layout, but when I try to stem from there and access the drawable I get a null reference -- To note, This is within a fragments 'onCreateView(...)' function if that matters:

    public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_account_list, container, false);
        FindView find = FindView.inView(view);
//        AbsListView absListView = find.absList(android.R.id.list);
        AbsListView absListView = view.findViewById(android.R.id.list);

        absListView.setAdapter(arrayAdapter = new BaseListAdapter<Account>(getActivity(), accounts) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = layoutInflater.inflate(R.layout.fragment_account_list_item, parent, false);


                    View layoutView = convertView.findViewById(R.id.icon);
                    View drawableView = layoutView.findViewById(R.id.accountShape);
                    Drawable drawableBackground = drawableView.getBackground();
                    // From here I will endeavor to modify the drawables shape background.

Here is the code within the code just mentioned where I am trying to access the drawable from within the layout:

View layoutView = convertView.findViewById(R.id.icon);
View drawableView = layoutView.findViewById(R.id.accountShape);
Drawable drawableBackground = drawableView.getBackground();
Evan Sevy
  • 659
  • 1
  • 13
  • 25
  • 1
    Possible duplicate of [How to change a layer-list drawable?](https://stackoverflow.com/questions/8018435/how-to-change-a-layer-list-drawable) – Bö macht Blau Apr 05 '18 at 17:57
  • This allowed me to do what I needed. Though it's not an entirely direct answer to my question asked: See Borja's answer: https://stackoverflow.com/questions/19864337/how-can-i-change-colors-in-my-statelistdrawable – Evan Sevy Apr 05 '18 at 18:57
  • I'm happy that you've found another useful answer but strictly speaking this does not really answer your main question (" How do I access the id for the shape element programmatically"), it is also about state list drawables while your question is about layer list drawables. – Bö macht Blau Apr 05 '18 at 19:16
  • Perhaps someone will be able to answer it. – Evan Sevy Apr 05 '18 at 22:35

0 Answers0