Issue
I want to change button colour when pressed, focused and clicked, but I think I must have done something wrong, my code doesn't work. Would someone please advice? Thanks!
This is my soundbtn.xml
in drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/sound_onpress_mdp" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@drawable/sound_onpress_mdp" /> <!-- focused -->
<item android:drawable="@drawable/sound" /> <!-- default -->
</selector>
And then I added background to ImageButton
in layout:
<ImageButton
android:id="@+id/imageSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/soundbtn"
android:contentDescription="@string/image_desc"
android:src="@drawable/sound" />
And this is my Java code:
private ImageButton btnSound;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_game);
btnSound =(ImageButton) findViewById(R.id.imageSound);
btnSound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btnSound.setBackgroundResource(R.drawable.soundbtn);
}
});
}
Solution
try to remove
android:src="@drawable/sound"
and
btnSound.setBackgroundResource(R.drawable.soundbtn);
You just need
android:background="@drawable/soundbtn"
because it calls the items like 'pressed' or 'focused' which has the target drawables.
Answered By - wtsang02
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.