setBackgroundColor in android

enter image description here

In this simple game I want to change the background color of the button that I press. But I get the following result, the buttons appearance becomes not good (the shape becomes different):

  • How to correctly use Google Plus Sign In with multiple activities?
  • Parse Dynamic Key Json String using Retrofit
  • How to disable Crashlytics while developing
  • Simple TextView.setText causes 40% CPU Usage
  • Detect that an activity has closed in Android
  • Prevent next fragment (tab) from preload with ViewPager
  • pressedButton.setBackgroundColor(Color.RED);
    

    Is there a nicer way to do that? Thanks.

    [Edit: my full code]

    package com.example.xo_game;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.graphics.Color;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        Button[] btns;
        char[][] gameState = new char[3][3];
        char turn = 'X';
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button[] btns = new Button[9];
    
            btns[0] = (Button) findViewById(R.id.btn1);
            btns[1] = (Button) findViewById(R.id.btn2);
            btns[2] = (Button) findViewById(R.id.btn3);
    
            btns[3] = (Button) findViewById(R.id.btn4);
            btns[4] = (Button) findViewById(R.id.btn5);
            btns[5] = (Button) findViewById(R.id.btn6);
    
            btns[6] = (Button) findViewById(R.id.btn7);
            btns[7] = (Button) findViewById(R.id.btn8);
            btns[8] = (Button) findViewById(R.id.btn9);
    
            for (int i = 0; i < 9; i++) {
                btns[i].setTag(i);
                btns[i].setOnClickListener(clickListener);
                gameState[i / 3][i % 3] = 'E';
            }
        }
    
        View.OnClickListener clickListener = new View.OnClickListener() {
    
            public void onClick(View v) {
                Button pressedButton = (Button) v;
    
                int indexOfPressedButton = Integer.parseInt(pressedButton.getTag()
                        .toString());
    
                int row = indexOfPressedButton / 3;
                int col = indexOfPressedButton % 3;
    
                if (gameState[row][col] != 'E')
                    return;
    
                gameState[row][col] = turn;
    
                String turnAsString = String.valueOf(turn);
    
                pressedButton.setText(turnAsString);
    
                if (turn == 'X') {
                    pressedButton.setBackgroundColor(Color.RED);
                    turn = 'O';
                } else {
                    pressedButton.setBackgroundColor(Color.GREEN);
                    turn = 'X';
                }
            }
        };
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    }
    

    Related posts:

    Do Android devices have a static IP address?
    sl4a python notify question
    Class Not Found Exception when Running YouTube API Demos for Android
    Error: This Android SDK requires Android Developer Toolkit version 22.6.1 or above
    ProgressDialog does not want to update the message
    Popup Menu issue in Android ListView
  • How do I restrict Google App Engine Endpoints API access to only my Android applications?
  • Java constants file
  • Is it possible to set a custom font for entire of application?
  • Receiving onTouch and onClick events with Android
  • Deep-linking intent does not work
  • Android launches system settings instead of my app
  • 4 Solutions collect form web for “setBackgroundColor in android”

    pressedButton.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
    

    create selector file any name like button_selector.xml in drawable folder

    Edited with Gradient

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item android:state_pressed="true">
            <shape android:shape="rectangle">
                <corners android:radius="5dp"/>
                <gradient android:startColor="#ad1c1c" android:endColor="#cc3737" android:angle="90"/>
                <padding android:left="10.0dip" android:top="10.0dip"
                    android:right="10.0dip" android:bottom="10.0dip"/>
                <stroke android:width="1.0dip" android:color="#7d0000"/>
            </shape>
        </item>
        <item android:state_pressed="false">
            <shape android:shape="rectangle">
                <corners android:radius="5dp"/>
                <gradient android:startColor="#cfcfcf" android:endColor="#ebebeb" android:angle="90"/>
                <padding android:left="10.0dip" android:top="10.0dip"
                    android:right="10.0dip" android:bottom="10.0dip"/>
                <stroke android:width="1.0dip" android:color="#8f8f8f"/>
            </shape>
        </item>
    </selector>
    

    then set in button background

    <Button
        android:background="@drawable/button_selector"
    />
    

    As @pratik said save this button.xml in drawable folder

    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 android:shape="rectangle">
                <corners android:radius="5dp"/>
                <solid android:color="#f00"/>
                <padding android:left="10.0dip" android:top="10.0dip"
                    android:right="10.0dip" android:bottom="10.0dip"/>
                <stroke android:width="1.0dip" android:color="#222"/>
            </shape>
        </item>
        <item android:state_pressed="false">
            <shape android:shape="rectangle">
                <corners android:radius="5dp"/>
                <solid android:color="#f1f1f1"/>
                <padding android:left="10.0dip" android:top="10.0dip"
                    android:right="10.0dip" android:bottom="10.0dip"/>
                <stroke android:width="1.0dip" android:color="#222"/>
            </shape>
        </item>
    </selector>
    

    Apply this button as background

    <Button
        android:background="@drawable/button"/>
    

    and in your class file do like this

    public void onClick(View v) {
    
      pressedButton.setPressed(true);
    }
    

    so that the red color will be stable

    try this :

    you have create like this in the ImageButton xml

    create xml file using the button image like this

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
    android:state_pressed="true" 
    android:drawable="@drawable/backbutton" />
    <item 
    android:drawable="@drawable/closebutton" />
    </selector> 
    

    add that xml file as background for IMageButton

    <ImageButton                 
    android:layout_height="50px" 
    android:layout_width="50px" 
    android:id="@+id/settings" 
    android:background="@drawable/settings_button" //setting_button in
                                                         the xml file            
    android:text="Settings"/>
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.