Reputation: 529
I'm facing an issue with my Android app involving ImageButtons. I have implemented selectors for the pressed state of the buttons, but the pressed state effect is not being displayed when the buttons are clicked or focused.
Problem: I have defined selectors (consumption_selector.xml, shopping_selector.xml, inventory_selector.xml) for the ImageButton backgrounds to display a different image when pressed or focused. However, despite setting up the selectors correctly, the pressed state effect is not being shown when interacting with the buttons.
What I've Tried: I've ensured that the selector files are correctly defined and placed in the res/drawable directory. Each ImageButton has its background set to the corresponding selector. I've verified that the onClick event is triggered by adding a Toast on button clicks, and it's working fine.
consumption_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/consumption2" android:state_pressed="true" />
<item android:drawable="@drawable/consumption2" android:state_focused="true" />
<item android:drawable="@drawable/consumption" />
</selector>
shopping_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/buying2" android:state_pressed="true" />
<item android:drawable="@drawable/buying2" android:state_focused="true" />
<item android:drawable="@drawable/buying" />
</selector>
inventory_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/inventory2" android:state_pressed="true" />
<item android:drawable="@drawable/inventory2" android:state_focused="true" />
<item android:drawable="@drawable/inventory" />
</selector>
activity_main.xml:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/ImageButton01"
android:layout_width="180dp"
android:layout_height="115dp"
android:src="@drawable/consumo"
android:background="@drawable/consumo_selector"
android:contentDescription="@string/description_consumo"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/ImageButton02"
android:layout_width="180dp"
android:layout_height="115dp"
android:src="@drawable/compras"
android:background="@drawable/compras_selector"
android:contentDescription="@string/description_compras"
android:scaleType="fitXY"
app:layout_constraintStart_toEndOf="@id/ImageButton01"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/ImageButton03"
android:layout_width="180dp"
android:layout_height="115dp"
android:src="@drawable/estoque"
android:background="@drawable/estoque_selector"
android:contentDescription="@string/description_estoque"
android:scaleType="fitXY"
app:layout_constraintStart_toEndOf="@id/ImageButton02"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
package com.project.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = findViewById(R.id.ImageButton01);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Botão 1 clicado!", Toast.LENGTH_SHORT).show();
}
});
}
}
Upvotes: 0
Views: 46
Reputation: 51
Is the android:src
drawable same with the default state drawable of the selector?
If it is, you can just remove src drawable. Then you can see the selector correctly.
Upvotes: 0