Question

I am very new to mobile development, this isn't homework I am working ahead of my class, I developed a simple app that has a button and when it's pressed it shows a message "Hello Android". I would like to build on this and change the color of the background when the onClickListener is called, I will post my code below, I am asking for the best approach to achieve my goal (change background). I want to iterate that this code below works, and that I am not asking for anything to do with the code I have presented, I want to add to it to change the background color (it's currently white, I'm assuming by default). Oh and I have never worked with Java before (very difficult course teaching android/iOS/WinMobile in 1 class). Thank you.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupMessageButton();
}

private void setupMessageButton() {
    // 1. Get a reference to the button
    final Button messageButton = (Button) findViewById(R.id.helloDroidButton);

    //Set the click listener to run my code.
    //Code will run when user clicks button.
    messageButton.setOnClickListener(new View.OnClickListener() {



        @Override
        public void onClick(View v) {
            // Anonymous class? --> not sure what he means
            Log.i("DemoButtonApp", "Hello Android!");
            Toast.makeText(
                    MainActivity.this, 
                    "Hello Android!",  
                    Toast.LENGTH_LONG
                    ).show();
        }
    });
}
Was it helpful?

Solution

Android support feature called Selector , that help you to change the background of any view in each state of it like pressed , forces and so one , take look on this useful tutorial and feed me back in any not obvious point

http://www.mkyong.com/android/android-imagebutton-selector-example/

hope it help you

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top