Hey so I’ve been searching around for this topic and couldn’t find a whole lot so I think I’m in the clear to ask this. What I want to accomplish is have two image buttons that are overlapping in an XML layout. Then in the .java file when the top button is clicked it dissapears and the second image button appears. When that is clicked it dissapears and the first image button reappears and on and on.
The problem I’m having is when i click the first button it dissapears fine and works for a total of two times around then everything dissapears. I’m not sure if I’m doing this right and there might be a better way to accomplish this than how i’m doing it so I will happily take advice.
Also, the reason i don’t have another line in the fist button that sets musicbutton2 as visible is because when I do that the label highlights in blue and it causes a force close.
optionsActivity.java (New):
package com.crazycastles;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
public class optionsActivity extends Activity {
/** Called when the activity is first created. */
ImageButton musicbutton, musicbutton2;
boolean answer=true;
final MediaPlayer buttonSound = MediaPlayer.create(
optionsActivity.this, R.raw.swords);
@Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
if (answer=true){
//CREATE BUTTON 1 & SOUND
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
musicbutton.setBackgroundResource(R.drawable.musicbutton2);
buttonSound.start();
answer=false;
}
});
}
if (answer!=true){
//CREATE BUTTON 1 & SOUND
final ImageButton musicbutton = (ImageButton) findViewById(R.id.musicbutton);
musicbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
musicbutton.setBackgroundResource(R.drawable.musicbutton);
buttonSound.start();
answer=true;
}
});
}
}
}
options.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/mainscreen" >
<ImageButton
android:id="@+id/musicbutton"
android:layout_width="218dp"
android:layout_height="51dp"
android:layout_marginBottom="100dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="200dp"
android:background="@drawable/musicbutton"
android:scaleType="centerCrop"
/>
</LinearLayout>
I think your problems are coming because of the way you are trying to use the linear layout and the margin top to scoot one of the buttons up.
Check out RelativeLayout if you switch to using one of those as your parent then you can get rid of the margin stuff. The buttons will default to being overlapped like you are aiming for. If you do that and are still getting the same problem then add some Log debugging statments to your click listeners that output when the buttons are changing visibility to try to see what is happening.
If your aim is simply to make the way the button looks change I would recommend only using one button and just changing its background with Button.setBackgroundResource(). You can just keep a boolean flag and use it check whether you should set to R.drawable.musicbutton or R.drawable.musicbutton2