0

First-time Android dev, though have used C# and Java in the past.

Trying to make a simple, Windows 8-like GUI. At the moment I have one tile (ImageButton) with a background color set in activity_main.xml.

    <ImageButton
    android:id="@+id/btn1"
    android:layout_width="120dp"
    android:layout_height="120dp"
    android:background="#FF0000"
    android:onClick="changeColor"/>

And I have a function to change the color in MainActivity.java.

public void changeColor(){
    ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
    btn1.setBackgroundColor(Color.GREEN);
}

Compiles fine, but every time I click the red square, the app crashes.

I'm assuming there's something fundemental about how Android is developed that I'm missing which is leading to a very obvious mistake. Is there a better way to be doing this rather than ImageButtons?

Thanks!

Serephucus
  • 3
  • 1
  • 3

3 Answers3

3

Compiles fine, but every time I click the red square, the app crashes.

Because when adding android:onClick in xml then method must be public and accept a View as its only parameter which we want to call on View click:

public void changeColor(View view){
   ImageButton btn1 = (ImageButton) findViewById(R.id.btn1);
   btn1.setBackgroundColor(Color.GREEN);
}
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • Worked perfectly! What view does this reference exactly? One hasn't been created anywhere, and it's not used anywhere else in this simple example that I can see. – Serephucus Feb 19 '15 at 15:45
  • @Serephucus: see here [Button](http://developer.android.com/reference/android/widget/Button.html) for more information – ρяσѕρєя K Feb 19 '15 at 15:46
2

It's better to create a selector and set it as backgraund to the button.

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

Hope it'll help you. P.S. useful link

Community
  • 1
  • 1
Valentin Baryshev
  • 2,195
  • 3
  • 15
  • 24
0

Is there a better way to be doing this rather than ImageButtons?
Yes, ImageButtons are mainly for creating a 'clickable' image. If you simply want a colored button, a regular Button will do fine.

You can do this by setting your Buttons background right from XML using a selector with a Drawable state list.

A good example can be found here: https://stackoverflow.com/a/3882151/1683141 (a color will also qualify as a Drawable)

Why isn't my current code working?
You should add the view as parameter to your method like this:

public void changeColor(View view){
   view.setBackgroundColor(Color.GREEN);
}
Community
  • 1
  • 1
Mdlc
  • 7,128
  • 12
  • 55
  • 98
  • I should have mentioned that I'll be adding images to the buttons later on - with transparent backgrounds - I just wanted to get the color change working, more to make sure I had a handle on Android generally than anything. – Serephucus Feb 19 '15 at 16:07
  • Ok, anyways, drawable state list backgrounds will also work for ImageButtons, so in case you prefer a full XML solution this will work to. – Mdlc Feb 19 '15 at 16:11
  • I would actually. I'm implementing this as I type. :) – Serephucus Feb 19 '15 at 16:12