0

Before marking this as a duplicate, please read my question first.

I have a shape with the id "rectangle" which looks like this:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
    <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp" />
    <corners android:radius="3dp" />
    <solid android:color="#ff5900" />
</shape>

I use this shape in a cardview of mine:

<View
    android:id="@+id/myrectangle"
    android:layout_width="15dp"
    android:layout_height="wrap_content"
    android:background="@drawable/rectangle"
    android:layout_alignBottom="@+id/desc" />

And finally, I try to set the color in the RVAdapter of the cardview: (Color is a View and gd is a Drawable)

color = cv.findViewById(R.id.myrectangle);
gd = color.getBackground();
if (omission.gd instanceof ShapeDrawable) {
    ((ShapeDrawable)omission.gd).getPaint().setColor(Color.parseColor(stundenplandata.getJSONObject(i).getString("color")));
} else if (omission.gd instanceof GradientDrawable) {
    ((GradientDrawable)omission.gd).setColor(Color.parseColor(stundenplandata.getJSONObject(i).getString("color")));
} else if (omission.gd instanceof ColorDrawable) {
    ((ColorDrawable)omission.gd).setColor(Color.parseColor(stundenplandata.getJSONObject(i).getString("color")));
}

The stundenplandata.getjsonobject etc is just getting the hex code for the color.

However, when I do this, the color of the rectangle is not changing, it actually changes to white (remember, it is orange in the xml file).

What is my problem/mistake?

EDIT: Here's the view hierarchy:

com.android.internal.policy.PhoneWindow$DecorView{a4dd2b7 V.E..... ... 0,0-1080,1920}
    android.widget.LinearLayout{9e0aa24 V.E..... ... 0,0-1080,1920}
      android.view.ViewStub{457998d G.E..... ... 0,0-0,0 #10203be android:id/action_mode_bar_stub}
      android.widget.FrameLayout{a238742 V.E..... ... 0,0-1080,1920}
        android.support.v7.widget.FitWindowsLinearLayout{362e353 V.E..... ... 0,0-1080,1920 #7f0f007b app:id/action_bar_root}
          android.support.v7.widget.ViewStubCompat{7904490 G.E..... ... 0,0-0,0 #7f0f007c app:id/action_mode_bar_stub}
          android.support.v7.widget.ContentFrameLayout{4b14189 V.E..... ... 0,0-1080,1920 #1020002 android:id/content}
            android.support.v4.widget.DrawerLayout{a66d58e VFED.... ... 0,0-1080,1920 #7f0f00b2 app:id/material_drawer_layout}
              android.support.v4.widget.DrawerLayout{cae61af VFED.... ... 0,0-1080,1920 #7f0f00b2 app:id/material_drawer_layout}
                android.support.design.widget.CoordinatorLayout{45d19bc V.ED.... ... 0,0-1080,1920}
                  android.support.design.widget.AppBarLayout{55ce6b8 V.E..... ... 0,72-1080,240}
                    android.support.v7.widget.Toolbar{ce36d45 V.E..... ... 0,0-1080,168 #7f0f0094 app:id/toolbar}
                      android.support.v7.widget.AppCompatTextView{7ebc9a V.ED.... ... 48,43-383,124}
                      android.support.v7.widget.ActionMenuView{c8969cb V.E..... ... 960,0-1080,168}
                        android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{2c015a8 VFED..C. ... 0,12-120,156}
                  android.widget.RelativeLayout{12a7c91 V.E..... ... 0,240-1080,1920}
                    android.widget.FrameLayout{a5298c1 V.E..... ... 30,0-1050,1680 #7f0f0099 app:id/main_container_1}
                      android.widget.FrameLayout{5fc0866 V.E..... ... 0,0-1020,1680}
                        com.dnddev.sos.MyRecyclerView{bc2d7a7 VFED.... F.. 0,0-1020,1680 #7f0f00aa app:id/rv_lessons}
                          android.widget.LinearLayout{1d4e454 V.E..... ... 0,0-1020,491}
                            android.support.v7.widget.CardView{22efffd V.E..... ... 15,15-1005,476 #7f0f0095 app:id/lessonscard}
                              android.widget.RelativeLayout{ab044f2 V.E..... ... 0,0-990,461}
                                android.view.View{73b4743 V.ED.... ... 0,461-45,461 #7f0f00ad app:id/myrectangle}
                                android.support.v7.widget.AppCompatTextView{4c5f1c0 V.ED.... ... 45,54-615,310 #7f0f00af app:id/headline}
                                android.support.v7.widget.AppCompatTextView{2709ef9 V.ED.... ... 45,310-251,461 #7f0f00ae app:id/desc}
                                android.support.v7.widget.AppCompatTextView{7f9be3e V.ED.... ... 45,54-212,127 #7f0f00b0 app:id/initials}
                                android.support.v7.widget.AppCompatTextView{f41149f V.ED.... ... 737,54-990,127 #7f0f00b1 app:id/time}
                          android.widget.LinearLayout{4f869ec V.E..... ... 0,491-1020,982}
                            android.support.v7.widget.CardView{69331b5 V.E..... ... 15,15-1005,476 #7f0f0095 app:id/lessonscard}
                              android.widget.RelativeLayout{ccf804a V.E..... ... 0,0-990,461}
                                android.view.View{ac5bbb V.ED.... ... 0,461-45,461 #7f0f00ad app:id/myrectangle}
                                android.support.v7.widget.AppCompatTextView{af838d8 V.ED.... ... 45,54-484,310 #7f0f00af app:id/headline}
                                android.support.v7.widget.AppCompatTextView{c123431 V.ED.... ... 45,310-251,461 #7f0f00ae app:id/desc}
                                android.support.v7.widget.AppCompatTextView{e8d5716 V.ED.... ... 45,54-206,127 #7f0f00b0 app:id/initials}
                                android.support.v7.widget.AppCompatTextView{3baf897 V.ED.... ... 737,54-990,127 #7f0f00b1 app:id/time}
                          android.widget.LinearLayout{a240a84 V.E..... ... 0,982-1020,1473}
                            android.support.v7.widget.CardView{2a4e26d V.E..... ... 15,15-1005,476 #7f0f0095 app:id/lessonscard}
                              android.widget.RelativeLayout{a7fcea2 V.E..... ... 0,0-990,461}
                                android.view.View{cac8733 V.ED.... ... 0,461-45,461 #7f0f00ad app:id/myrectangle}
                                android.support.v7.widget.AppCompatTextView{b194af0 V.ED.... ... 45,54-517,310 #7f0f00af app:id/headline}
                                android.support.v7.widget.AppCompatTextView{43a3869 V.ED.... ... 45,310-251,461 #7f0f00ae app:id/desc}
                                android.support.v7.widget.AppCompatTextView{43032ee V.ED.... ... 45,54-212,127 #7f0f00b0 app:id/initials}
                                android.support.v7.widget.AppCompatTextView{a1e638f V.ED.... ... 737,54-990,127 #7f0f00b1 app:id/time}
                        com.melnykov.fab.FloatingActionButton{e0261c VFED..C. ... 804,1464-972,1632 #7f0f00ab app:id/lessons_fab}
                com.mikepenz.materialize.view.ScrimInsetsRelativeLayout{b4f225 I.ED.... ... -912,0-0,1920 #7f0f0018 app:id/material_drawer_slider_layout}
                  android.view.View{c708ffa G.ED.... ... 0,0-0,0 #7f0f00ca app:id/material_drawer_inner_shadow}
                  android.support.v7.widget.RecyclerView{d27a9ab VFED.V.. ... 0,0-912,1920 #7f0f00c9 app:id/material_drawer_recycler_view}
              com.mikepenz.materialize.view.ScrimInsetsRelativeLayout{4d78808 I.ED.... ... -912,0-0,1920 #7f0f0018 app:id/material_drawer_slider_layout}
                android.view.View{9678ba1 G.ED.... ... 0,0-0,0 #7f0f00ca app:id/material_drawer_inner_shadow}
                android.support.v7.widget.RecyclerView{d27b1c6 VFED.V.. ... 0,0-912,1920 #7f0f00c9 app:id/material_drawer_recycler_view}
                  android.widget.LinearLayout{7353587 V.....CL ... 0,0-912,543 #9e626c54}
                    android.widget.FrameLayout{5611cb4 V.E...C. ... 0,0-912,516}
                      android.support.v7.widget.AppCompatImageView{85040dd V.ED.... ... 0,0-912,516 #7f0f00b3 app:id/material_drawer_account_header_background}
                      android.widget.RelativeLayout{5dd2452 V.E..... ... 0,0-912,516 #7f0f00b4 app:id/material_drawer_account_header}
                        com.mikepenz.materialdrawer.view.BezelImageView{9a5a323 V.ED..CL ... 48,120-240,312 #7f0f00b8 app:id/material_drawer_account_header_current}
                        android.widget.LinearLayout{34d5020 V.E..... ... 912,120-912,120}
                          com.mikepenz.materialdrawer.view.BezelImageView{f150dd9 G.ED..C. ... 0,0-0,0 #7f0f00b5 app:id/material_drawer_account_header_small_first}
                          com.mikepenz.materialdrawer.view.BezelImageView{505339e G.ED..C. ... 0,0-0,0 #7f0f00b6 app:id/material_drawer_account_header_small_second}
                          com.mikepenz.materialdrawer.view.BezelImageView{3254e7f G.ED..C. ... 0,0-0,0 #7f0f00b7 app:id/material_drawer_account_header_small_third}
                        android.widget.LinearLayout{a074e4c V.E..... ... 0,324-912,492 #7f0f00b9 app:id/material_drawer_account_header_text_section}
                          android.support.v7.widget.AppCompatTextView{9bfae95 V.ED.... ... 48,27-744,84 #7f0f00ba app:id/material_drawer_account_header_name}
                          android.support.v7.widget.AppCompatTextView{a0cebaa V.ED.... ... 48,84-744,141 #7f0f00bb app:id/material_drawer_account_header_email}
                        android.support.v7.widget.AppCompatImageView{6ca539b V.ED.... ... 798,408-864,474 #7f0f00bc app:id/material_drawer_account_header_text_switcher}
                    android.view.View{e810338 V.ED.... ... 0,516-912,519}
                  android.widget.LinearLayout{d09d768 V.E...CL ... 0,543-912,687 #1}
                    android.support.v7.widget.AppCompatImageView{1399f11 V.ED.... ... 48,0-216,144 #7f0f00be app:id/material_drawer_icon}
                    android.widget.LinearLayout{c261876 V.E..... ... 216,0-864,144}
                      android.support.v7.widget.AppCompatTextView{8f08e77 V.ED.... ... 0,43-648,100 #7f0f00bf app:id/material_drawer_name}
                      android.support.v7.widget.AppCompatTextView{df1ae4 G.ED.... ... 0,0-0,0 #7f0f00c0 app:id/material_drawer_description}
                    android.widget.LinearLayout{9881b4d G.E..... ... 0,0-0,0 #7f0f00c4 app:id/material_drawer_badge_container}
                      android.support.v7.widget.AppCompatTextView{1d34602 G.ED.... ... 0,0-0,0 #7f0f00c3 app:id/material_drawer_badge}
                  android.widget.RelativeLayout{dd59b13 V......L ... 0,687-912,738 #9e626c52}
                    android.view.View{ce50150 V.ED.... ... 0,24-912,27 #7f0f00bd app:id/material_drawer_divider}
                  android.widget.LinearLayout{7c81f49 V.E...CL .S. 0,738-912,882 #2}
                    android.support.v7.widget.AppCompatImageView{533c04e V.ED.... .S. 48,0-216,144 #7f0f00be app:id/material_drawer_icon}
                    android.widget.LinearLayout{5f4d56f V.E..... .S. 216,0-864,144}
                      android.support.v7.widget.AppCompatTextView{520e27c V.ED.... .S. 0,43-648,100 #7f0f00bf app:id/material_drawer_name}
                      android.support.v7.widget.AppCompatTextView{bea6705 G.ED.... .S. 0,0-0,0 #7f0f00c0 app:id/material_drawer_description}
                    android.widget.LinearLayout{10f935a G.E..... .S. 0,0-0,0 #7f0f00c4 app:id/material_drawer_badge_container}
                      android.support.v7.widget.AppCompatTextView{423598b G.ED.... .S. 0,0-0,0 #7f0f00c3 app:id/material_drawer_badge}
                  android.widget.LinearLayout{4d7aa68 V.E...CL ... 0,882-912,1026 #3}
                    android.support.v7.widget.AppCompatImageView{a2f6e81 V.ED.... ... 48,0-216,144 #7f0f00be app:id/material_drawer_icon}
                    android.widget.LinearLayout{2a38b26 V.E..... ... 216,0-864,144}
                      android.support.v7.widget.AppCompatTextView{96c0367 V.ED.... ... 0,43-648,100 #7f0f00bf app:id/material_drawer_name}
                      android.support.v7.widget.AppCompatTextView{4b10514 G.ED.... ... 0,0-0,0 #7f0f00c0 app:id/material_drawer_description}
                    android.widget.LinearLayout{6371bd G.E..... ... 0,0-0,0 #7f0f00c4 app:id/material_drawer_badge_container}
                      android.support.v7.widget.AppCompatTextView{c2d33b2 G.ED.... ... 0,0-0,0 #7f0f00c3 app:id/material_drawer_badge}
                  android.widget.LinearLayout{2ab6f03 V.E...CL ... 0,1026-912,1170 #5}
                    android.support.v7.widget.AppCompatImageView{e235e80 V.ED.... ... 48,0-216,144 #7f0f00be app:id/material_drawer_icon}
                    android.widget.LinearLayout{2da6cb9 V.E..... ... 216,0-864,144}
                      android.support.v7.widget.AppCompatTextView{436d8fe V.ED.... ... 0,43-648,100 #7f0f00bf app:id/material_drawer_name}
                      android.support.v7.widget.AppCompatTextView{3ebf85f G.ED.... ... 0,0-0,0 #7f0f00c0 app:id/material_drawer_description}
                    android.widget.LinearLayout{99fe2ac G.E..... ... 0,0-0,0 #7f0f00c4 app:id/material_drawer_badge_container}
                      android.support.v7.widget.AppCompatTextView{d2c1b75 G.ED.... ... 0,0-0,0 #7f0f00c3 app:id/material_drawer_badge}
                  android.widget.RelativeLayout{da3870a V......L ... 0,1170-912,1221 #9e626c53}
                    android.view.View{d81bb7b V.ED.... ... 0,24-912,27 #7f0f00bd app:id/material_drawer_divider}
                  android.widget.LinearLayout{37e7d98 V.E...CL ... 0,1221-912,1365 #4}
                    android.support.v7.widget.AppCompatImageView{2aff9f1 V.ED.... ... 48,0-216,144 #7f0f00be app:id/material_drawer_icon}
                    android.widget.LinearLayout{47b09d6 V.E..... ... 216,0-864,144}
                      android.support.v7.widget.AppCompatTextView{6e69457 V.ED.... ... 0,43-648,100 #7f0f00bf app:id/material_drawer_name}
                      android.support.v7.widget.AppCompatTextView{3a9db44 G.ED.... ... 0,0-0,0 #7f0f00c0 app:id/material_drawer_description}
                    android.widget.LinearLayout{ab9442d G.E..... ... 0,0-0,0 #7f0f00c4 app:id/material_drawer_badge_container}
                      android.support.v7.widget.AppCompatTextView{b75ed62 G.ED.... ... 0,0-0,0 #7f0f00c3 app:id/material_drawer_badge}
Mark Tyers
  • 93
  • 9
  • instead of `Color.parseColor(stundenplandata.getJSONObject(i).getString("color"))` for testing, use `Color.RED` or something and see what happens – pskink Sep 16 '16 at 12:07
  • btw why arent you using `Drawable#setColorFilter`? – pskink Sep 16 '16 at 12:20
  • I used parseColor before and it worked so it can't be the problem. So replacing it with Color.RED it didn't do much. – Mark Tyers Sep 16 '16 at 12:31
  • so if you use Color.RED the color is changing into white? – pskink Sep 16 '16 at 12:45
  • @pskink no, It's always white, even if I don't try to change the color programatically. – Mark Tyers Sep 16 '16 at 12:48
  • @pskink I tried to remove the rectangle from the view and setting the background color, and this isnt working either. It always stays white, despite showing up as red in the xml editor. – Mark Tyers Sep 16 '16 at 12:54
  • run `adb shell dumpsys activity top` and mark the view of question – pskink Sep 16 '16 at 12:58
  • @pskink I added the view hierarchy to my question, if that's what you wanted. – Mark Tyers Sep 16 '16 at 13:04
  • see `android.view.View{ac5bbb V.ED.... ... 0,461-45,461 #7f0f00ad app:id/myrectangle}` it has bounds `0,461-45,461` so top = 461 and bottom = 461 so its height == 0, the same applies to other `app:id/myrectangle` views, change `android:layout_height="wrap_content"` to something else – pskink Sep 16 '16 at 13:15
  • btw what is your `app:id/myrectangle` view for? why arent you setting your Drawable to parent `RelativeLayout` ? – pskink Sep 16 '16 at 13:18
  • It was supposed to be a sidebar rectangle which changes its color depending on the card type. How do I fix the height of it or is there an alternative to do that? – Mark Tyers Sep 16 '16 at 13:20
  • the same way you fixed its width – pskink Sep 16 '16 at 13:20
  • It doesn't go beyond the borders of the card right? So setting the height to lets say 100dp despite not exactly being clean programming, would work? – Mark Tyers Sep 16 '16 at 13:24
  • i have no idea what size you want your view to be... is it 15dp x 15 dp? or 15 dp x full height? something else? – pskink Sep 16 '16 at 13:25
  • its dynamic depending on the content. However I just set it to 1000dp (match_parent didnt work), and it's working thus far. Thanks for the help. – Mark Tyers Sep 16 '16 at 13:26
  • `match_parent` fills the parent `RelativeView` so it has to work – pskink Sep 16 '16 at 13:28
  • Yes I know, but it doesn't :( – Mark Tyers Sep 16 '16 at 13:28

1 Answers1

0

Try the below code:

 GradientDrawable drawable = (GradientDrawable) myrectangle.getBackground();
            drawable.setColor(ContextCompat.getColor(context,stundenplandata.getJSONObject(i).getString("color")));
myrectangle.setBackground(drawable);
kgandroid
  • 5,507
  • 5
  • 39
  • 69