I need to change the background of a button when it is clicked. Here is the code I tried:
private Button btn;
private int colorFlag = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela2);
btn = (Button) findViewById(R.id.button7);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (colorFlag == 0) {
btn.getResources().getDrawable(R.drawable.curisoidade_desligado);
colorFlag = 1;
} else {
btn.getResources().getDrawable(R.drawable.curisoidade);
colorFlag = 0;
}
}
When the id button (button7) is clicked, it should be switched to the off-hook image, however, it is not occurring. In addition to main, should I add something in xml?
Xml:
<Button
android:id="@+id/button7"
android:layout_width="32dp"
android:layout_height="37dp"
android:layout_marginEnd="47dp"
android:background="@drawable/curisoidade"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.97"
app:layout_constraintVertical_bias="0.087" />