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.