Matej Varga
Matej Varga

Reputation: 95

Changing ImageButton Src Image OnClick

I have been trying to change my ImageButton source OnClick and change it back after 500 miliseconds. This is the code (There are more ImageButtons, this is just the complete example of one of the ImageButtons): [UPDATED]

package com.example.apptest;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.mainactivitylayout);

        ImageButton Next=(ImageButton)findViewById(R.id.nexticon);
        // Here there are more buttons //
        
        Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            Intent splash = new Intent(MainActivity.this, NextActivity.class);
                startActivity(splash);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
                ImageButton.setImageResource(R.mipmap.imgbtnonclick);
                Handler h =new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.nexticon);
                        ImageButton.setImageResource(R.mipmap.imgbtn);
                    }
                }, 500);
            }
        });
}
 

It doesn't work, the message grade build says:[UPDATED]

Error:(34, 28) error: non-static method setImageResource(int) cannot be referenced from a static context

Error:(40, 36) error: non-static method setImageResource(int) cannot be referenced from a static context

Hope you can help me. Thanks!

Upvotes: 0

Views: 1737

Answers (4)

Origence
Origence

Reputation: 145

You're trying to assign a drawable to a resource property. use setBackground instead.

ImageButton.setBackground(getResources().getDrawable(R.mipmap.imgbtn));

Something else I see is that ImageButton is a class, that is why you get the error message about static context. You should have a reference of the button you want to change the image using view.findViewById()

You can use ButterKnife to easily bind buttons and apply methods to group of buttons.

Bind the buttons:

@Bind({R.id.nexticon, R.id.nexticon2, R.id.nexticon3, R.id.nexticon4}) List<View> allButtons;

Define the action:

static final ButterKnife.Action<View> changeImageSource = new ButterKnife.Action<View>() {
        @Override public void apply(View view, int index) {
             ImageButton.setImageResource(R.mipmap.imgbtnonclick);
                Handler h =new Handler();
                h.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.nexticon);
                        ImageButton.setImageResource(R.mipmap.imgbtn);
                    }
                }, 500);
        }
    };

Execute the action:

Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
            Intent splash = new Intent(MainActivity.this, NextActivity.class);
                startActivity(splash);
                overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
              ButterKnife.apply(allButtons, ADDLISTENER);
            }
        });

Still what you're trying to do I think is better using a background selector defined in xml and then change the state of the button temporarily. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/buttonok_pressed"
          android:state_enabled="false" />

    <item android:drawable="@drawable/buttonok" />

</selector>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
       android:minWidth="@dimen/button_minwidth"
    >

    <corners android:radius="@dimen/button_radius"/>

    <gradient
    android:angle="45"
        android:centerX="35%"
        android:centerColor="#0070DC"
        android:startColor="#00A0E2"
        android:endColor="#0001CF"
        android:type="linear"
    />

    <padding
        android:left="@dimen/button_paddingH"
        android:top="@dimen/button_paddingV"
        android:right="@dimen/button_paddingH"
        android:bottom="@dimen/button_paddingV"
        />


    <stroke
        android:width="2dp"
        android:color="#0000FF"
    />

</shape>

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle"
       android:minWidth="@dimen/button_minwidth">

    <corners android:radius="@dimen/button_radius"/>

    <gradient
        android:angle="45"
        android:centerX="35%"
        android:centerColor="#0097FC"
        android:startColor="#00D5FF"
        android:endColor="#0011FF"
        android:type="linear"
    />

    <padding
        android:left="@dimen/button_paddingH"
        android:top="@dimen/button_paddingV"
        android:right="@dimen/button_paddingH"
        android:bottom="@dimen/button_paddingV"
    />

    <stroke
        android:width="2dp"
        android:color="#0000FF"
    />

</shape>

Also I suggest you to not go for fast results or just make it work and try different stuff around. I guess you're starting developing with Android and focus on learning and experimenting for now.

Upvotes: 2

Rustam
Rustam

Reputation: 6515

try like this

imageView.setImageResource(R.drawable.your_icon);

or

imageView.setImageDrawable(getResources().getDrawable(R.drawable.your_icon));

imageView.setBackground(..)

this will change background not src

Upvotes: 0

hrskrs
hrskrs

Reputation: 4480

You are accessing Handler and ImageButton statically

Instead of:

 Handler=new Handler();
            Handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ImageButton.setBackgroundResource(getResources().getDrawable(R.mipmap.imgbtn));
                }
            }, 500);

use:

Handler h =new Handler();
            h.postDelayed(new Runnable() {
                @Override
                public void run() {
          //Here ImageButton also should be initialized (findViewByd(R.id.yourImageButton)) 
          ((ImageButton) view.findViewById(R.id.yourImageView)).setBackgroundResource(getResources().getDrawable(R.mipmap.imgbtn));
                }
            }, 500);

Upvotes: 0

Joakim
Joakim

Reputation: 3284

The method you call, setBackgroundResource() expects you to give an Integer as argument. In this case, it refers to the R.mipmap.imgbtn value. instead of writing getResources().getDrawable(R.mipmap.imgbtn), just pass ImageButton.setBackgroundResource(R.mipmap.imgbtn).

Also you should move your imgbtn to the drawable folder instead of mipmap (which is used for launcher icons(Then the code would begetResources().getDrawable(R.drawable.imgbtn)`

Upvotes: 0

Related Questions