java - Multiple ImageButtons with setOnClickListener (clicks but does not launch activities) -
i have managed start 2 activities using imagebutton along .setonclicklistener tied it, have included layouts different imagebuttons. each button launches activity. have created activities. have managed remover crash bugs, lint errors, have latest android sdk. buttons stop working though hear click. neither activity launches on first imagebutton nor second one.
this happens moment put multiple imagebuttons in. works 1 button. suspect (this) command in class confused call. intent call method start new activities streamlined , basic quick, uncomplicated access.
can please me why multiple setonclicklistener cannot tied relevant imagebuttons please?
import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.content.intent; import android.view.view.onclicklistener; import android.widget.imagebutton; public class mainactivity extends actionbaractivity implements onclicklistener { imagebutton imagebutton1; imagebutton imagebutton2; imagebutton imagebutton3; imagebutton imagebutton4; intent intent; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.reusable_layout); imagebutton1=(imagebutton)findviewbyid(r.id.imagebutton1); imagebutton2=(imagebutton)findviewbyid(r.id.imagebutton2); imagebutton3=(imagebutton)findviewbyid(r.id.imagebutton3); imagebutton4=(imagebutton)findviewbyid(r.id.imagebutton4); imagebutton1.setonclicklistener(this); imagebutton2.setonclicklistener(this); imagebutton3.setonclicklistener(this); imagebutton4.setonclicklistener(this); } public void onclick1(view view) { intent intent = new intent(this, otheractivity.class); startactivity(intent); } public void onclick2(view view) { intent intent = new intent(this, otheractivity2.class); startactivity(intent); }
if notice have coded 2 of 4 buttons (so once code works in theory others should).
this otheractivity imagebutton1 calls.
package com.example.startanotheractivity; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.imagebutton; public class otheractivity extends activity implements onclicklistener { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.other_layout); intent intent = getintent(); } @override public void onclick(view v) { // todo auto-generated method stub } }
and activity imagebutton2 calls
package com.example.startanotheractivity; import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.imagebutton; public class otheractivity2 extends activity implements onclicklistener { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.other_layout_2); intent intent = getintent(); } @override public void onclick(view v) { // todo auto-generated method stub } }
here layout.xml
<?xml version="1.0" encoding="utf-8"?> <scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:background="#ffffff" > <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="vertical" > <imagebutton android:id="@+id/imagebutton1" android:layout_width="566dp" android:layout_height="456dp" android:background="@null" android:src="@drawable/gatanga1" android:onclick="onclick1" /> <imagebutton android:id="@+id/imagebutton2" android:layout_width="540dp" android:layout_height="189dp" android:background="@null" android:src="@drawable/gatanga2" android:onclick="onclick2" /> <imagebutton android:id="@+id/imagebutton3" android:layout_width="540dp" android:layout_height="189dp" android:background="@null" android:src="@drawable/gatanga3" android:onclick="onclick3" /> <imagebutton android:id="@+id/imagebutton4" android:layout_width="540dp" android:layout_height="189dp" android:background="@null" android:src="@drawable/gatanga4" android:onclick="onclick4" /> </linearlayout>
thank you
many many thanks.
use switch statement in onclick method
@override public void onclick(view v) { switch(v.getid()){ case r.id.imagebutton1: /** start new activity otheractivity.java */ intent intent = new intent(mainactivity.this, mycards.class); this.startactivity(intent); break; case r.id.imagebutton2: /** start new activity otheractivity2.java */ intent intent2 = new intent(mainactivity.this, otheractivity2.class); this.startactivity(intent2); break; }
Comments
Post a Comment