2

I would like to change solid color in my drawable pragmatically.

source:

link

@drawable/rightmenu_categories_title :

<item
    android:id="@+id/drawable_main_title_details_content_an_item"
    >
    <shape android:shape="rectangle">

        <solid android:color="#f4262b"/>
        <corners android:topLeftRadius="4dp"/>

    </shape>

</item>

my layout :

<RelativeLayout
    android:id="@+id/lay_rel_title_categories_rightmenucontents" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    >

    <TextView
        android:id="@+id/txt_view_title_detalis_content_an_item" 
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_width="120dp"
        android:layout_height="40dp"
        android:textSize="16sp"
        android:gravity="center"
        android:text="@string/str_title_categories_rightmenu"
        android:background="@drawable/rightmenu_categories_title"
        />

</RelativeLayout> 

private void setColorOnTitleDetailContentAnItem(int which){
    View v = getActivity().findViewById(R.id.txt_view_title_detalis_content_an_item);

    LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();
    final GradientDrawable shape = (GradientDrawable)  bgDrawable.findDrawableByLayerId(R.id.drawable_main_title_details_content_an_item);
    shape.setColor(Color.BLACK);
}

but I get follow errors :

> Caused by: java.lang.NullPointerException 02-01 19:10:05.326:
> E/AndroidRuntime(23755):  at
> ....ContentAnItem.setColorOnTitleDetailContentAnItem(ContentAnItem.java:241)
> 02-01 19:10:05.326: E/AndroidRuntime(23755):  at
> ....ContentAnItem.findViewsById(ContentAnItem.java:227)

on 241 line :

LayerDrawable bgDrawable = (LayerDrawable)v.getBackground();

on 227 line,my function is called .


solved :

in fragment class I must using rootView:

  View v = rootView.findViewById(R.id.txt_view_title_detalis_content_an_item);
Community
  • 1
  • 1
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

0 Answers0