Cássio Bruzasco
Cássio Bruzasco

Reputation: 183

ImageButton not doing anything on click - android

Im doing the front-end of my new app on android, and I came across with a problem. On my first Activity my button works fine and take the user to the second Activity, now the problem appear. When I click on another button to take me to a third Activity nothing happens.

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

    ImageButton saldoButton = (ImageButton)findViewById(R.id.saldoButton);

    saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.activity_saldo);

        }
    });
}

now follow my xml of this button:

            <ImageButton
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:id="@+id/saldoButton"
            android:layout_column="2"
            android:background="?android:attr/selectableItemBackground"
            android:src="@drawable/test02" />

Anyone has a clue what is happening?

Upvotes: 1

Views: 2515

Answers (4)

Agravat Kalpesh
Agravat Kalpesh

Reputation: 11

add android:clickable="true" in ImageButton XML.

Define and Initialization AudioManger.

var audioManager:AudioManager = context.getSystemService(Context.AUDIO_SERVICE) 
as AudioManager

after that.

imageButton.setOnClickListener(View.OnClickListener { view ->
        audioManager.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN,1f);
    })

Upvotes: 0

TyMarc
TyMarc

Reputation: 962

Since you are inflating a new layout, you need to call once again findViewById to regain access to your ImageView since the ID of your button in one layout layout is not the same in another layout.

saldoButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        setContentView(R.layout.activity_saldo);
        saldoButton = (ImageView) findViewById(R.id.saldoButton);
    }
});

That being said, you should create another activity or another fragment instead of inflating another layout.

Upvotes: 1

Diego Fortes
Diego Fortes

Reputation: 9800

You are missing the android:clickable="true" on your XML.

        <ImageButton
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:id="@+id/saldoButton"
        android:layout_column="2"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:src="@drawable/test02" />

And then on your class activity

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

    ImageButton saldoButton = (ImageButton)findViewById(R.id.saldoButton);

    saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d("This Class", "I am a working button!");
            Intent intent = new Intent(this, NewClass.class);
            this.startActivity(intent);

        }
    });
}

it should work now. If it doesn't, please tell me. Good luck!

Upvotes: 2

Erfan Eghterafi
Erfan Eghterafi

Reputation: 5665

You have to start third activity

saldoButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             Intent i= new Intent(currentActivity.this,thirdActivity.class);
             startActivity(i);

        }
    });

Upvotes: 3

Related Questions