1

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?

Cássio Bruzasco
  • 183
  • 2
  • 13
  • You need to call 'findViewById' once again to regain access to your ImageView http://stackoverflow.com/questions/7052484/calling-setcontentview-multiple-times – TyMarc May 21 '15 at 19:25
  • 1
    Just be sure only calling `setContentView(R.layout.activity_saldo);` doesn't mean you are starting new activity, it is the same activity with only layout will be changed. And to interact with new layout view you have to define views again using `findViewById()`. – user370305 May 21 '15 at 19:28
  • @TyMarc where I need to call it again ? – Cássio Bruzasco May 21 '15 at 19:34
  • @user370305 Im using `setContentView` to go to my third Activity, its another layout. I still doesnt get what I need to do for this button starting working. – Cássio Bruzasco May 21 '15 at 19:35
  • 1
    @CássioBruzasco You should read this http://developer.android.com/training/basics/firstapp/starting-activity.html – ci_ May 21 '15 at 19:38
  • @ci_ I read this doc and I made this `android:onClick="saldo"` on my ImageButton on xml and `public void saldo(View v) { ImageButton saldoButton = (ImageButton)findViewById(R.id.saldoButton); saldoButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent i = new Intent(FirstPage.this,saldo.class); startActivity(i); } }); }` and my app crashs. – Cássio Bruzasco May 21 '15 at 19:47
  • Add the logcat output when you click on your button, it will help us to help you :) – Vincent D. May 21 '15 at 20:07

4 Answers4

3

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);

        }
    });
Erfan Eghterafi
  • 4,344
  • 1
  • 33
  • 44
2

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!

Diego Fortes
  • 8,830
  • 3
  • 32
  • 42
1

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.

TyMarc
  • 932
  • 7
  • 13
0

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);
    })