-1

I am creating an android app and I want to have an ImageButton change to a pressed sprite I created when it is pressed and then when it is uppressed to change back to the original sprite.

I tried messing with the .isPressed() function but it is unlike the event function and so it doesn't work the same like ACTION_DOWN, it mostly froze the program and occasionally did something but not what it should.

Can anyone help?

XML:

<ImageButton
                android:id="@+id/player_down"
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:background="#00000000"
                android:scaleType="centerInside"
                app:srcCompat="@drawable/player_down_1"

MainActivity:

public void onClick(View view) {
if (view == player_down) {
     pixelGridView.player_goDown();
}
           

MainActivity with the filed try:

if (view == player_down) {
   player_down.setImageResource(R.drawable.player_down_2);
   if(!player_down.isPressed()) {
        player_down.setImageResource(R.drawable.player_down_1);
        pixelGridView.player_goDown();
   }
}

2 Answers2

1

A simple answer could be using a boolean and different asset like a toggle button

MainActivity

Boolean isClicked = false;

public void onClick(View view) {
if (view == player_down) {

   if(isClicked == true){
       isClicked = false;
       // change your button here
   }else{

       isClicked = true;       
       // change your button here
   }
     pixelGridView.player_goDown();
}
  
Ali
  • 519
  • 4
  • 13
0

you can Create XML into like button.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
    <solid
        android:color="#343434" />
    <stroke
        android:width="1dp"
        android:color="#171717" />
    <corners
        android:radius="3dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>
</item>
  <item>
    <shape>
       <gradient
        android:startColor="#343434"
        android:endColor="#171717"
        android:angle="270" />
    <stroke
        android:width="1dp"
        android:color="#171717" />
    <corners
        android:radius="4dp" />
    <padding
        android:left="10dp"
        android:top="10dp"
        android:right="10dp"
        android:bottom="10dp" />
</shape>
  </item>
</selector>

And set to your Button as Background like this :

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/button"
android:text="String"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

And if you want Change Button Text Color then create XML file at res/color/btnext.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
  android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
  android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>

And set this layout XML will apply the color list to a View:

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/btntext" />

and also you can create two different buttons and switch between them by setting one their visibility to "gone".

narcis dpr
  • 939
  • 2
  • 12
  • 32