2

first I am telling you about the Layout for my Page. My Button is inside the LinearLayout. and I am using weightSum so that my Button layout_height = "0dp".

Now this is my Button property.

<Button
    android:id="@+id/imagebutton_shape_round"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="1dp"
    android:layout_marginLeft="1dp"
    android:layout_marginRight="1dp"
    android:layout_weight="1.0"
    android:background="@color/button_background"
    android:gravity="center"
    android:drawableLeft="@drawable/round"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
     />

In that I have set Image to the Button which name is round.Now I want to set OnclickListener and Change it's BackGroundColor as well as Image.

 Button imagebutton_shape_round = (Button) findViewById(R.id.imagebutton_shape_round);
           imagebutton_shape_round.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                     imagebutton_shape_round.setCompoundDrawables(getResources().getDrawable(R.drawable.white_round, getApplicationContext().getTheme()), null, null, null);
                    }
                 else {
                    imagebutton_shape_round.setCompoundDrawables(getResources().getDrawable(R.drawable.white_round), null, null, null);
                    }

              imagebutton_shape_round.setBackgroundColor(ContextCompat.getColor(InventoryActivity.this,R.color.linearlayout_background));
                }
            });

as you can see we can set drawableLeft using this code.

Drawable img = getContext().getResources().getDrawable( R.drawable.smiley );
txtVw.setCompoundDrawablesWithIntrinsicBounds( img, null, null, null);

How to programmatically set drawableLeft on Android button?

But my code else part why getDrawable is display deprecated.

I see this link

that why I am using this in If condition getResources().getDrawable(R.drawable.white_round, getApplicationContext().getTheme()), null, null, null))

but else part not work

getResources().getDrawable(R.drawable.white_round), null, null, null);

Update :

Instead of Using this

getResources().getDrawable(R.drawable.white_round, getApplicationContext().getTheme())

I use this so it remove the If else condition

ContextCompat.getDrawable(InventoryActivity.this,R.drawable.white_round)

Now Only One Problem is there

when I click the second Image is not display means when i click the Image is not change.

Community
  • 1
  • 1
Harshad Pansuriya
  • 17,218
  • 7
  • 58
  • 86

1 Answers1

1

When an API is deprecated and the new API that takes its place is most probably always compatible with older devices too, So you don't need to put any condition for Build Version.

I think you should try replace your onClick code snippet with this one and see if it works

Drawable image = ResourcesCompat.getDrawable(R.drawable.white_round);
int h = image.getIntrinsicHeight();
int w = image.getIntrinsicWidth();
image.setBounds( 0, 0, w, h );
imagebutton_shape_round.setCompoundDrawables(image, null, null, null);
imagebutton_shape_round.setBackgroundColor(ContextCompat.getColor(InventoryActivity.this,R.color.linearlayout_background));
Max
  • 12,408
  • 4
  • 48
  • 62